Skip to content

Commit

Permalink
Merge pull request #16 from TariqSaiyad/bugfix/get-index-fix
Browse files Browse the repository at this point in the history
refactor resource endpoints
  • Loading branch information
TariqSaiyad committed Jul 10, 2023
2 parents 374b4a1 + 646175a commit e627e0c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-jars-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"drupal-fetch": patch
---

Manually build resource endpoint. This bypasses the need to query drupal for it
48 changes: 14 additions & 34 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@

import { DrupalJsonApiParams } from "drupal-jsonapi-params";
import { Jsona } from "jsona";
import { TJsonApiBody } from "jsona/lib/JsonaTypes";
import { stringify } from "qs";
import {
JsonApiOptions,
DrupalFetchOptions,
JsonApiResource,
DrupalPathData,
JsonApiResponse,
DrupalMenuItem,
DrupalPathData,
DrupalView,
Locale,
JsonApiOptions,
JsonApiResource,
Locale
} from "./types";
import { TJsonApiBody } from "jsona/lib/JsonaTypes";

const DEFAULT_API_PREFIX = "/jsonapi";
const DEFAULT_FETCH_OPTIONS: JsonApiOptions = {};
Expand Down Expand Up @@ -58,8 +57,9 @@ export class DrupalFetch {
* @param {JsonApiOptions} - Options for the fetch operation.
* @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);
async getResource<T extends JsonApiResource>(type: string, uuid: string, options: JsonApiOptions = DEFAULT_FETCH_OPTIONS): Promise<T | null> {
const apiPath = this.getResourceEndpoint(type, options.locale);


if (options.params && options.version) {
options.params.addCustomParam({ resourceVersion: options.version });
Expand All @@ -86,7 +86,7 @@ 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 = this.getResourceEndpoint(type, options.locale);

const url = this.buildUrl(apiPath, options?.params);

Expand Down Expand Up @@ -146,22 +146,6 @@ export class DrupalFetch {
return await response.json();
}

/**
* 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(): Promise<JsonApiResponse | null> {
const url = this.buildUrl(this.apiPrefix);

try {
const response = await fetch(url.toString());
return await response.json();
} catch (error: any) {
new Error(`Failed to fetch JSON:API index at ${url.toString()} - ${error.message}`);
}
return null;
}

/**
* Retrieves the menu items for the specified menu.
* @param {string} name - The name of the menu.
Expand Down Expand Up @@ -214,14 +198,9 @@ 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();

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

if (!link) console.error(`Resource of type '${type}' and locale ${locale} not found.`);

return link?.href;
private getResourceEndpoint(type: string, locale?: Locale): string {
const path = type.replace("--", "/");
return locale ? `${this.baseUrl}/${locale}${this.apiPrefix}/${path}` : `${this.baseUrl}${this.apiPrefix}/${path}`;
}

/**
Expand Down Expand Up @@ -255,7 +234,8 @@ export class DrupalFetch {

if (params) url.search = stringify(params.getQueryObject());

url.protocol='https'
url.protocol = 'https'

return url;
}

Expand Down

0 comments on commit e627e0c

Please sign in to comment.