Skip to content

Commit

Permalink
feat(next-drupal)!: make Subrequests module optional
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
The subrequests Drupal module is not optional and next-drupal will now only
perform JSON:API subrequests when the useSubrequests flag is enabled.

Fixes #744
  • Loading branch information
JohnAlbin committed Apr 30, 2024
1 parent f56c243 commit d326d48
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
26 changes: 23 additions & 3 deletions packages/next-drupal/src/next-drupal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export class NextDrupal extends NextDrupalBase {

useDefaultEndpoints: boolean

useSubrequests: boolean

/**
* Instantiates a new NextDrupal.
*
Expand All @@ -70,6 +72,7 @@ export class NextDrupal extends NextDrupalBase {
headers = DEFAULT_HEADERS,
throwJsonApiErrors = true,
useDefaultEndpoints = true,
useSubrequests = false,
} = options

this.apiPrefix = apiPrefix
Expand All @@ -78,6 +81,7 @@ export class NextDrupal extends NextDrupalBase {
this.headers = headers
this.throwJsonApiErrors = !!throwJsonApiErrors
this.useDefaultEndpoints = !!useDefaultEndpoints
this.useSubrequests = useSubrequests

// Do not throw errors in production.
if (process.env.NODE_ENV === "production") {
Expand Down Expand Up @@ -331,10 +335,26 @@ export class NextDrupal extends NextDrupalBase {
params.resourceVersion = resourceVersion
}

options.params = params

if (!this.useSubrequests) {
const translatedPath = await this.translatePath(path, options)

// If the path was 404 Not Found, return null.
if (translatedPath === null) {
return null
}

const uuid = translatedPath.entity.uuid
const type = translatedPath.jsonapi.resourceName

return this.getResource<T>(uuid, type, options)
}

const resourceParams = stringify(params)

// We are intentionally not using translatePath here.
// We want a single request using subrequests.
// Instead of using translatePath and getResource, we create a single
// JSON:API request using subrequests.
const payload = [
{
requestId: "router",
Expand Down Expand Up @@ -570,7 +590,7 @@ export class NextDrupal extends NextDrupalBase {
return null
}

await this.throwIfJsonErrors(response)
await this.throwIfJsonErrors(response, "Error while fetching path: ")

return await response.json()
}
Expand Down
10 changes: 10 additions & 0 deletions packages/next-drupal/src/types/next-drupal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ export type NextDrupalOptions = NextDrupalBaseOptions & {
* [Documentation](https://next-drupal.org/docs/client/configuration#usedefaultendpoints)
*/
useDefaultEndpoints?: boolean

/**
* Optionally use the subrequests Drupal module to perform 2 or more JSON:API requests in one fetch operation.
*
* * **Default value**: `false`
* * **Required**: *No*
*
* [Documentation](https://next-drupal.org/docs/client/configuration#usesubrequests)
*/
useSubrequests?: boolean
}

export type JsonDeserializer = (
Expand Down

0 comments on commit d326d48

Please sign in to comment.