Skip to content

Commit bd967a8

Browse files
authored
Improvements to API Reference (#830)
1 parent f09ca89 commit bd967a8

21 files changed

+857
-93
lines changed

packages/next-drupal/src/deprecated/get-access-token.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { AccessToken } from "../types"
33

44
const CACHE_KEY = "NEXT_DRUPAL_ACCESS_TOKEN"
55

6+
/** @deprecated */
67
export async function getAccessToken(): Promise<AccessToken> {
78
if (!process.env.DRUPAL_CLIENT_ID || !process.env.DRUPAL_CLIENT_SECRET) {
89
return null

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface JsonApiLinks {
1313
[key: string]: string | Record<string, string>
1414
}
1515

16+
/** @hidden */
1617
export class JsonApiErrors extends Error {
1718
errors: JsonApiError[] | string
1819
statusCode: number

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

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,21 @@ export class NextDrupalBase {
179179
/**
180180
* Fetches a resource from the given input URL or path.
181181
*
182-
* @param {RequestInfo} input The input URL or path.
183-
* @param {FetchOptions} init The fetch options.
182+
* @param {RequestInfo} input The url to fetch from.
183+
* @param {FetchOptions} init The fetch options with `withAuth`.
184+
* If `withAuth` is set, `fetch` will fetch an `Authorization` header before making the request.
184185
* @returns {Promise<Response>} The fetch response.
186+
* @remarks
187+
* To provide your own custom fetcher, see the fetcher docs.
188+
* @example
189+
* ```ts
190+
* const url = drupal.buildUrl("/jsonapi/node/article", {
191+
* sort: "-created",
192+
* "fields[node--article]": "title,path",
193+
* })
194+
*
195+
* const response = await drupal.fetch(url.toString())
196+
* ```
185197
*/
186198
async fetch(
187199
input: RequestInfo,
@@ -269,9 +281,35 @@ export class NextDrupalBase {
269281
/**
270282
* Builds a URL with the given path and search parameters.
271283
*
272-
* @param {string} path The URL path.
273-
* @param {EndpointSearchParams} searchParams The search parameters.
284+
* @param {string} path The path for the url. Example: "/example"
285+
* @param {string | Record<string, string> | URLSearchParams | JsonApiParams} searchParams Optional query parameters.
274286
* @returns {URL} The constructed URL.
287+
* @example
288+
* ```ts
289+
* const drupal = new DrupalClient("https://example.com")
290+
*
291+
* // https://drupal.org
292+
* drupal.buildUrl("https://drupal.org").toString()
293+
*
294+
* // https://example.com/foo
295+
* drupal.buildUrl("/foo").toString()
296+
*
297+
* // https://example.com/foo?bar=baz
298+
* client.buildUrl("/foo", { bar: "baz" }).toString()
299+
* ```
300+
*
301+
* Build a URL from `DrupalJsonApiParams`
302+
* ```ts
303+
* const params = {
304+
* getQueryObject: () => ({
305+
* sort: "-created",
306+
* "fields[node--article]": "title,path",
307+
* }),
308+
* }
309+
*
310+
* // https://example.com/jsonapi/node/article?sort=-created&fields%5Bnode--article%5D=title%2Cpath
311+
* drupal.buildUrl("/jsonapi/node/article", params).toString()
312+
* ```
275313
*/
276314
buildUrl(path: string, searchParams?: EndpointSearchParams): URL {
277315
const url = new URL(path, this.baseUrl)
@@ -407,10 +445,19 @@ export class NextDrupalBase {
407445
}
408446

409447
/**
410-
* Gets an access token using the provided client ID and secret.
448+
* Retrieve an access token.
411449
*
412450
* @param {NextDrupalAuthClientIdSecret} clientIdSecret The client ID and secret.
413451
* @returns {Promise<AccessToken>} The access token.
452+
* @remarks
453+
* If options is not provided, `DrupalClient` will use the `clientId` and `clientSecret` configured in `auth`.
454+
* @example
455+
* ```ts
456+
* const accessToken = await drupal.getAccessToken({
457+
* clientId: "7034f4db-7151-466f-a711-8384bddb9e60",
458+
* clientSecret: "d92Fm^ds",
459+
* })
460+
* ```
414461
*/
415462
async getAccessToken(
416463
clientIdSecret?: NextDrupalAuthClientIdSecret

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

Lines changed: 201 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)