axios-easyconf
Version:
基于axios的轻量级二次封装库,支持自动协议匹配、简化拦截器配置、自动挂载 WebSocket 地址。
41 lines (32 loc) • 1.28 kB
TypeScript
import { AxiosInstance, InternalAxiosRequestConfig, AxiosResponse, CreateAxiosDefaults } from 'axios';
// 请求拦截函数类型
export type beforeRequestFun = (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig>;
// 响应拦截函数类型
export type beforeResponseFun = (response: AxiosResponse) => AxiosResponse | Promise<AxiosResponse>;
// 拦截器的结构是一个函数数组:[成功处理, 失败处理]
export type beforeRequest = [
beforeRequestFun,
(error: any) => any
];
export type beforeResponse = [
beforeResponseFun,
(error: any) => any
];
// 创建实例时的扩展配置
export interface CreateAxiosEConfConfig extends CreateAxiosDefaults {
autoProtocol?: boolean;
beforeRequest?: beforeRequest;
beforeResponse?: beforeResponse;
}
// 扩展AxiosInstance以附加自定义字段
export interface ExtendedAxiosInstance extends AxiosInstance {
WS_URL?: string;
BASE_URL?: string;
}
export interface AxiosEConf {
create(config: CreateAxiosEConfConfig): ExtendedAxiosInstance;
}
export * from 'axios';
declare const AxiosEConf: AxiosEConf;
export default AxiosEConf;
export declare const createRequest: (config: CreateAxiosEConfConfig) => ExtendedAxiosInstance;