Quick Start

Install Drupal State from NPM:

npm i @gdwc/drupal-state

Import DrupalState in your JavaScript application:

import { DrupalState } from '@gdwc/drupal-state';

Create an instance of the store and specify the root of your API:

const store = new DrupalState({
  apiBase: 'https://dev-ds-demo.pantheonsite.io',
  apiPrefix: 'jsonapi', // optional, defaults to 'jsonapi'
});

Get a collection of objects:

// If the object doesn't exist in local state, it will be fetched from the API
// and then added to the store
const recipesFromApi = await store.getObject({ objectName: 'node--recipe' });

Get a single object:

// Since the object is already in local state as part of the recipes collection,
// this will be returned from the store without requiring a fetch from Drupal.
const recipeFromStore = await store.getObject({
  objectName: 'recipes',
  id: '33386d32-a87c-44b9-b66b-3dd0bfc38dca',
});

Try it!

Edit Drupal State Quickstart