@@ -64,11 +64,29 @@ export class NextDrupalPages extends NextDrupal {
6464 }
6565
6666 /**
67- * Gets the entry point for a given resource type.
67+ * Get the JSON:API entry for a resource type.
6868 *
69- * @param {string } resourceType The resource type.
70- * @param {Locale } locale The locale.
69+ * @param {string } resourceType The resource type. Example: `node--article`.
70+ * @param {Locale } locale Optional. The locale to fetch the index. Example: `es` or `fr` .
7171 * @returns {Promise<string> } The entry point URL.
72+ * @remarks
73+ * By default, when retrieving resources in `getResource` or `getResourceCollection`,
74+ * the `DrupalClient` make a request to Drupal to fetch the JSON:API resource entry.
75+ *
76+ * Example: if you provide `node--article`, `DrupalClient` will make a request to
77+ * `http://example.com/jsonapi/node/article`.
78+ *
79+ * If you would like to infer the entry from the resource type, use the useDefaultResourceTypeEntry option:
80+ * ```ts
81+ * const drupal = new DrupalClient(process.env.NEXT_PUBLIC_DRUPAL_BASE_URL, {
82+ * useDefaultResourceTypeEntry: true,
83+ * })
84+ * ```
85+ * @example
86+ * ```ts
87+ * // https://example.com/jsonapi/node/article
88+ * const url = await drupal.getEntryForResourceType(`node--article`)
89+ * ```
7290 */
7391 async getEntryForResourceType (
7492 resourceType : string ,
@@ -88,10 +106,89 @@ export class NextDrupalPages extends NextDrupal {
88106 /**
89107 * Gets a resource from the context.
90108 *
91- * @param {string | DrupalTranslatedPath } input The input path or translated path.
92- * @param {GetStaticPropsContext } context The static props context.
109+ * @param {string | DrupalTranslatedPath } input Either a resource type (e.g. "node--article") or a translated path from translatePath() .
110+ * @param {GetStaticPropsContext } context The Next.js context from getStaticProps .
93111 * @param {Object } options Options for the request.
112+ * @param {PathPrefix } [options.pathPrefix] The path prefix to use for the request (defaults to "/").
113+ * @param {boolean } [options.isVersionable] Whether the resource is versionable (defaults to false for all entity types except nodes).
94114 * @returns {Promise<T> } The fetched resource.
115+ * @remarks
116+ * The localized resource will be fetched based on the `locale` and `defaultLocale` values from `context`.
117+ *
118+ * If you pass in a `DrupalTranslatedPath` for input, `getResourceFromContext` will take the `type` and `id` from the path and make a `getResource` call to Drupal:
119+ * ```ts
120+ * export async function getStaticProps(context) {
121+ * const path = await drupal.translatePathFromContext(context)
122+ *
123+ * const node = await drupal.getResourceFromContext(path, context)
124+ *
125+ * return {
126+ * props: {
127+ * node,
128+ * },
129+ * }
130+ * }
131+ * ```
132+ *
133+ * If you pass in a `string` input, such as `node--article`, `getResourceFromContext` will make a subrequest call to Drupal to translate the path and then fetch the resource.
134+ * You will need both the [Subrequests](https://drupal.org/project/subrequests) and [Decoupled Router](https://drupal.org/project/decoupled_router) modules:
135+ * ```ts
136+ * export async function getStaticProps(context) {
137+ * const node = await drupal.getResourceFromContext("node--article", context)
138+ *
139+ * return {
140+ * props: {
141+ * node,
142+ * },
143+ * }
144+ * }
145+ * ```
146+ * @examples
147+ * Fetch a resource from context.
148+ * ```ts title=pages/[[...slug]].tsx
149+ * export async function getStaticProps(context) {
150+ * const node = await drupal.getResourceFromContext("node--page", context)
151+ *
152+ * return {
153+ * props: {
154+ * node,
155+ * },
156+ * }
157+ * }
158+ * ```
159+ * Fetch a resource from context in a sub directory.
160+ * ```ts title=pages/articles/[[...slug]].tsx
161+ * export async function getStaticProps(context) {
162+ * const node = await drupal.getResourceFromContext("node--page", context, {
163+ * pathPrefix: "/articles",
164+ * })
165+ *
166+ * return {
167+ * props: {
168+ * node,
169+ * },
170+ * }
171+ * }
172+ * ```
173+ * Using DrupalNode type:
174+ * ```ts
175+ * import { DrupalNode } from "next-drupal"
176+ *
177+ * const node = await drupal.getResourceFromContext<DrupalNode>(
178+ * "node--page",
179+ * context
180+ * )
181+ * ```
182+ * Using DrupalTaxonomyTerm type:
183+ * ```ts
184+ * import { DrupalTaxonomyTerm } from "next-drupal"
185+ *
186+ * const term = await drupal.getResourceFromContext<DrupalTaxonomyTerm>(
187+ * "taxonomy_term--tags",
188+ * context
189+ * )
190+ * ```
191+ * @see {@link https://next-drupal.org/docs/typescript } for more built-in types.
95192 */
96193 async getResourceFromContext < T extends JsonApiResource > (
97194 input : string | DrupalTranslatedPath ,
@@ -179,10 +276,37 @@ export class NextDrupalPages extends NextDrupal {
179276 /**
180277 * Gets a collection of resources from the context.
181278 *
182- * @param {string } type The type of the resources.
183- * @param {GetStaticPropsContext } context The static props context.
279+ * @param {string } type The type of the resources. Example: `node--article` or `user--user`.
280+ * @param {GetStaticPropsContext } context The static props context from getStaticProps or getServerSideProps .
184281 * @param {Object } options Options for the request.
282+ * - deserialize: Set to false to return the raw JSON:API response
185283 * @returns {Promise<T> } The fetched collection of resources.
284+ * @remarks
285+ * The localized resources will be fetched based on the `locale` and `defaultLocale` values from `context`.
286+ * @example
287+ * Get all articles from context
288+ * ```
289+ * export async function getStaticProps(context) {
290+ * const articles = await drupal.getResourceCollectionFromContext(
291+ * "node--article",
292+ * context
293+ * )
294+ *
295+ * return {
296+ * props: {
297+ * articles,
298+ * },
299+ * }
300+ * }
301+ * ```
302+ * Using TypeScript with DrupalNode for a node entity type
303+ * ```
304+ * import { DrupalNode } from "next-drupal"
305+ * const nodes = await drupal.getResourceCollectionFromContext<DrupalNode[]>(
306+ * "node--article",
307+ * context
308+ * )
309+ * ```
186310 */
187311 async getResourceCollectionFromContext < T = JsonApiResource [ ] > (
188312 type : string ,
@@ -227,9 +351,17 @@ export class NextDrupalPages extends NextDrupal {
227351 /**
228352 * Translates a path from the context.
229353 *
230- * @param {GetStaticPropsContext } context The static props context .
354+ * @param {GetStaticPropsContext } context The context from `getStaticProps` or `getServerSideProps` .
231355 * @param {Object } options Options for the request.
232356 * @returns {Promise<DrupalTranslatedPath | null> } The translated path.
357+ * @requires Decoupled Router module
358+ * @example
359+ * Get info about a path from `getStaticProps` context
360+ * ```ts
361+ * export async function getStaticProps(context) {
362+ * const path = await drupal.translatePathFromContext(context)
363+ * }
364+ * ```
233365 */
234366 async translatePathFromContext (
235367 context : GetStaticPropsContext ,
@@ -251,11 +383,18 @@ export class NextDrupalPages extends NextDrupal {
251383 }
252384
253385 /**
254- * Gets the path from the context.
386+ * Return the path (slug) from getStaticProps or getServerSideProps context.
255387 *
256- * @param {GetStaticPropsContext } context The static props context .
388+ * @param {GetStaticPropsContext } context The context from `getStaticProps` or `getServerSideProps` .
257389 * @param {Object } options Options for the request.
258390 * @returns {string } The constructed path.
391+ * @example
392+ * Get the path (slug) from `getStaticProps` context
393+ * ```ts
394+ * export async function getStaticProps(context) {
395+ * const slug = await drupal.getPathFromContext(context)
396+ * }
397+ * ```
259398 */
260399 getPathFromContext (
261400 context : GetStaticPropsContext ,
@@ -275,10 +414,33 @@ export class NextDrupalPages extends NextDrupal {
275414 /**
276415 * Gets static paths from the context.
277416 *
278- * @param {string | string[] } types The types of the resources.
279- * @param {GetStaticPathsContext } context The static paths context.
280- * @param {Object } options Options for the request.
281- * @returns {Promise<GetStaticPathsResult<{ slug: string[] }>["paths"]> } The fetched static paths.
417+ * @param {string | string[] } types The resource types. Example: `node--article` or `["taxonomy_term--tags", "user--user"]`.
418+ * @param {GetStaticPathsContext } context The context from `getStaticPaths`.
419+ * @param {object } options Options for the request.
420+ * @returns {Promise<GetStaticPathsResult<{ slug: string[] }>["paths"]> } The static paths.
421+ * @example
422+ * Return static paths for `node--page` resources
423+ * ```ts
424+ * export async function getStaticPaths(context) {
425+ * return {
426+ * paths: await drupal.getStaticPathsFromContext("node--page", context),
427+ * fallback: "blocking",
428+ * }
429+ * }
430+ * ```
431+ *
432+ * Return static paths for `node--page` and `node--article` resources
433+ * ```ts
434+ * export async function getStaticPaths(context) {
435+ * return {
436+ * paths: await drupal.getStaticPathsFromContext(
437+ * ["node--page", "node--article"],
438+ * context
439+ * ),
440+ * fallback: "blocking",
441+ * }
442+ * }
443+ * ```
282444 */
283445 async getStaticPathsFromContext (
284446 types : string | string [ ] ,
@@ -414,11 +576,33 @@ export class NextDrupalPages extends NextDrupal {
414576 }
415577
416578 /**
417- * Handles preview mode.
579+ * Handle preview mode for resources .
418580 *
419- * @param {NextApiRequest } request The API request .
420- * @param {NextApiResponse } response The API response .
581+ * @param {NextApiRequest } request The `request` from an API route .
582+ * @param {NextApiResponse } response The `response` from an API route .
421583 * @param {Object } options Options for the request.
584+ * @returns {Promise<void> }
585+ * @remarks
586+ * The `preview` method should be called in an API route.
587+ * Remember to set a `previewSecret` on the client.
588+ * ```ts
589+ * // lib/drupal.ts
590+ * export const drupal = new DrupalClient(
591+ * process.env.NEXT_PUBLIC_DRUPAL_BASE_URL,
592+ * {
593+ * previewSecret: process.env.DRUPAL_PREVIEW_SECRET,
594+ * }
595+ * )
596+ * ```
597+ * @example
598+ * ```ts
599+ * // pages/api/preview.ts
600+ * import { drupal } from "lib/drupal"
601+ *
602+ * export default async function handler(req, res) {
603+ * return await drupal.preview(req, res)
604+ * }
605+ * ```
422606 */
423607 async preview (
424608 request : NextApiRequest ,
0 commit comments