yuumi-request
Version:
request queue for browser
60 lines (59 loc) • 2.38 kB
TypeScript
import { Queue } from "./queue/queue";
import { ResolveFun, RejectFun } from "./queue/job";
import { RequestMethod } from "./request/type";
import { XHR_RequestOption } from "./request/xhr";
export interface YuumiRequestInterface {
baseURI: string;
headers: Record<string, string>;
concurrency: number;
timeout: number;
}
export type YuumiRequestCtorOption = {
[K in keyof YuumiRequestInterface]?: YuumiRequestInterface[K] extends Function ? never : YuumiRequestInterface[K];
} & {
paramStringify?: (value: any) => string;
xhr?: <T>(option: XHR_RequestOption) => Promise<T>;
};
export type RequestOption = {
path: string;
method: RequestMethod;
async?: boolean;
headers?: Record<string, string>;
params?: Record<string, string | number>;
data?: Record<string, any>;
cancelToken?: (cancel?: () => void) => void;
timeout?: number;
enforce?: "pre" | "normal";
uploader?: {
[key: string]: EventListener;
};
};
export type MethodRequestOptions = {
[K in keyof RequestOption as Exclude<K, "path" | "method">]: RequestOption[K];
};
export declare class YuumiRequestInterceptor {
requestInterceptors: [ResolveFun, RejectFun?][];
responseInterceptors: [ResolveFun, RejectFun?][];
constructor();
request(resolve: ResolveFun, reject?: RejectFun): void;
response(resolve: ResolveFun, reject?: RejectFun): void;
}
export declare class YuumiRequest implements YuumiRequestInterface {
readonly baseURI: string;
readonly headers: Record<string, string>;
readonly concurrency: number;
readonly timeout: number;
readonly paramStringify?: (value: any) => string;
readonly xhr: <T>(option: XHR_RequestOption) => Promise<T>;
readonly interceptor: YuumiRequestInterceptor;
readonly queue: Queue;
constructor(option?: YuumiRequestCtorOption);
request<T>(option: RequestOption): Promise<T>;
get<T>(path: string, params?: {
[key: string]: number | string;
}, options?: MethodRequestOptions): Promise<T>;
post<T>(path: string, data?: any, options?: MethodRequestOptions): Promise<T>;
patch<T>(path: string, data?: any, options?: MethodRequestOptions): Promise<T>;
put<T>(path: string, data?: any, options?: MethodRequestOptions): Promise<T>;
delete<T>(path: string, data?: any, options?: MethodRequestOptions): Promise<T>;
}