Skip to content

Commit af6086a

Browse files
committed
docs: add reference examples to next-drupal createResource
1 parent b06eb64 commit af6086a

File tree

1 file changed

+77
-3
lines changed

1 file changed

+77
-3
lines changed

packages/next-drupal/src/next-drupal.ts

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,84 @@ export class NextDrupal extends NextDrupalBase {
9393
/**
9494
* Creates a new resource of the specified type.
9595
*
96-
* @param {string} type The type of the resource.
97-
* @param {JsonApiCreateResourceBody} body The body of the resource.
98-
* @param {JsonApiOptions} options Options for the request.
96+
* @param {string} type The type of the resource. Example: `node--article`, `taxonomy_term--tags`, or `block_content--basic`.
97+
* @param {JsonApiCreateResourceBody} body The body payload with data.
98+
* @param {JsonApiOptions & JsonApiWithNextFetchOptions} options Options for the request.
9999
* @returns {Promise<T>} The created resource.
100+
* @example
101+
* Create a node--page resource
102+
* ```
103+
* const page = await drupal.createResource("node--page", {
104+
* data: {
105+
* attributes: {
106+
* title: "Page Title",
107+
* body: {
108+
* value: "<p>Content of body field</p>",
109+
* format: "full_html",
110+
* },
111+
* },
112+
* },
113+
* })
114+
* ```
115+
* Create a node--article with a taxonomy term
116+
* ```
117+
* const article = await drupal.createResource("node--article", {
118+
* data: {
119+
* attributes: {
120+
* title: "Title of Article",
121+
* body: {
122+
* value: "<p>Content of body field</p>",
123+
* format: "full_html",
124+
* },
125+
* },
126+
* relationships: {
127+
* field_category: {
128+
* data: {
129+
* type: "taxonomy_term--category",
130+
* id: "28ab9f26-927d-4e33-9510-b59a7ccdafe6",
131+
* },
132+
* },
133+
* },
134+
* },
135+
* })
136+
* ```
137+
* Using filters
138+
* ```
139+
* const page = await drupal.createResource(
140+
* "node--page",
141+
* {
142+
* data: {
143+
* attributes: {
144+
* title: "Page Title",
145+
* body: {
146+
* value: "<p>Content of body field</p>",
147+
* format: "full_html",
148+
* },
149+
* },
150+
* },
151+
* },
152+
* {
153+
* params: {
154+
* "fields[node--page]": "title,path",
155+
* },
156+
* }
157+
* )
158+
* ```
159+
* Using TypeScript with DrupalNode
160+
* ```
161+
* import { DrupalNode } from "next-drupal"
162+
* const page = await drupal.createResource<DrupalNode>("node--page", {
163+
* data: {
164+
* attributes: {
165+
* title: "Page Title",
166+
* body: {
167+
* value: "<p>Content of body field</p>",
168+
* format: "full_html",
169+
* },
170+
* },
171+
* },
172+
* })
173+
* ```
100174
*/
101175
async createResource<T extends JsonApiResource>(
102176
type: string,

0 commit comments

Comments
 (0)