UNPKG

hook-fetch

Version:

A lightweight and modern HTTP request library developed based on the native Fetch API of the browser, providing a user-friendly interface similar to Axios and powerful extensibility.

111 lines (109 loc) 6.09 kB
import { BaseOptions, BaseRequestOptions, DeleteOptions, GetOptions, HeadOptions, HookFetchPlugin, OptionsOptions, PatchOptions, PostOptions, PutOptions, RequestOptions, ResponseError, StreamContext } from "./types-CdktDLPc.js"; import QueryString from "qs"; import { AnyObject, Generic } from "typescript-api-pro"; //#region src/utils.d.ts declare class HookFetchRequest<T = unknown, E = unknown> implements PromiseLike<T> { #private; constructor(options: BaseRequestOptions<unknown, unknown, E>); then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null | undefined): Promise<TResult1 | TResult2>; catch<TResult = never>(onrejected?: ((reason: unknown) => TResult | PromiseLike<TResult>) | null | undefined): Promise<T | TResult>; finally(onfinally?: (() => void) | null | undefined): void; abort(): void; json(): Promise<T>; blob(): Promise<Blob>; text(): Promise<string>; arrayBuffer(): Promise<ArrayBuffer>; formData(): Promise<FormData>; bytes(): Promise<Uint8Array<ArrayBufferLike>>; stream<T>(): AsyncGenerator<StreamContext<T> | StreamContext<null>, ResponseError<unknown> | undefined, unknown>; retry(): HookFetchRequest<unknown, E>; get response(): Promise<Response>; } //#endregion //#region src/base.d.ts type GenericWithNull<T, R extends AnyObject | null, K extends keyof R> = R extends null ? T : Generic<Exclude<R, null>, K, T>; declare class HookFetch<R extends AnyObject | null = null, K extends keyof R = never, E = AnyObject> { #private; constructor({ timeout, baseURL, headers, plugins, withCredentials }: BaseOptions); use(plugin: HookFetchPlugin<any, any, any, any>): this; request<T = AnyObject, P = AnyObject, D = AnyObject>(url: string, { timeout, headers, method, params, data, qsArrayFormat, withCredentials, extra }?: RequestOptions<P, D, E>): HookFetchRequest<GenericWithNull<T, R, K>, E>; get<T = AnyObject, P = AnyObject>(url: string, params?: P, options?: GetOptions<P, E>): HookFetchRequest<GenericWithNull<T, R, K>, E>; head<T = AnyObject, P = AnyObject>(url: string, params?: P, options?: HeadOptions<P, E>): HookFetchRequest<GenericWithNull<T, R, K>, E>; options<T = AnyObject, P = AnyObject, D = AnyObject>(url: string, params?: P, options?: OptionsOptions<P, D, E>): HookFetchRequest<GenericWithNull<T, R, K>, E>; delete<T = AnyObject, D = AnyObject, P = AnyObject>(url: string, options?: DeleteOptions<P, D, E>): HookFetchRequest<GenericWithNull<T, R, K>, E>; post<T = AnyObject, D = AnyObject, P = AnyObject>(url: string, data?: D, options?: PostOptions<D, P, E>): HookFetchRequest<GenericWithNull<T, R, K>, E>; upload<T = AnyObject, D = AnyObject, P = AnyObject>(url: string, data?: D, options?: PostOptions<D, P, E>): HookFetchRequest<GenericWithNull<T, R, K>, E>; put<T = AnyObject, D = AnyObject, P = AnyObject>(url: string, data?: D, options?: PutOptions<D, P, E>): HookFetchRequest<GenericWithNull<T, R, K>, E>; patch<T = AnyObject, D = AnyObject, P = AnyObject>(url: string, data?: D, options?: PatchOptions<D, P, E>): HookFetchRequest<GenericWithNull<T, R, K>, E>; abortAll(): void; } declare function useRequest<R = AnyObject, P = AnyObject, D = AnyObject, E = AnyObject>(url: string, options?: RequestOptions<P, D, E>): HookFetchRequest<R, E>; declare const request: typeof useRequest; declare function get<R = AnyObject, P = AnyObject, E = AnyObject>(url: string, params?: P, options?: GetOptions<P, E>): HookFetchRequest<R, E>; declare function head<R = AnyObject, P = AnyObject, E = AnyObject>(url: string, params?: P, options?: HeadOptions<P, E>): HookFetchRequest<R, E>; declare function options<R = AnyObject, P = AnyObject, D = AnyObject, E = AnyObject>(url: string, params?: P, options?: OptionsOptions<P, D, E>): HookFetchRequest<R, E>; declare function del<R = AnyObject, D = AnyObject, P = AnyObject, E = AnyObject>(url: string, options?: DeleteOptions<P, D, E>): HookFetchRequest<R, E>; declare function post<R = AnyObject, D = AnyObject, P = AnyObject, E = AnyObject>(url: string, data?: D, options?: PostOptions<D, P, E>): HookFetchRequest<R, E>; declare function upload<R = AnyObject, D = AnyObject, P = AnyObject, E = AnyObject>(url: string, data?: D, options?: PostOptions<D, P, E>): HookFetchRequest<R, E>; declare function put<R = AnyObject, D = AnyObject, P = AnyObject, E = AnyObject>(url: string, data?: D, options?: PutOptions<D, P, E>): HookFetchRequest<R, E>; declare function patch<R = AnyObject, D = AnyObject, P = AnyObject, E = AnyObject>(url: string, data?: D, options?: PatchOptions<D, P, E>): HookFetchRequest<R, E>; type ExportDefault = typeof useRequest & { create: <R extends AnyObject | null = null, K extends keyof R = never, E = AnyObject>(options: BaseOptions) => (HookFetch<R, K, E>['request'] & HookFetch<R, K, E>); get: typeof get; head: typeof head; options: typeof options; delete: typeof del; post: typeof post; put: typeof put; patch: typeof patch; upload: typeof upload; }; declare const hookFetch: ExportDefault; //#endregion //#region src/enum.d.ts declare enum ContentType { JSON = "application/json", FORM_URLENCODED = "application/x-www-form-urlencoded", FORM_DATA = "multipart/form-data", TEXT = "text/plain", HTML = "text/html", XML = "text/xml", CSV = "text/csv", STREAM = "application/octet-stream", } declare enum StatusCode { TIME_OUT = 408, ABORTED = 499, NETWORK_ERROR = 599, BODY_NULL = 502, UNKNOWN = 601, } declare enum Method { GET = "GET", POST = "POST", PUT = "PUT", DELETE = "DELETE", PATCH = "PATCH", HEAD = "HEAD", OPTIONS = "OPTIONS", } //#endregion //#region src/index.d.ts type HookFetchRequest$1<T = unknown, E = unknown> = HookFetchRequest<T, E>; //#endregion export { ContentType, HookFetchRequest$1 as HookFetchRequest, HookFetchRequest as HookFetchRequest$1, Method, StatusCode, del, get, head, hookFetch, options, patch, post, put, request, upload };