@timwheeler/monkey-fetch
Version:
Monkey-patch library for the native fetch API
65 lines (64 loc) • 3.46 kB
TypeScript
interface IMonkeyFetchRequestConfiguration {
request?: (resource: RequestInfo | URL, options: RequestInit) => Promise<[(RequestInfo | URL), RequestInit]>;
requestError?: (error: Error) => Promise<any>;
}
interface IMonkeyFetchResponseConfiguration {
response?: (response: IMonkeyFetchResponse) => Promise<IMonkeyFetchResponse>;
responseError?: (response: IMonkeyFetchResponse) => Promise<any>;
}
interface IMonkeyFetchMetaConfiguration {
debug?: boolean;
}
interface IMonkeyFetchConfiguration extends IMonkeyFetchRequestConfiguration, IMonkeyFetchResponseConfiguration, IMonkeyFetchMetaConfiguration {
}
interface IMonkeyFetchResponse extends Response {
request: Request;
}
export declare class MonkeyFetch {
debug: boolean;
private readonly interceptors;
constructor();
/**
* @description Determine the current JS runtime context, either Browser or Node.js
*/
private getCurrentRuntimeContext;
/**
* @description Applies the default or user-supplied `request` and/or the `requestError` interceptors
* @param {[(RequestInfo | URL), RequestInit]} args - the request arguments used to create the monkey-patched request
* @returns {Promise<IMonkeyFetchResponse>} - the Promise containing the request arguments after interceptors have been applied
*/
protected applyRequestInterceptors(args: [(RequestInfo | URL), RequestInit]): Promise<[(RequestInfo | URL), RequestInit]>;
/**
* @description Returns the monkey-patched promise that was either resolved or rejected
* @param {Function} fetch - the Fetch API for the current JavaScript runtime context
* @param {[(RequestInfo | URL), RequestInit]} args - the request arguments used to create the monkey-patched request
* @returns {Promise<IMonkeyFetchResponse>} - the Promise containing the response with interceptors applied
*/
protected sendInterceptedRequest(fetch: Function, args: [(RequestInfo | URL), RequestInit]): Promise<IMonkeyFetchResponse>;
/**
* @description Applies the default or user-supplied `response` and `responseError` interceptors
* @param {IMonkeyFetchResponse} initialResponse - the initial, unaltered response before interceptors are applied
* @returns {Promise<IMonkeyFetchResponse>} - the response with interceptors applied
*/
protected applyResponseInterceptors(initialResponse: IMonkeyFetchResponse): Promise<IMonkeyFetchResponse>;
/**
* @description Applies all interceptors to the request and response objects
* @param {Function} fetch - the Fetch API for the current JavaScript runtime context
* @param {[(RequestInfo | URL), RequestInit]} args - the request arguments used to create the monkey-patched request
* @returns {Promise<IMonkeyFetchResponse>} - the response with all interceptors applied
*/
protected applyInterceptors(fetch: Function, ...args: [(RequestInfo | URL), RequestInit]): Promise<any>;
/**
* Debugging tool for internal logging
* @param {any} args - Arbitrary arguments to be emitted via logger
* @returns {void}
*/
private debugLog;
/**
* @description Custom, user-supplied configuration that is applied to `MonkeyFetch`
* @param {IMonkeyFetchConfiguration} configuration - Custom configuration applied to all requests and responses
* @returns {void}
*/
configure(configuration: IMonkeyFetchConfiguration): void;
}
export {};