Skip to content

Commit 6e2b9d8

Browse files
committed
Generate API documentation via TypeDoc
Fixes #796 --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/chapter-three/next-drupal/issues/796?shareId=XXXX-XXXX-XXXX-XXXX).
1 parent 36387ff commit 6e2b9d8

File tree

5 files changed

+326
-7
lines changed

5 files changed

+326
-7
lines changed

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

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ const DEFAULT_HEADERS = {
3030
Accept: "application/json",
3131
}
3232

33+
/**
34+
* The base class for NextDrupal clients.
35+
*/
3336
export class NextDrupalBase {
3437
accessToken?: NextDrupalBaseOptions["accessToken"]
3538

@@ -173,6 +176,13 @@ export class NextDrupalBase {
173176
return this._token
174177
}
175178

179+
/**
180+
* Fetches a resource from the given input URL or path.
181+
*
182+
* @param {RequestInfo} input The input URL or path.
183+
* @param {FetchOptions} init The fetch options.
184+
* @returns {Promise<Response>} The fetch response.
185+
*/
176186
async fetch(
177187
input: RequestInfo,
178188
{ withAuth, ...init }: FetchOptions = {}
@@ -215,6 +225,12 @@ export class NextDrupalBase {
215225
return await fetch(input, init)
216226
}
217227

228+
/**
229+
* Gets the authorization header value based on the provided auth configuration.
230+
*
231+
* @param {NextDrupalAuth} auth The auth configuration.
232+
* @returns {Promise<string>} The authorization header value.
233+
*/
218234
async getAuthorizationHeader(auth: NextDrupalAuth) {
219235
let header: string
220236

@@ -250,6 +266,13 @@ export class NextDrupalBase {
250266
return header
251267
}
252268

269+
/**
270+
* Builds a URL with the given path and search parameters.
271+
*
272+
* @param {string} path The URL path.
273+
* @param {EndpointSearchParams} searchParams The search parameters.
274+
* @returns {URL} The constructed URL.
275+
*/
253276
buildUrl(path: string, searchParams?: EndpointSearchParams): URL {
254277
const url = new URL(path, this.baseUrl)
255278

@@ -269,7 +292,15 @@ export class NextDrupalBase {
269292
return url
270293
}
271294

272-
// async so subclasses can query for endpoint discovery.
295+
/**
296+
* Builds an endpoint URL with the given options.
297+
*
298+
* @param {Object} options The options for building the endpoint.
299+
* @param {string} options.locale The locale.
300+
* @param {string} options.path The path.
301+
* @param {EndpointSearchParams} options.searchParams The search parameters.
302+
* @returns {Promise<string>} The constructed endpoint URL.
303+
*/
273304
async buildEndpoint({
274305
locale = "",
275306
path = "",
@@ -291,6 +322,16 @@ export class NextDrupalBase {
291322
).toString()
292323
}
293324

325+
/**
326+
* Constructs a path from the given segment and options.
327+
*
328+
* @param {string | string[]} segment The path segment.
329+
* @param {Object} options The options for constructing the path.
330+
* @param {Locale} options.locale The locale.
331+
* @param {Locale} options.defaultLocale The default locale.
332+
* @param {PathPrefix} options.pathPrefix The path prefix.
333+
* @returns {string} The constructed path.
334+
*/
294335
constructPathFromSegment(
295336
segment: string | string[],
296337
options: {
@@ -338,6 +379,15 @@ export class NextDrupalBase {
338379
})
339380
}
340381

382+
/**
383+
* Adds a locale prefix to the given path.
384+
*
385+
* @param {string} path The path.
386+
* @param {Object} options The options for adding the locale prefix.
387+
* @param {Locale} options.locale The locale.
388+
* @param {Locale} options.defaultLocale The default locale.
389+
* @returns {string} The path with the locale prefix.
390+
*/
341391
addLocalePrefix(
342392
path: string,
343393
options: { locale?: Locale; defaultLocale?: Locale } = {}
@@ -356,6 +406,12 @@ export class NextDrupalBase {
356406
return `${localePrefix}${path}`
357407
}
358408

409+
/**
410+
* Gets an access token using the provided client ID and secret.
411+
*
412+
* @param {NextDrupalAuthClientIdSecret} clientIdSecret The client ID and secret.
413+
* @returns {Promise<AccessToken>} The access token.
414+
*/
359415
async getAccessToken(
360416
clientIdSecret?: NextDrupalAuthClientIdSecret
361417
): Promise<AccessToken> {
@@ -435,6 +491,12 @@ export class NextDrupalBase {
435491
return result
436492
}
437493

494+
/**
495+
* Validates the draft URL using the provided search parameters.
496+
*
497+
* @param {URLSearchParams} searchParams The search parameters.
498+
* @returns {Promise<Response>} The validation response.
499+
*/
438500
async validateDraftUrl(searchParams: URLSearchParams): Promise<Response> {
439501
const path = searchParams.get("path")
440502

@@ -468,17 +530,35 @@ export class NextDrupalBase {
468530
return response
469531
}
470532

533+
/**
534+
* Logs a debug message if debug mode is enabled.
535+
*
536+
* @param {string} message The debug message.
537+
*/
471538
debug(message) {
472539
this.isDebugEnabled && this.logger.debug(message)
473540
}
474541

542+
/**
543+
* Throws an error if the response contains JSON:API errors.
544+
*
545+
* @param {Response} response The fetch response.
546+
* @param {string} messagePrefix The error message prefix.
547+
* @throws {JsonApiErrors} The JSON:API errors.
548+
*/
475549
async throwIfJsonErrors(response: Response, messagePrefix = "") {
476550
if (!response?.ok) {
477551
const errors = await this.getErrorsFromResponse(response)
478552
throw new JsonApiErrors(errors, response.status, messagePrefix)
479553
}
480554
}
481555

556+
/**
557+
* Extracts errors from the fetch response.
558+
*
559+
* @param {Response} response The fetch response.
560+
* @returns {Promise<string | JsonApiResponse>} The extracted errors.
561+
*/
482562
async getErrorsFromResponse(response: Response) {
483563
const type = response.headers.get("content-type")
484564
let error: JsonApiResponse | { message: string }
@@ -506,6 +586,12 @@ export class NextDrupalBase {
506586
}
507587
}
508588

589+
/**
590+
* Checks if the provided auth configuration is basic auth.
591+
*
592+
* @param {NextDrupalAuth} auth The auth configuration.
593+
* @returns {boolean} True if the auth configuration is basic auth, false otherwise.
594+
*/
509595
export function isBasicAuth(
510596
auth: NextDrupalAuth
511597
): auth is NextDrupalAuthUsernamePassword {
@@ -515,6 +601,12 @@ export function isBasicAuth(
515601
)
516602
}
517603

604+
/**
605+
* Checks if the provided auth configuration is access token auth.
606+
*
607+
* @param {NextDrupalAuth} auth The auth configuration.
608+
* @returns {boolean} True if the auth configuration is access token auth, false otherwise.
609+
*/
518610
export function isAccessTokenAuth(
519611
auth: NextDrupalAuth
520612
): auth is NextDrupalAuthAccessToken {
@@ -524,6 +616,12 @@ export function isAccessTokenAuth(
524616
)
525617
}
526618

619+
/**
620+
* Checks if the provided auth configuration is client ID and secret auth.
621+
*
622+
* @param {NextDrupalAuth} auth The auth configuration.
623+
* @returns {boolean} True if the auth configuration is client ID and secret auth, false otherwise.
624+
*/
527625
export function isClientIdSecretAuth(
528626
auth: NextDrupalAuth
529627
): auth is NextDrupalAuthClientIdSecret {

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

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ import type {
2727
NextApiResponse,
2828
} from "next"
2929

30+
/**
31+
* The NextDrupalPages class extends the NextDrupal class and provides methods
32+
* for interacting with a Drupal backend in the context of Next.js pages.
33+
*/
3034
export class NextDrupalPages extends NextDrupal {
3135
private serializer: DrupalClientOptions["serializer"]
3236

@@ -59,6 +63,13 @@ export class NextDrupalPages extends NextDrupal {
5963
) => this.serializer.deserialize(body, options)
6064
}
6165

66+
/**
67+
* Gets the entry point for a given resource type.
68+
*
69+
* @param {string} resourceType The resource type.
70+
* @param {Locale} locale The locale.
71+
* @returns {Promise<string>} The entry point URL.
72+
*/
6273
async getEntryForResourceType(
6374
resourceType: string,
6475
locale?: Locale
@@ -74,6 +85,14 @@ export class NextDrupalPages extends NextDrupal {
7485
return new DrupalMenuTree<DrupalMenuItem>(links, parent)
7586
}
7687

88+
/**
89+
* Gets a resource from the context.
90+
*
91+
* @param {string | DrupalTranslatedPath} input The input path or translated path.
92+
* @param {GetStaticPropsContext} context The static props context.
93+
* @param {Object} options Options for the request.
94+
* @returns {Promise<T>} The fetched resource.
95+
*/
7796
async getResourceFromContext<T extends JsonApiResource>(
7897
input: string | DrupalTranslatedPath,
7998
context: GetStaticPropsContext,
@@ -157,6 +176,14 @@ export class NextDrupalPages extends NextDrupal {
157176
return resource
158177
}
159178

179+
/**
180+
* Gets a collection of resources from the context.
181+
*
182+
* @param {string} type The type of the resources.
183+
* @param {GetStaticPropsContext} context The static props context.
184+
* @param {Object} options Options for the request.
185+
* @returns {Promise<T>} The fetched collection of resources.
186+
*/
160187
async getResourceCollectionFromContext<T = JsonApiResource[]>(
161188
type: string,
162189
context: GetStaticPropsContext,
@@ -177,6 +204,14 @@ export class NextDrupalPages extends NextDrupal {
177204
})
178205
}
179206

207+
/**
208+
* Gets a search index from the context.
209+
*
210+
* @param {string} name The name of the search index.
211+
* @param {GetStaticPropsContext} context The static props context.
212+
* @param {Object} options Options for the request.
213+
* @returns {Promise<T>} The fetched search index.
214+
*/
180215
async getSearchIndexFromContext<T = JsonApiResource[]>(
181216
name: string,
182217
context: GetStaticPropsContext,
@@ -189,6 +224,13 @@ export class NextDrupalPages extends NextDrupal {
189224
})
190225
}
191226

227+
/**
228+
* Translates a path from the context.
229+
*
230+
* @param {GetStaticPropsContext} context The static props context.
231+
* @param {Object} options Options for the request.
232+
* @returns {Promise<DrupalTranslatedPath | null>} The translated path.
233+
*/
192234
async translatePathFromContext(
193235
context: GetStaticPropsContext,
194236
options?: {
@@ -208,6 +250,13 @@ export class NextDrupalPages extends NextDrupal {
208250
})
209251
}
210252

253+
/**
254+
* Gets the path from the context.
255+
*
256+
* @param {GetStaticPropsContext} context The static props context.
257+
* @param {Object} options Options for the request.
258+
* @returns {string} The constructed path.
259+
*/
211260
getPathFromContext(
212261
context: GetStaticPropsContext,
213262
options?: {
@@ -223,6 +272,14 @@ export class NextDrupalPages extends NextDrupal {
223272

224273
getPathsFromContext = this.getStaticPathsFromContext
225274

275+
/**
276+
* Gets static paths from the context.
277+
*
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.
282+
*/
226283
async getStaticPathsFromContext(
227284
types: string | string[],
228285
context: GetStaticPathsContext,
@@ -291,6 +348,13 @@ export class NextDrupalPages extends NextDrupal {
291348
return paths.flat()
292349
}
293350

351+
/**
352+
* Builds static paths from resources.
353+
*
354+
* @param {Object[]} resources The resources.
355+
* @param {Object} options Options for the request.
356+
* @returns {Object[]} The built static paths.
357+
*/
294358
buildStaticPathsFromResources(
295359
resources: {
296360
path: DrupalPathAlias
@@ -313,6 +377,13 @@ export class NextDrupalPages extends NextDrupal {
313377
: []
314378
}
315379

380+
/**
381+
* Builds static paths parameters from paths.
382+
*
383+
* @param {string[]} paths The paths.
384+
* @param {Object} options Options for the request.
385+
* @returns {Object[]} The built static paths parameters.
386+
*/
316387
buildStaticPathsParamsFromPaths(
317388
paths: string[],
318389
options?: { pathPrefix?: PathPrefix; locale?: Locale }
@@ -342,6 +413,13 @@ export class NextDrupalPages extends NextDrupal {
342413
})
343414
}
344415

416+
/**
417+
* Handles preview mode.
418+
*
419+
* @param {NextApiRequest} request The API request.
420+
* @param {NextApiResponse} response The API response.
421+
* @param {Object} options Options for the request.
422+
*/
345423
async preview(
346424
request: NextApiRequest,
347425
response: NextApiResponse,
@@ -411,6 +489,12 @@ export class NextDrupalPages extends NextDrupal {
411489
}
412490
}
413491

492+
/**
493+
* Disables preview mode.
494+
*
495+
* @param {NextApiRequest} request The API request.
496+
* @param {NextApiResponse} response The API response.
497+
*/
414498
async previewDisable(request: NextApiRequest, response: NextApiResponse) {
415499
// Disable both preview and draft modes.
416500
response.clearPreviewData()
@@ -427,6 +511,13 @@ export class NextDrupalPages extends NextDrupal {
427511
response.end()
428512
}
429513

514+
/**
515+
* Gets the authentication configuration from the context and options.
516+
*
517+
* @param {GetStaticPropsContext} context The static props context.
518+
* @param {JsonApiWithAuthOption} options Options for the request.
519+
* @returns {NextDrupalAuth} The authentication configuration.
520+
*/
430521
getAuthFromContextAndOptions(
431522
context: GetStaticPropsContext,
432523
options: JsonApiWithAuthOption

0 commit comments

Comments
 (0)