Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9dcc592
feat: update menu strucutre and mark areas in docs that need to be ex…
backlineint Oct 11, 2024
11a18fc
feat: add pages router example to client intro page
backlineint Nov 5, 2024
cfcd026
chore: add todo to apply patches doc
backlineint Nov 5, 2024
e3d39cb
fix: update apply patches documentation
backlineint Nov 5, 2024
46eeb8c
docs: adjustments to configuration entry
backlineint Nov 8, 2024
04f4295
docs: update additional serializer related options
backlineint Nov 8, 2024
06ebfb8
docs: edits to building pages entry - work in progress
backlineint Nov 8, 2024
0758e01
docs: add revalidation examples to building pages entry
backlineint Nov 8, 2024
5f184c0
docs: building pages updates
backlineint Nov 8, 2024
dbfb228
docs: updates to upgrade guide entry
backlineint Nov 18, 2024
6e2b9d8
Generate API documentation via TypeDoc
backlineint Nov 19, 2024
1d7ffe8
build: add todo
backlineint Nov 19, 2024
e9ce3be
build: lock yarn updates
backlineint Nov 19, 2024
93a0cdc
docs: re-order elements on homepage to prioritize text content
backlineint Jan 10, 2025
386ac0f
docs: update features code for app router
backlineint Jan 10, 2025
26a59eb
docs: first pass at 2.0 announcement post
backlineint Jan 10, 2025
7026d74
docs: minor revisions from review of draft PR
backlineint Jan 10, 2025
b814feb
feat: fixes to typedoc markdown build
backlineint Jan 13, 2025
1468bce
feat: include autogenerated API reference in docs site
backlineint Jan 13, 2025
f824090
Merge branch 'generate-api-docs' into feat/2x-docs-finalization
backlineint Jan 13, 2025
24bbf4e
feat: remove manually authored API reference
backlineint Jan 13, 2025
99d53e7
docs: remove todo on index page
backlineint Jan 16, 2025
d3b97a8
docs: update client example in readme
backlineint Jan 17, 2025
993872f
Merge branch 'main' into feat/2x-docs-finalization
backlineint Jan 17, 2025
c2498e7
docs: remove files generated by typedoc from version control
backlineint Jan 17, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ node_modules
*.esm.js
/drupal
/drupal-*
/local-next-drupal
4 changes: 2 additions & 2 deletions packages/next-drupal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ https://next-drupal.org
A page with all "Article" nodes.

```jsx
import { DrupalClient } from "next-drupal"
import { NextDrupal } from "next-drupal"

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

export default function BlogPage({ articles }) {
return (
Expand Down
100 changes: 99 additions & 1 deletion packages/next-drupal/src/next-drupal-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const DEFAULT_HEADERS = {
Accept: "application/json",
}

/**
* The base class for NextDrupal clients.
*/
export class NextDrupalBase {
accessToken?: NextDrupalBaseOptions["accessToken"]

Expand Down Expand Up @@ -173,6 +176,13 @@ export class NextDrupalBase {
return this._token
}

/**
* Fetches a resource from the given input URL or path.
*
* @param {RequestInfo} input The input URL or path.
* @param {FetchOptions} init The fetch options.
* @returns {Promise<Response>} The fetch response.
*/
async fetch(
input: RequestInfo,
{ withAuth, ...init }: FetchOptions = {}
Expand Down Expand Up @@ -215,6 +225,12 @@ export class NextDrupalBase {
return await fetch(input, init)
}

/**
* Gets the authorization header value based on the provided auth configuration.
*
* @param {NextDrupalAuth} auth The auth configuration.
* @returns {Promise<string>} The authorization header value.
*/
async getAuthorizationHeader(auth: NextDrupalAuth) {
let header: string

Expand Down Expand Up @@ -250,6 +266,13 @@ export class NextDrupalBase {
return header
}

/**
* Builds a URL with the given path and search parameters.
*
* @param {string} path The URL path.
* @param {EndpointSearchParams} searchParams The search parameters.
* @returns {URL} The constructed URL.
*/
buildUrl(path: string, searchParams?: EndpointSearchParams): URL {
const url = new URL(path, this.baseUrl)

Expand All @@ -269,7 +292,15 @@ export class NextDrupalBase {
return url
}

// async so subclasses can query for endpoint discovery.
/**
* Builds an endpoint URL with the given options.
*
* @param {Object} options The options for building the endpoint.
* @param {string} options.locale The locale.
* @param {string} options.path The path.
* @param {EndpointSearchParams} options.searchParams The search parameters.
* @returns {Promise<string>} The constructed endpoint URL.
*/
async buildEndpoint({
locale = "",
path = "",
Expand All @@ -291,6 +322,16 @@ export class NextDrupalBase {
).toString()
}

/**
* Constructs a path from the given segment and options.
*
* @param {string | string[]} segment The path segment.
* @param {Object} options The options for constructing the path.
* @param {Locale} options.locale The locale.
* @param {Locale} options.defaultLocale The default locale.
* @param {PathPrefix} options.pathPrefix The path prefix.
* @returns {string} The constructed path.
*/
constructPathFromSegment(
segment: string | string[],
options: {
Expand Down Expand Up @@ -338,6 +379,15 @@ export class NextDrupalBase {
})
}

/**
* Adds a locale prefix to the given path.
*
* @param {string} path The path.
* @param {Object} options The options for adding the locale prefix.
* @param {Locale} options.locale The locale.
* @param {Locale} options.defaultLocale The default locale.
* @returns {string} The path with the locale prefix.
*/
addLocalePrefix(
path: string,
options: { locale?: Locale; defaultLocale?: Locale } = {}
Expand All @@ -356,6 +406,12 @@ export class NextDrupalBase {
return `${localePrefix}${path}`
}

/**
* Gets an access token using the provided client ID and secret.
*
* @param {NextDrupalAuthClientIdSecret} clientIdSecret The client ID and secret.
* @returns {Promise<AccessToken>} The access token.
*/
async getAccessToken(
clientIdSecret?: NextDrupalAuthClientIdSecret
): Promise<AccessToken> {
Expand Down Expand Up @@ -435,6 +491,12 @@ export class NextDrupalBase {
return result
}

/**
* Validates the draft URL using the provided search parameters.
*
* @param {URLSearchParams} searchParams The search parameters.
* @returns {Promise<Response>} The validation response.
*/
async validateDraftUrl(searchParams: URLSearchParams): Promise<Response> {
const path = searchParams.get("path")

Expand Down Expand Up @@ -468,17 +530,35 @@ export class NextDrupalBase {
return response
}

/**
* Logs a debug message if debug mode is enabled.
*
* @param {string} message The debug message.
*/
debug(message) {
this.isDebugEnabled && this.logger.debug(message)
}

/**
* Throws an error if the response contains JSON:API errors.
*
* @param {Response} response The fetch response.
* @param {string} messagePrefix The error message prefix.
* @throws {JsonApiErrors} The JSON:API errors.
*/
async throwIfJsonErrors(response: Response, messagePrefix = "") {
if (!response?.ok) {
const errors = await this.getErrorsFromResponse(response)
throw new JsonApiErrors(errors, response.status, messagePrefix)
}
}

/**
* Extracts errors from the fetch response.
*
* @param {Response} response The fetch response.
* @returns {Promise<string | JsonApiResponse>} The extracted errors.
*/
async getErrorsFromResponse(response: Response) {
const type = response.headers.get("content-type")
let error: JsonApiResponse | { message: string }
Expand Down Expand Up @@ -506,6 +586,12 @@ export class NextDrupalBase {
}
}

/**
* Checks if the provided auth configuration is basic auth.
*
* @param {NextDrupalAuth} auth The auth configuration.
* @returns {boolean} True if the auth configuration is basic auth, false otherwise.
*/
export function isBasicAuth(
auth: NextDrupalAuth
): auth is NextDrupalAuthUsernamePassword {
Expand All @@ -515,6 +601,12 @@ export function isBasicAuth(
)
}

/**
* Checks if the provided auth configuration is access token auth.
*
* @param {NextDrupalAuth} auth The auth configuration.
* @returns {boolean} True if the auth configuration is access token auth, false otherwise.
*/
export function isAccessTokenAuth(
auth: NextDrupalAuth
): auth is NextDrupalAuthAccessToken {
Expand All @@ -524,6 +616,12 @@ export function isAccessTokenAuth(
)
}

/**
* Checks if the provided auth configuration is client ID and secret auth.
*
* @param {NextDrupalAuth} auth The auth configuration.
* @returns {boolean} True if the auth configuration is client ID and secret auth, false otherwise.
*/
export function isClientIdSecretAuth(
auth: NextDrupalAuth
): auth is NextDrupalAuthClientIdSecret {
Expand Down
Loading
Loading