Skip to content

Commit 27cd1b7

Browse files
committed
docs: initial revisions to getResourceFromContext typedoc
1 parent 61488cd commit 27cd1b7

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

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

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,64 @@ export class NextDrupalPages extends NextDrupal {
8888
/**
8989
* Gets a resource from the context.
9090
*
91-
* @param {string | DrupalTranslatedPath} input The input path or translated path.
92-
* @param {GetStaticPropsContext} context The static props context.
91+
* @param {string | DrupalTranslatedPath} input Either a resource type (e.g. "node--article") or a translated path from translatePath().
92+
* @param {GetStaticPropsContext} context The Next.js context from getStaticProps.
9393
* @param {Object} options Options for the request.
94+
* @param {PathPrefix} [options.pathPrefix] The path prefix to use for the request (defaults to "/").
95+
* @param {boolean} [options.isVersionable] Whether the resource is versionable (defaults to false for all entity types except nodes).
9496
* @returns {Promise<T>} The fetched resource.
97+
* @remarks
98+
* The localized resource will be fetched based on the `locale` and `defaultLocale` values from `context`.
99+
*
100+
* 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:
101+
* ```ts
102+
* export async function getStaticProps(context) {
103+
* const path = await drupal.translatePathFromContext(context)
104+
*
105+
* const node = await drupal.getResourceFromContext(path, context)
106+
*
107+
* return {
108+
* props: {
109+
* node,
110+
* },
111+
* }
112+
* }
113+
* ```
114+
*
115+
* 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.
116+
* You will need both the [Subrequests](https://drupal.org/project/subrequests) and [Decoupled Router](https://drupal.org/project/decoupled_router) modules:
117+
* ```ts
118+
* export async function getStaticProps(context) {
119+
* const node = await drupal.getResourceFromContext("node--article", context)
120+
*
121+
* return {
122+
* props: {
123+
* node,
124+
* },
125+
* }
126+
* }
127+
* ```
128+
* @examples
129+
*
130+
* Using DrupalNode type:
131+
* ```ts
132+
* import { DrupalNode } from "next-drupal"
133+
*
134+
* const node = await drupal.getResourceFromContext<DrupalNode>(
135+
* "node--page",
136+
* context
137+
* )
138+
* ```
139+
* Using DrupalTaxonomyTerm type:
140+
* ```ts
141+
* import { DrupalTaxonomyTerm } from "next-drupal"
142+
*
143+
* const term = await drupal.getResourceFromContext<DrupalTaxonomyTerm>(
144+
* "taxonomy_term--tags",
145+
* context
146+
* )
147+
* ```
148+
* @see {@link https://next-drupal.org/docs/typescript} for more built-in types.
95149
*/
96150
async getResourceFromContext<T extends JsonApiResource>(
97151
input: string | DrupalTranslatedPath,

0 commit comments

Comments
 (0)