From d326d48b8005c03268e374505a18a42b6c897c71 Mon Sep 17 00:00:00 2001 From: JohnAlbin Date: Wed, 24 Apr 2024 17:18:02 +0800 Subject: [PATCH] feat(next-drupal)!: make Subrequests module optional 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 --- packages/next-drupal/src/next-drupal.ts | 26 ++++++++++++++++--- packages/next-drupal/src/types/next-drupal.ts | 10 +++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/packages/next-drupal/src/next-drupal.ts b/packages/next-drupal/src/next-drupal.ts index 564e25eb..385ce988 100644 --- a/packages/next-drupal/src/next-drupal.ts +++ b/packages/next-drupal/src/next-drupal.ts @@ -52,6 +52,8 @@ export class NextDrupal extends NextDrupalBase { useDefaultEndpoints: boolean + useSubrequests: boolean + /** * Instantiates a new NextDrupal. * @@ -70,6 +72,7 @@ export class NextDrupal extends NextDrupalBase { headers = DEFAULT_HEADERS, throwJsonApiErrors = true, useDefaultEndpoints = true, + useSubrequests = false, } = options this.apiPrefix = apiPrefix @@ -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") { @@ -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(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", @@ -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() } diff --git a/packages/next-drupal/src/types/next-drupal.ts b/packages/next-drupal/src/types/next-drupal.ts index 37407e10..da7defd0 100644 --- a/packages/next-drupal/src/types/next-drupal.ts +++ b/packages/next-drupal/src/types/next-drupal.ts @@ -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 = (