@joker.front/requester
Version:
Joker 接口请求库,可通过该类库实现与@joker.server/core的快速对接。
96 lines (95 loc) • 3.99 kB
TypeScript
export declare const ERROR_CODE_REQUEST_BREAK = "ERROR_CODE_REQUEST_BREAK";
export declare const ERROR_CODE_REQUEST_ABORT = "ERROR_CODE_REQUEST_ABORT";
export declare const ERROR_CODE_REQUEST_DEFAULT = "ERROR_CODE_REQUEST";
export declare const ERROR_CDODE_TIME_OUT = "ERROR_CDODE_TIME_OUT";
/** 请求处理程序 */
export declare class Requester<T = {}> {
option: RequesterOption;
/** 请求前置callback */
beforeCallbacks: {
add: (callback: (requestOption: RequestOption & T) => false | Promise<false> | Promise<void> | void) => () => void;
callbacks: ((requestOption: RequestOption & T) => false | Promise<false> | Promise<void> | void)[];
reset: () => void;
remove: (callBack: (requestOption: RequestOption & T) => false | Promise<false> | Promise<void> | void) => void;
};
/** 请求后置callback */
afterCallbacks: {
add: (callback: (requestOption: RequestOption & T, data: any | RequestError, response?: Response) => void) => () => void;
callbacks: ((requestOption: RequestOption & T, data: any | RequestError, response?: Response) => void)[];
reset: () => void;
remove: (callBack: (requestOption: RequestOption & T, data: any | RequestError, response?: Response) => void) => void;
};
/** 请求错误callback */
errorCallbacks: {
add: (callback: (error: RequestError<T>, response?: Response) => void) => () => void;
callbacks: ((error: RequestError<T>, response?: Response) => void)[];
reset: () => void;
remove: (callBack: (error: RequestError<T>, response?: Response) => void) => void;
};
constructor(option: RequesterOption);
/** 请求中队列 */
requestList: Array<RequestQueueItem>;
/** 请求接口 */
request<I = any, O = any>(url: string, option?: Partial<Omit<RequestOption<I>, "url"> & T>): Promise<O>;
cancelAllRequest(): void;
private execBeforeEvent;
private deleteCache;
private getCache;
private execError;
}
export type RequestQueueItem = {
cancel: Function;
option: RequestOption;
};
export type RequestError<T = any> = {
code: string;
message: string;
data?: any;
option: RequestOption & T;
e?: Error;
};
/**
* 请求处理程序配置
*/
export type RequesterOption = {
/** 请求地址根 */
base?: string;
/**
* 接口超时时间
* 当设置为false时,不做超时处理
* @default 10s
*/
timeout?: number | false;
/** 错误码-信息映射转译 */
errorCodeMessage?: Record<string, string>;
/** 自定义默认错误处理 */
defaultErrorFunc?: (err: RequestError, response?: Response) => void;
/** 自定义请求数据转换 */
transformReqData?: (data: any, option: RequestOption & Record<string, any>, requesteroption: RequesterOption) => any | Promise<any>;
/** 自定义服务端返回数据转换 */
transformRspData?: (data: any, option: RequestOption & Record<string, any>, requesteroption: RequesterOption) => any | Promise<any>;
/** 自定义解析rsp数据,并进行成功、失败分流 */
analyRspResult?: (data: any, success: (data: any) => void, error: (err: Omit<RequestError, "option">) => void, response: Response) => void;
mock?: (option: RequestOption & Record<string, any>) => Promise<any>;
};
export type RequestMethod = "GET" | "POST" | "DELETE" | "PUT";
export type RequestCacheOption = {
id: string;
expires?: number;
};
/**
* 请求参数配置
*/
export type RequestOption<T = any> = {
url: string;
method: RequestMethod;
data?: T;
rspType?: "json" | "stream";
timeout?: number | false;
cache?: RequestCacheOption | true;
forceRefreshCache?: boolean;
headers?: Record<string, any>;
error?: (err: RequestError, response?: Response) => void | false;
success?: (data: any, response?: Response) => void;
stream?: (chunk: string, allChunk: string, response?: Response) => void;
};