UNPKG

advanced-http-client

Version:

Universal HTTP client library using fetch for JS/TS projects (React, Next.js, Vue, Node.js, Bun, etc.)

138 lines 5.32 kB
export interface ExtendedRequestInit extends RequestInit { isolated?: boolean; includeHeaders?: string[]; /** * Request timeout in milliseconds. If the request does not complete within this time it will be aborted. */ timeout?: number; /** * Unique key that can later be used to cancel this request via cancelRequest. */ controlKey?: string; } export interface HttpClientError extends Error { response: HttpClientResponse; } export interface HttpClientResponse<T = unknown> { data: T; status: number; statusText: string; headers: Record<string, string>; config: { url: string; options?: RequestInit; method: string; body?: unknown; }; request: Response; } export interface HttpRequestOptions extends Omit<RequestInit, "headers"> { /** * Headers as a plain object. This is always a Record<string, string> in this implementation. */ headers: Record<string, string>; /** * If true, this request will ignore all global, instance, and default settings, using only the provided options. */ isolated?: boolean; /** * If set, and isolated is true, these header names will be included from global/instance headers if present. */ includeHeaders?: string[]; /** * Request timeout in milliseconds. If the request does not complete within this time it will be aborted. */ timeout?: number; /** * Unique key that can later be used to cancel this request via cancelRequest. */ controlKey?: string; } export interface HttpClientConfig extends Omit<RequestInit, "headers"> { baseURL?: string; headers?: Record<string, string>; timeout?: number; /** * Default cancellation key applied to every request made by this instance (optional) */ controlKey?: string; } export interface RequestInterceptor { (_config: HttpRequestOptions): HttpRequestOptions | Promise<HttpRequestOptions>; } export interface ResponseInterceptor<T = unknown> { (_response: HttpClientResponse<T>): HttpClientResponse<T> | Promise<HttpClientResponse<T>>; } export interface ErrorInterceptor { (_error: HttpClientError): HttpClientError | Promise<HttpClientError> | Promise<never>; } interface InterceptorHandler<T> { _fulfilled?: T; _rejected?: T; } export interface InterceptorManager<T> { use(_fulfilled?: T, _rejected?: T): number; eject(_id: number): void; clear(): void; handlers: InterceptorHandler<T>[]; } export declare class HttpClient { private static globalHeaders; private static globalControllers; private static allInstances; private readonly baseURL?; private readonly instanceHeaders; private readonly instanceOptions; interceptors: { request: InterceptorManager<RequestInterceptor>; response: InterceptorManager<ResponseInterceptor>; error: InterceptorManager<ErrorInterceptor>; }; private controllers; constructor(config?: HttpClientConfig); /** * Set a global header for all requests (e.g., for authorization). */ static setHeader(key: string, value: string): void; /** * Generate a random 20-character alphanumeric string suitable for use as a controlKey. */ static generateControlKey(): string; /** * Create a new HttpClient instance with custom default configuration. * @param config - Default configuration for this instance (e.g., baseURL, headers, credentials, etc.) * @returns A new HttpClient instance with the provided defaults. * * Example: * ```js * const api = HttpClient.create({ * baseURL: 'https://api.example.com', * headers: { Authorization: 'Bearer token' } * }); * api.get('/users'); // GET https://api.example.com/users * ``` */ static create(config?: HttpClientConfig): HttpClient; private static parseResponseBody; private mergeConfig; private convertHeadersToObject; private buildURL; private executeRequestInterceptors; private executeResponseInterceptors; private executeErrorInterceptors; request<T = unknown>(url: string, options?: RequestInit): Promise<HttpClientResponse<T>>; get<T = unknown>(url: string, options?: RequestInit): Promise<HttpClientResponse<T>>; private requestWithBody; post<T = unknown>(url: string, body?: unknown, options?: RequestInit): Promise<HttpClientResponse<T>>; patch<T = unknown>(url: string, body?: unknown, options?: RequestInit): Promise<HttpClientResponse<T>>; delete<T = unknown>(url: string, body?: unknown, options?: RequestInit): Promise<HttpClientResponse<T>>; private _abortAllControllers; static get<T = unknown>(url: string, options?: RequestInit): Promise<HttpClientResponse<T>>; static post<T = unknown>(url: string, body?: unknown, options?: RequestInit): Promise<HttpClientResponse<T>>; static patch<T = unknown>(url: string, body?: unknown, options?: RequestInit): Promise<HttpClientResponse<T>>; static delete<T = unknown>(url: string, body?: unknown, options?: RequestInit): Promise<HttpClientResponse<T>>; static cancelRequest(controlKey: string): void; static cancelAllRequests(): void; } export default HttpClient; //# sourceMappingURL=index.d.ts.map