@iel/axios-ext
Version:
Extend extra functionality for axios.
82 lines (81 loc) • 2.96 kB
TypeScript
import { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import { AxiosExtInstance } from './AxiosExt';
import { EventStoreType } from './helper';
export declare type AxiosExtPlugin<T = any> = (axiosExt: AxiosExtInstance, options: T) => void;
declare type HookCommonParams = {
/**
* 事件仓储
*/
$eventStore: EventStoreType;
/**
* 请求配置项
*/
config: AxiosRequestConfig;
/**
* 响应最终的返回值
*/
returnValue: any;
/**
* 设置响应返回值,若调用 `resolve` 后将固定返回值不再被更新
*/
setReturnValue: (value: any) => void;
/**
* 确定最终的返回值,仅在首次调用时生效
*/
resolve: (value: any) => void;
/**
* 抛出错误,仅在首次调用时生效
*/
reject: (error: any) => void;
};
declare type AxiosRequestFnType = AxiosInstance['request'];
export declare type AxiosExtPluginOnRequestHook = (params: {
/**
* 请求方法
*/
requestFn: AxiosRequestFnType;
/**
* 更改请求方法
*/
setRequestFn: (fn: any) => void;
} & HookCommonParams) => void;
export declare type AxiosExtPluginOnResponseHook = (params: {
/**
* `axios` 响应结果
*/
response: AxiosResponse;
} & HookCommonParams) => void;
export declare type AxiosExtPluginOnResponseErrorHook = (params: {
/**
* `axios` 响应错误
*/
error: AxiosError;
} & HookCommonParams) => void;
export declare type AxiosExtPluginOnFinallyHook = (params: {
/**
* 接口请求是否错误,仅区分正常响应和错误响应
*/
isError: boolean;
} & Pick<HookCommonParams, '$eventStore' | 'config' | 'returnValue'>) => void;
export declare type AxiosExtPluginOnDestroyHook = () => void;
export declare type AxiosExtPluginHooks = {
onRequest: AxiosExtPluginOnRequestHook[];
onResponse: AxiosExtPluginOnResponseHook[];
onResponseError: AxiosExtPluginOnResponseErrorHook[];
onFinally: AxiosExtPluginOnFinallyHook[];
onDestroy: AxiosExtPluginOnDestroyHook[];
};
export declare type AxiosExtPluginManagerInstance = AxiosExtPluginManager;
export default class AxiosExtPluginManager {
plugins: Map<AxiosExtPlugin, AxiosExtPluginHooks>;
constructor();
has(plugin: AxiosExtPlugin): boolean;
set(plugin: AxiosExtPlugin): Map<AxiosExtPlugin<any>, AxiosExtPluginHooks>;
get(plugin: AxiosExtPlugin): AxiosExtPluginHooks | undefined;
delete(plugin: AxiosExtPlugin): boolean;
clear(): void;
appendHookFn(plugin: AxiosExtPlugin, name: keyof AxiosExtPluginHooks, callback: any): false | undefined;
getHooks(name: keyof AxiosExtPluginHooks): AxiosExtPluginOnRequestHook[] | AxiosExtPluginOnResponseHook[] | AxiosExtPluginOnResponseErrorHook[] | AxiosExtPluginOnFinallyHook[] | AxiosExtPluginOnDestroyHook[];
runHooks(name: keyof AxiosExtPluginHooks, ...args: any[]): void;
}
export {};