npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

drupal-fetch

v0.1.9

Published

Helper functions to fetch JSON:API resources from a Drupal site.

Downloads

132

Readme

BUILD Publish

Drupal Fetch

Helper functions to fetch JSON:API resources from a Drupal site.

This library simplifies the process of retrieving data from a Drupal backend and handles common operations such as fetching single resources, resource collections, menu items and views.

Installation

Install drupal-fetch, use the package manager of your choice:

Note: drupal-jsonapi-params is a helper library used to create complex queries easily.

npm install drupal-fetch drupal-jsonapi-params

Usage/Examples

Import and Instantiate Fetcher:

import { DrupalFetch } from "drupal-fetch";

const fetcher = new DrupalFetch("https://drupal-site-base-url.com");

Fetching a Single Resource:

// fetch article with uuid.
const article = await fetcher.getResource<DrupalNode>(
  "node--article",
  "2a8758e2-70a5-4ffb-ade1-25da62963bd6"
);

// fetch a text block paragraph with uuid.
const textBlock = await fetcher.getResource<DrupalParagraph>(
  "paragraph-text_block",
  "2a8758e2-70a5-4ffb-ade1-25da62963bd6"
);

Fetching a Collection of Resources:

// fetch a sorted list of published node--resource content.
const resources = await fetcher.getResourceCollection<DrupalNode[]>(
  "node--resources",
  {
    params: new DrupalJsonApiParams()
      .addFilter("status", "1") // filter out unpublished content.
      .addSort("created", "DESC"), // sort by created date.
  }
);

Fetching Menu Items

The getMenu method returns a drupal menu in a tree-like format.

const mainMenu = await fetcher.getMenu("main");

Fetching View Results

// pass contextual filter params to drupal view.
const params = new DrupalJsonApiParams();
params.addCustomParam({ "views-argument": ["11+15+19"] });

const menuView = await fetcher.getView("menu--default", { params });

Using drupal-jsonapi-params

  • We can form requests for specific data from drupal entities, and sort, paginate, filter them as needed using the drupal-jsonapi-params package.
  • Most fields, entities have an id that can be useful as a key prop when mapping through items in React.

Examples:

Here are a few examples for fetching various fields on a drupal resource:

Basic usage

Most fields can be accessed simply by adding the field name to the parameters.

  • These include title, Text (plain), Boolean, Color etc
const params = new DrupalJsonApiParams().addFields("node--resource", [
  "title",
  "field_subtitle",
  "field_theme_color",
  "field_is_enabled",
]);

Output:

{
  type: 'node--resource',
  title: 'Example Resource',
  field_subtitle: 'Test subtitle value',
  field_theme_color: { color: '#FAFAFA', opacity: null },
  field_is_enabled: false,
}

Note: some fields may be objects like field_theme_color above. These can be accessed in the React component like any other json object.

Accessing referenced fields

  • Some fields are Drupal entities themselves.
  • However, these objects can get really large, so we can request only the required properties as below:

Media Entity

const params = new DrupalJsonApiParams()
  .addFields("node--resource", ["field_thumbnail"]) // get Media field on Resource content type
  .addInclude(["field_thumbnail", "field_thumbnail.field_media_image"]) // request for that Media Entity, and the nested File Entity.
  .addFields("file--file", ["uri", "resourceIdObjMeta"]); // For that File Entity, get uri and metadata for alt, height, width.

Output:

{
  type: 'media--image',
  field_media_image: {
    type: 'file--file',
    uri: {
      value: 'public://image.jpg',
      url: 'http://site/image-pathimage.jpg'
    },
    resourceIdObjMeta: {
      alt: 'alt',
      title: '',
      width: 640,
      height: 960,
    }
  },
}

Taxonomy Term

  • Access data for a taxonomy vocabulary called "Audience", referenced in the field_audience:
const params = new DrupalJsonApiParams()
  .addFields("node--resource", ["field_audience"]) // get taxonomy field on Resource content type
  .addInclude(["field_audience"]) // request for that taxonomy entity's data
  .addFields("taxonomy_term--audience", ["name", "path"]); // For that Taxonomy Entity, get name, path, or any other fields

Output:

{
  type: 'node--resource',
  field_audience: {
    type: 'taxonomy_term--audience',
    id: '345feac2-e262-4298-91d3-c7d965c73f56',
    name: 'Students',
    path: { alias: '/students', pid: 3, langcode: 'en' },
  },
}