Skip to content

Commit

Permalink
Merge pull request #21 from TariqSaiyad/bugfix/get-index-fix
Browse files Browse the repository at this point in the history
small update to fetching the correct locale endpoint
  • Loading branch information
TariqSaiyad committed Mar 22, 2024
2 parents a9544cb + 8b12ef3 commit faddca2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/thin-pandas-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"drupal-fetch": patch
---

Fix for fetching the correct endpoint
26 changes: 18 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,18 @@ export class DrupalFetch {
* @returns {Promise<T>} A Promise that resolves to the fetched resource.
*/
async getResource<T extends JsonApiResource>(type: string, uuid: string, options: JsonApiOptions = DEFAULT_FETCH_OPTIONS): Promise<T> {
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());
Expand All @@ -86,9 +91,14 @@ export class DrupalFetch {
* @returns {Promise<T>} A Promise that resolves to the fetched resource collection.
*/
async getResourceCollection<T = JsonApiResource[]>(type: string, options: JsonApiOptions = DEFAULT_FETCH_OPTIONS): Promise<T> {
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());
Expand Down Expand Up @@ -150,8 +160,8 @@ export class DrupalFetch {
* Retrieves the JSON:API index.
* @returns {Promise<JsonApiResponse|null>} A Promise that resolves to the JSON:API index, or null if the fetch operation fails.
*/
async getIndex(locale?: Locale): Promise<JsonApiResponse | null> {
const url = this.buildUrl(locale ? `/${locale}${this.apiPrefix}` : this.apiPrefix);
async getIndex(): Promise<JsonApiResponse | null> {
const url = this.buildUrl(this.apiPrefix);

try {
const response = await fetch(url.toString());
Expand Down Expand Up @@ -214,8 +224,8 @@ export class DrupalFetch {
* @param {Locale} [locale] - The locale of the resource.
* @returns {Promise<string>} A Promise that resolves to the endpoint URL.
*/
private async getResourceEndpoint(type: string, locale?: Locale): Promise<string> {
const index = await this.getIndex(locale);
async getResourceEndpoint(type: string, locale?: Locale): Promise<string> {
const index = await this.getIndex();

const link = index?.links?.[type] as { href: string };

Expand Down

0 comments on commit faddca2

Please sign in to comment.