createResource
创建资源。
const resource = await drupal.createResource<T = JsonApiResource>( type, body, options?: { params, withAuth, deserialize, }): Promise<T>类型:字符串- 必需
- 资源类型。例如:
node--article、taxonomy_term--tags或block_content--basic。
body:JsonApiCreateResourceBody- 必需
- 带有
data的主体有效负载。 - interface JsonApiCreateResourceBody {data: {type?: stringattributes?: Record<string, unknown>relationships?: Record<string, JsonApiResourceBodyRelationship>}}
选项- 可选
params: JsonApiParams: JSON:API 参数,例如filter、fields、include或sort。withAuth:布尔值 | DrupalClientAuth:- 设置要使用的身份验证方法。请参阅身份验证文档。
- 将此设置为
true以使用客户端上配置的身份验证方法。
deserialize: boolean: 设置为 false 以返回原始 JSON:API 响应。
示例
- 创建一个
node--page资源。
const page = await drupal.createResource("node--page", { data: { attributes: { title: "Page Title", body: { value: "<p>Content of body field</p>", format: "full_html", }, }, },})- 获取一个包含分类术语的
node--article资源。
const article = await drupal.createResource("node--article", { data: { attributes: { title: "Title of Article", body: { value: "<p>Content of body field</p>", format: "full_html", }, }, relationships: { field_category: { data: { type: "taxonomy_term--category", id: "28ab9f26-927d-4e33-9510-b59a7ccdafe6", }, }, }, },})- 使用过滤器。
const page = await drupal.createResource( "node--page", { data: { attributes: { title: "Page Title", body: { value: "<p>Content of body field</p>", format: "full_html", }, }, }, }, { params: { "fields[node--page]": "title,path", }, })TypeScript
- 使用
DrupalNode用于节点实体类型。
import { DrupalNode } from "next-drupal"
const page = await drupal.createResource<DrupalNode>("node--page", { data: { attributes: { title: "Page Title", body: { value: "<p>Content of body field</p>", format: "full_html", }, }, },})查看 TypeScript 文档 以了解更多内置类型。