UNPKG

fexjs

Version:

A lightweight and powerful Fetch API wrapper with interceptors, cancel tokens, and timeout support.

61 lines (60 loc) 2.14 kB
interface FexError<T = any> extends Error { isFexError: boolean; response?: FexResponse<T>; request?: Response; config: FetchConfig; cause?: any; } type FexErrorHandler<T = any> = (error: FexError<T>) => unknown; type XOR<T, U> = (T | U) extends object ? (T extends U ? never : T) | (U extends T ? never : U) : T | U; type FetchConfig = Omit<RequestInit, "headers" | "signal"> & { headers: Record<string, string>; baseURL?: string; url?: string; timeout?: number; cancelToken?: FexCancelToken; } & XOR<{ mode?: RequestMode; }, { withCredentials?: boolean; }>; interface FexResponse<T = any> { data: T; status: number; statusText: string; headers: Headers; config: FetchConfig; request: Response; } declare class FexCancelToken { private controller; promise: Promise<void>; reason?: string; constructor(); cancel: (message?: string) => void; get signal(): AbortSignal; } declare class FexInstance { private defaultConfig; interceptors: { request: { use: (onFulfilled: (config: FetchConfig) => FetchConfig, onRejected?: (error: FexError) => unknown) => void; }; response: { use: (onFulfilled: <T>(response: FexResponse<T>) => FexResponse<T> | Promise<FexResponse<T>>, onRejected?: FexErrorHandler) => void; }; }; private requestInterceptors; private responseInterceptors; constructor(config?: Partial<FetchConfig>); private request; get<T>(url: string, config?: Partial<FetchConfig>): Promise<FexResponse<T>>; post<T>(url: string, data?: unknown, config?: Partial<FetchConfig>): Promise<FexResponse<T>>; put<T>(url: string, data?: unknown, config?: Partial<FetchConfig>): Promise<FexResponse<T>>; delete<T>(url: string, config?: Partial<FetchConfig>): Promise<FexResponse<T>>; options<T>(url: string, config?: Partial<FetchConfig>): Promise<FexResponse<T>>; create(config?: Partial<FetchConfig>): FexInstance; } declare const fex: FexInstance; export default fex; export { FexCancelToken, FetchConfig, FexResponse, FexError, FexInstance };