import { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'; // request 方法 opts 参数的接口 export interface IRequestOptions extends AxiosRequestConfig { requestInterceptors?: IRequestInterceptorTuple[]; responseInterceptors?: IResponseInterceptorTuple[]; [key: string]: any; } export interface IRequestOptionsWithResponse extends IRequestOptions { getResponse: true; } export interface IRequestOptionsWithoutResponse extends IRequestOptions { getResponse: false; } export interface IRequest { (url: string, opts: IRequestOptionsWithResponse): Promise>; (url: string, opts: IRequestOptionsWithoutResponse): Promise; (url: string, opts?: IRequestOptions): Promise; // getResponse 默认是 false, 因此不提供该参数时,只返回 data,不提供 opts 时,默认使用 'GET' method,并且默认返回 data } export type RequestError = AxiosError | Error; export interface IErrorHandler { (error: RequestError, opts: IRequestOptions): void; } export type WithPromise = T | Promise; export type IRequestInterceptorAxios = (config: IRequestOptions) => WithPromise; export type IRequestInterceptorRequest = ( url: string, config: IRequestOptions ) => WithPromise<{ url: string; options: IRequestOptions }>; export type IRequestInterceptor = IRequestInterceptorAxios | IRequestInterceptorRequest; export type IErrorInterceptor = (error: Error) => Promise; export type IResponseInterceptor = ( response: AxiosResponse ) => WithPromise>; export type IRequestInterceptorTuple = | [IRequestInterceptor, IErrorInterceptor] | [IRequestInterceptor] | IRequestInterceptor; export type IResponseInterceptorTuple = | [IResponseInterceptor, IErrorInterceptor] | [IResponseInterceptor] | IResponseInterceptor; export interface RequestConfig extends IRequestOptions { skipErrorHandler?: boolean; encryption?: { enabled: boolean; key: string; }; errorConfig?: { errorHandler?: IErrorHandler; errorThrower?: (res: T) => void; }; }