@drupal-api-client/utils
v0.4.0
Published
Shared utilities for the Drupal API Client
Downloads
27,011
Readme
Utilities and other tools
This package contains optional utilities that may be useful for working with the Drupal API Client, but are not included in the base package.
Installation
npm install @drupal-api-client/utilsUsage
createCache
import { createCache } from "@drupal-api-client/utils";
const cache = createCache();A cache based on Nanostores that satisfies the @drupal-api-client/api-client cache interface.
DefaultSerializer
A serializer that extends Jsona and attaches JSON:API top-level meta and links to deserialized results via getMeta() and getLinks() methods. Use it with JsonApiClient when you need access to document-level meta and links without cluttering the resource object.
import { DefaultSerializer, createCache } from "@drupal-api-client/utils";
import { JsonApiClient } from "@drupal-api-client/json-api-client";
const client = new JsonApiClient(baseUrl, {
serializer: new DefaultSerializer(),
cache: createCache(),
});
// Single resource: result has the same shape as Jsona, plus getMeta() and getLinks()
const article = await client.getResource("node--article", "123");
article.getMeta(); // top-level "meta" from the response, or undefined
article.getLinks(); // top-level "links" from the response, or undefined
// Collection: the array has getMeta() and getLinks() on the result itself
const articles = await client.getCollection("node--article");
articles.getMeta(); // top-level "meta", or undefined
articles.getLinks(); // top-level "links", or undefinedDefaultSerializer uses the same deserialized shape as Jsona (flattened attributes, etc.). It also exposes Jsona’s serialize method, so you can re-serialize data when updating or creating resources. Meta and links are stored on the result with non-enumerable properties, so they do not appear in Object.keys() or JSON output.
