UNPKG

@prismicio/client

Version:

The official JavaScript + TypeScript client library for Prismic

153 lines (152 loc) 4.47 kB
/** * The default delay used with APIs not providing rate limit headers. * * @internal */ export declare const UNKNOWN_RATE_LIMIT_DELAY = 1500; /** * A universal API to make network requests. A subset of the `fetch()` API. * * {@link https://developer.mozilla.org/en-US/docs/Web/API/fetch} */ export type FetchLike = (input: string, init?: RequestInitLike) => Promise<ResponseLike>; /** * An object that allows you to abort a `fetch()` request if needed via an * `AbortController` object * * {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal} */ export type AbortSignalLike = any; /** * A subset of RequestInit properties to configure a `fetch()` request. */ export interface RequestInitLike extends Pick<RequestInit, "cache"> { /** * The HTTP method to use for the request. */ method?: string; /** * The request body to send with the request. */ body?: any | FormData | string; /** * An object literal to set the `fetch()` request's headers. */ headers?: Record<string, string>; /** * An AbortSignal to set the `fetch()` request's signal. * * See: * [https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) */ signal?: AbortSignalLike; } /** * The minimum required properties from Response. */ export interface ResponseLike { ok: boolean; status: number; headers: HeadersLike; json(): Promise<any>; text(): Promise<string>; blob(): Promise<Blob>; } /** * The minimum required properties from Headers. */ export interface HeadersLike { get(name: string): string | null; } /** * The minimum required properties to treat as an HTTP Request for automatic * Prismic preview support. */ export type HttpRequestLike = /** * Web API Request * * @see http://developer.mozilla.org/en-US/docs/Web/API/Request */ { headers?: { get(name: string): string | null; }; url?: string; } /** * Express-style Request */ | { headers?: { cookie?: string; }; query?: Record<string, unknown>; }; /** * Configuration for clients that determine how APIs are queried. */ export type BaseClientConfig = { /** * The function used to make network requests to the Prismic REST API. In * environments where a global `fetch` function does not exist, such as * Node.js, this function must be provided. */ fetch?: FetchLike; /** * Options provided to the client's `fetch()` on all network requests. These * options will be merged with internally required options. They can also be * overriden on a per-query basis using the query's `fetchOptions` parameter. */ fetchOptions?: RequestInitLike; }; /** * Parameters for any client method that use `fetch()`. */ export type FetchParams = { /** * Options provided to the client's `fetch()` on all network requests. These * options will be merged with internally required options. They can also be * overriden on a per-query basis using the query's `fetchOptions` parameter. */ fetchOptions?: RequestInitLike; /** * An `AbortSignal` provided by an `AbortController`. This allows the network * request to be cancelled if necessary. * * @deprecated Move the `signal` parameter into `fetchOptions.signal`: * * @see \<https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal\> */ signal?: AbortSignalLike; }; /** * The result of a `fetch()` job. */ type FetchJobResult<TJSON = any> = { status: number; headers: HeadersLike; json: TJSON; text?: string; }; export declare abstract class BaseClient { /** * The function used to make network requests to the Prismic REST API. In * environments where a global `fetch` function does not exist, such as * Node.js, this function must be provided. */ fetchFn: FetchLike; fetchOptions?: RequestInitLike; /** * Active queued `fetch()` jobs keyed by URL and AbortSignal (if it exists). */ private queuedFetchJobs; /** * Active deduped `fetch()` jobs keyed by URL and AbortSignal (if it exists). */ private dedupedFetchJobs; constructor(options: BaseClientConfig); protected fetch(url: string, params?: FetchParams): Promise<FetchJobResult>; private queueFetch; private dedupeFetch; private createFetchJob; } export {};