@gdwc/drupal-state
Version:
A simple data store to manage application state sourced from Drupal's JSON:API.
124 lines (121 loc) • 5.38 kB
text/typescript
import { ServerResponse } from 'http';
import { StoreApi, State, GetState, SetState, Subscribe, Destroy, PartialState } from 'zustand/vanilla';
import { DrupalJsonApiParams } from 'drupal-jsonapi-params';
import { TJsonApiBody } from 'jsona/lib/JsonaTypes';
import { fetchAdapter, DrupalStateConfig, GenericIndex, GetObjectByPathParams, GetObjectParams } from './types/types.cjs';
declare class DrupalState {
apiBase: string;
apiPrefix: string;
defaultLocale?: string;
apiRoot: string;
private clientId;
private clientSecret;
fetchAdapter?: fetchAdapter;
auth: boolean;
private token;
debug: boolean;
store: StoreApi<State>;
getState: GetState<State>;
setState: SetState<State>;
subscribe: Subscribe<State>;
destroy: Destroy;
private dataFormatter;
onError: (err: Error) => void;
noStore: boolean;
constructor({ apiBase, apiPrefix, defaultLocale, clientId, clientSecret, fetchAdapter, debug, onError, noStore, }: DrupalStateConfig);
/**
* Format apiBase, apiPrefix, and combine into apiRoot.
* @returns a fully qualified JSON:API root endpoint URL
*/
assembleApiRoot(): string;
/**
* Assembles a correctly formatted JSON:API endpoint URL.
* @param indexHref a JSON:API resource endpoint
* @param id id of an individual resource
* @param params user provided JSON:API parameter string or DrupalJsonApiParams object
* @returns a full endpoint URL
*/
assembleEndpoint(indexHref: string, id?: string, params?: string | DrupalJsonApiParams): string;
/**
* Assembles an authorization header using an existing token if valid, or by
* fetching a new token if necessary.
* @returns a string containing an authorization header value
*/
getAuthHeader(): Promise<string>;
/**
* Wraps {@link fetch/fetchApiIndex} function so it can be overridden.
*/
fetchApiIndex(apiRoot: string): Promise<void | GenericIndex>;
/**
*
* Wraps {@link fetch/fetchJsonapiEndpoint} function so it can be overridden.
*/
fetchJsonapiEndpoint(endpoint: string, requestInit: {} | undefined, onError: (err: Error) => void, res: ServerResponse | boolean): Promise<void | Response>;
/**
* Fetches data using our fetch method.
* @param endpoint the assembled JSON:API endpoint
* @param res response object
* @param anon make the request anonymously if true
* @returns data fetched from JSON:API endpoint
*/
fetchData(endpoint: string, res?: ServerResponse | boolean, anon?: boolean): Promise<TJsonApiBody | void>;
/**
* Get the contents of the root API from local state if it exists, or fetch
* it from Drupal if it doesn't exist in local state.
* @returns a promise containing an index of api links
*/
private getApiIndex;
/**
* Get an object by path alias from local state if it exists, or fetch it from Drupal
* if it doesn't exist in local state.
* @remarks The query option was experimental and is now deprecated
* @param options.objectName - Name of object to retrieve.
* @param options.path - Alias of a specific resource
* @param options.res - response object
* @param options.params - user provided JSON:API parameter string or DrupalJsonApiParams object
* @param options.refresh - a boolean value. If true, ignore local state.
* @param options.anon - a boolean value. If true, send the request without the authentication header if valid credentials exist.
* @returns a promise containing deserialized JSON:API data for the requested
* object
*
* @example
* ```
* await store.getObjectByPath({
* objectName: 'node--article',
* path: '/articles/my-article',
* res: ServerResponse,
* params: 'include=field_media_image',
* refresh: true,
* )}
* ```
*/
getObjectByPath({ objectName, path, res, params, refresh, anon, query, }: GetObjectByPathParams): Promise<PartialState<State> | void>;
/**
* Get an object from local state if it exists, or fetch it from Drupal if
* it doesn't exist in local state.
* @remarks The query option was experimental and is now deprecated
* @param options.objectName - Name of object to fetch
* @param options.id - id of a specific resource
* @param options.res - response object
* @param options.params - user provided JSON:API parameter string or DrupalJsonApiParams object
* @param options.all - a boolean value. If true, fetch all objects in a collection.
* @param options.refresh - a boolean value. If true, ignore local state.
* @param options.anon - a boolean value. If true, send the request without the authentication header if valid credentials exist.
* @returns a promise containing deserialized JSON:API data for the requested
* object
*
* @example
* * @example
* ```
* await store.getObject({
* objectName: 'node--article',
* id: 'some-article-uuid-here',
* res: ServerResponse,
* params: 'include=field_media_image',
* refresh: true,
* )}
* ```
*/
getObject({ objectName, id, res, params, all, refresh, anon, query, }: GetObjectParams): Promise<PartialState<State> | void>;
}
export { DrupalState as default };