Skip to content

Commit 74c8dc7

Browse files
authored
feat: Finalize 2.x Documentation (#804)
1 parent f8561b1 commit 74c8dc7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+996
-1979
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ node_modules
55
*.esm.js
66
/drupal
77
/drupal-*
8+
/local-next-drupal

packages/next-drupal/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ https://next-drupal.org
2121
A page with all "Article" nodes.
2222

2323
```jsx
24-
import { DrupalClient } from "next-drupal"
24+
import { NextDrupal } from "next-drupal"
2525

26-
const drupal = new DrupalClient("https://cms.next-drupal.org")
26+
const drupal = new NextDrupal("https://cms.next-drupal.org")
2727

2828
export default function BlogPage({ articles }) {
2929
return (

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 {

0 commit comments

Comments
 (0)