From 8b12ef38b13b305e06d6d0f4d045e956a3448040 Mon Sep 17 00:00:00 2001 From: Tariq Saiyad Date: Fri, 22 Mar 2024 16:00:07 +1300 Subject: [PATCH] small update to fetching the correct locale endpoint --- .changeset/thin-pandas-draw.md | 5 +++++ src/index.ts | 26 ++++++++++++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 .changeset/thin-pandas-draw.md diff --git a/.changeset/thin-pandas-draw.md b/.changeset/thin-pandas-draw.md new file mode 100644 index 0000000..483c121 --- /dev/null +++ b/.changeset/thin-pandas-draw.md @@ -0,0 +1,5 @@ +--- +"drupal-fetch": patch +--- + +Fix for fetching the correct endpoint diff --git a/src/index.ts b/src/index.ts index a8dc4fe..4cb5a69 100644 --- a/src/index.ts +++ b/src/index.ts @@ -59,13 +59,18 @@ export class DrupalFetch { * @returns {Promise} A Promise that resolves to the fetched resource. */ async getResource(type: string, uuid: string, options: JsonApiOptions = DEFAULT_FETCH_OPTIONS): Promise { - const apiPath = await this.getResourceEndpoint(type, options.locale); + const apiPath = await this.getResourceEndpoint(type, options?.locale); + const apiURL = new URL(apiPath); if (options.params && options.version) { options.params.addCustomParam({ resourceVersion: options.version }); } - const url = this.buildUrl(`${apiPath}/${uuid}`, options?.params); + if (options?.locale) { + apiURL.pathname = `${options.locale}/${apiURL.pathname}`; + } + + const url = this.buildUrl(`${apiURL.toString()}/${uuid}`, options?.params); this._debug(`Fetching resource ${type} with id ${uuid}.`); this._debug(url.toString()); @@ -86,9 +91,14 @@ export class DrupalFetch { * @returns {Promise} A Promise that resolves to the fetched resource collection. */ async getResourceCollection(type: string, options: JsonApiOptions = DEFAULT_FETCH_OPTIONS): Promise { - const apiPath = await this.getResourceEndpoint(type, options.locale); + const apiPath = await this.getResourceEndpoint(type, options?.locale); + const apiURL = new URL(apiPath); + + if (options?.locale) { + apiURL.pathname = `${options.locale}/${apiURL.pathname}`; + } - const url = this.buildUrl(apiPath, options?.params); + const url = this.buildUrl(apiURL.toString(), options?.params); this._debug(`Fetching resource collection of type ${type}`); this._debug(url.toString()); @@ -150,8 +160,8 @@ export class DrupalFetch { * Retrieves the JSON:API index. * @returns {Promise} A Promise that resolves to the JSON:API index, or null if the fetch operation fails. */ - async getIndex(locale?: Locale): Promise { - const url = this.buildUrl(locale ? `/${locale}${this.apiPrefix}` : this.apiPrefix); + async getIndex(): Promise { + const url = this.buildUrl(this.apiPrefix); try { const response = await fetch(url.toString()); @@ -214,8 +224,8 @@ export class DrupalFetch { * @param {Locale} [locale] - The locale of the resource. * @returns {Promise} A Promise that resolves to the endpoint URL. */ - private async getResourceEndpoint(type: string, locale?: Locale): Promise { - const index = await this.getIndex(locale); + async getResourceEndpoint(type: string, locale?: Locale): Promise { + const index = await this.getIndex(); const link = index?.links?.[type] as { href: string };