create-request
Version:
A modern, chainable wrapper for fetch with automatic retries, timeouts, comprehensive error handling, and first-class TypeScript support
222 lines (221 loc) • 6.88 kB
TypeScript
import type { RequestInterceptor, ResponseInterceptor, ErrorInterceptor } from "../types.js";
/**
* Global configuration for create-request
*/
export declare class Config {
private static instance;
private csrfHeaderName;
private xsrfCookieName;
private xsrfHeaderName;
private csrfToken;
private enableAutoXsrf;
private enableAntiCsrf;
private requestInterceptors;
private responseInterceptors;
private errorInterceptors;
private nextInterceptorId;
private constructor();
/**
* Get the singleton instance of the Config class
*
* @returns The global configuration instance
*
* @example
* const config = Config.getInstance();
* config.setCsrfToken('token123');
*/
static getInstance(): Config;
/**
* Set a global CSRF token to be used for all requests
*
* @param token - The CSRF token value
* @returns The config instance for chaining
*
* @example
* Config.getInstance().setCsrfToken('myToken123');
*/
setCsrfToken(token: string): Config;
/**
* Get the global CSRF token that will be automatically applied to requests
*
* @returns The current CSRF token or null if not set
*/
getCsrfToken(): string | null;
/**
* Set the CSRF header name used when sending the token
*
* @param name - The header name to use
* @returns The config instance for chaining
*
* @example
* Config.getInstance().setCsrfHeaderName('X-My-CSRF-Token');
*/
setCsrfHeaderName(name: string): Config;
/**
* Get the configured CSRF header name
*
* @returns The current CSRF header name
*/
getCsrfHeaderName(): string;
/**
* Set the XSRF cookie name to look for when extracting tokens from cookies
*
* @param name - The cookie name to look for
* @returns The config instance for chaining
*
* @example
* Config.getInstance().setXsrfCookieName('MY-XSRF-COOKIE');
*/
setXsrfCookieName(name: string): Config;
/**
* Get the configured XSRF cookie name
*
* @returns The current XSRF cookie name
*/
getXsrfCookieName(): string;
/**
* Set the XSRF header name for sending tokens extracted from cookies
*
* @param name - The header name to use
* @returns The config instance for chaining
*/
setXsrfHeaderName(name: string): Config;
/**
* Get the configured XSRF header name
*
* @returns The current XSRF header name
*/
getXsrfHeaderName(): string;
/**
* Enable or disable automatic extraction of XSRF tokens from cookies
* When enabled, the library will look for XSRF tokens in cookies and
* automatically add them to request headers.
*
* @param enable - Whether to enable this feature
* @returns The config instance for chaining
*
* @example
* Config.getInstance().setEnableAutoXsrf(false); // Disable XSRF extraction
*/
setEnableAutoXsrf(enable: boolean): Config;
/**
* Check if automatic XSRF token extraction is enabled
*
* @returns True if automatic XSRF is enabled
*/
isAutoXsrfEnabled(): boolean;
/**
* Enable or disable automatic addition of anti-CSRF headers
* When enabled, X-Requested-With: XMLHttpRequest will be added to all requests.
*
* @param enable - Whether to enable this feature
* @returns The config instance for chaining
*/
setEnableAntiCsrf(enable: boolean): Config;
/**
* Check if anti-CSRF protection is enabled
*
* @returns True if anti-CSRF protection is enabled
*/
isAntiCsrfEnabled(): boolean;
/**
* Add a global request interceptor
* Request interceptors can modify the request configuration or return an early response
*
* @param interceptor - The request interceptor function
* @returns The interceptor ID for later removal
*
* @example
* const id = Config.getInstance().addRequestInterceptor((config) => {
* config.headers['X-Custom'] = 'value';
* return config;
* });
*/
addRequestInterceptor(interceptor: RequestInterceptor): number;
/**
* Add a global response interceptor
* Response interceptors can transform the response
*
* @param interceptor - The response interceptor function
* @returns The interceptor ID for later removal
*
* @example
* const id = Config.getInstance().addResponseInterceptor((response) => {
* console.log('Response received:', response.status);
* return response;
* });
*/
addResponseInterceptor(interceptor: ResponseInterceptor): number;
/**
* Add a global error interceptor
* Error interceptors can handle or transform errors
*
* @param interceptor - The error interceptor function
* @returns The interceptor ID for later removal
*
* @example
* const id = Config.getInstance().addErrorInterceptor((error) => {
* console.error('Request failed:', error);
* throw error;
* });
*/
addErrorInterceptor(interceptor: ErrorInterceptor): number;
/**
* Remove a request interceptor by its ID
*
* @param id - The interceptor ID returned from addRequestInterceptor
*
* @example
* Config.getInstance().removeRequestInterceptor(id);
*/
removeRequestInterceptor(id: number): void;
/**
* Remove a response interceptor by its ID
*
* @param id - The interceptor ID returned from addResponseInterceptor
*
* @example
* Config.getInstance().removeResponseInterceptor(id);
*/
removeResponseInterceptor(id: number): void;
/**
* Remove an error interceptor by its ID
*
* @param id - The interceptor ID returned from addErrorInterceptor
*
* @example
* Config.getInstance().removeErrorInterceptor(id);
*/
removeErrorInterceptor(id: number): void;
/**
* Clear all interceptors (request, response, and error)
*
* @example
* Config.getInstance().clearInterceptors();
*/
clearInterceptors(): void;
/**
* Get all global request interceptors (in registration order)
* @internal
*/
getRequestInterceptors(): RequestInterceptor[];
/**
* Get all global response interceptors (in registration order)
* @internal
*/
getResponseInterceptors(): ResponseInterceptor[];
/**
* Get all global error interceptors (in registration order)
* @internal
*/
getErrorInterceptors(): ErrorInterceptor[];
/**
* Reset all configuration options to their default values
*
* @returns The config instance for chaining
*
* @example
* Config.getInstance().reset();
*/
reset(): Config;
}