公用事业和其他工具
此套件包含可选的公用设施,可能对 Drupal API 客户端工作有用,但并不包含在基础套件中.
安装
npm install @drupal-api-client/utils
使用量
创建缓存
import { createCache } from "@drupal-api-client/utils";
const cache = createCache();
基于 纳米店 满足 @drupal-api-client/api-client 缓存接口 .
默认序列器
延伸的序列器 乔纳 并附上 JSON: API 顶级 meta 财务报告和审定财务报表 links 将结果通过 getMeta() 财务报告和审定财务报表 getLinks() 方法。 。 。 用这个 JsonApiClient 当您需要访问文档级别元和链接而不对资源对象进行挤压时.
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 undefined
DefaultSerializer 使用与Jsona相同的去序列化形状(平分属性等). 也揭露了乔娜的 serialize 方法,因此在更新或创建资源时可以重排数据序列。 Meta和链接被存储在结果上,具有不可假定的属性,所以它们不出现在 Object.keys() 或者JSON输出.