2023-03-28 21:32:54 +08:00
|
|
|
import { buildURL } from '../helpers/buildURL';
|
2023-03-23 20:09:00 +08:00
|
|
|
import { mergeConfig } from './mergeConfig';
|
|
|
|
import {
|
|
|
|
AxiosAdapter,
|
2023-03-28 20:35:40 +08:00
|
|
|
AxiosAdapterRequestMethod,
|
2023-03-23 20:09:00 +08:00
|
|
|
AxiosAdapterTask,
|
2023-03-28 20:35:40 +08:00
|
|
|
AxiosAdapterResponse,
|
|
|
|
AxiosAdapterRequestConfig,
|
2023-04-05 13:31:48 +08:00
|
|
|
AxiosAdapterResponseError,
|
2023-03-24 20:15:51 +08:00
|
|
|
} from '../adapter';
|
2023-03-23 20:09:00 +08:00
|
|
|
import { CancelToken } from './cancel';
|
|
|
|
import dispatchRequest from './dispatchRequest';
|
|
|
|
import InterceptorManager from './InterceptorManager';
|
|
|
|
import { AxiosTransformer } from './transformData';
|
|
|
|
|
2023-03-28 20:35:40 +08:00
|
|
|
export type AxiosRequestMethod =
|
|
|
|
| AxiosAdapterRequestMethod
|
2023-03-23 20:09:00 +08:00
|
|
|
| 'options'
|
|
|
|
| 'get'
|
|
|
|
| 'head'
|
|
|
|
| 'post'
|
|
|
|
| 'put'
|
|
|
|
| 'delete'
|
|
|
|
| 'trace'
|
|
|
|
| 'connect';
|
|
|
|
|
2023-03-28 20:35:40 +08:00
|
|
|
export interface AxiosRequestHeaders extends AnyObject {
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 通用请求头
|
|
|
|
*/
|
2023-04-05 09:15:05 +08:00
|
|
|
common?: AnyObject;
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* options 请求头
|
|
|
|
*/
|
2023-03-28 20:35:40 +08:00
|
|
|
options?: AnyObject;
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* get 请求头
|
|
|
|
*/
|
2023-03-28 20:35:40 +08:00
|
|
|
get?: AnyObject;
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* head 请求头
|
|
|
|
*/
|
2023-03-28 20:35:40 +08:00
|
|
|
head?: AnyObject;
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* post 请求头
|
|
|
|
*/
|
2023-03-28 20:35:40 +08:00
|
|
|
post?: AnyObject;
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* put 请求头
|
|
|
|
*/
|
2023-03-28 20:35:40 +08:00
|
|
|
put?: AnyObject;
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* delete 请求头
|
|
|
|
*/
|
2023-03-28 20:35:40 +08:00
|
|
|
delete?: AnyObject;
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* trace 请求头
|
|
|
|
*/
|
2023-03-28 20:35:40 +08:00
|
|
|
trace?: AnyObject;
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* connect 请求头
|
|
|
|
*/
|
2023-03-28 20:35:40 +08:00
|
|
|
connect?: AnyObject;
|
|
|
|
}
|
2023-03-23 20:09:00 +08:00
|
|
|
|
2023-03-28 20:35:40 +08:00
|
|
|
export interface AxiosRequestFormData extends AnyObject {
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 文件名
|
|
|
|
*/
|
|
|
|
name: string;
|
|
|
|
/**
|
|
|
|
* 文件路径
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
filePath: string;
|
|
|
|
}
|
|
|
|
|
2023-04-06 15:16:12 +08:00
|
|
|
export type AxiosRequestData = AnyObject | AxiosRequestFormData;
|
|
|
|
|
2023-03-23 20:09:00 +08:00
|
|
|
export interface AxiosProgressEvent {
|
|
|
|
progress: number;
|
|
|
|
totalBytesSent: number;
|
|
|
|
totalBytesExpectedToSend: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AxiosProgressCallback {
|
|
|
|
(event: AxiosProgressEvent): void;
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:35:40 +08:00
|
|
|
export interface AxiosRequestConfig
|
|
|
|
extends Omit<
|
|
|
|
Partial<AxiosAdapterRequestConfig>,
|
|
|
|
'type' | 'method' | 'success' | 'fail'
|
|
|
|
> {
|
|
|
|
/**
|
|
|
|
* 请求适配器
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
adapter?: AxiosAdapter;
|
2023-03-28 20:35:40 +08:00
|
|
|
/**
|
|
|
|
* 基础路径
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
baseURL?: string;
|
2023-03-28 20:35:40 +08:00
|
|
|
/**
|
|
|
|
* 请求参数
|
|
|
|
*/
|
|
|
|
params?: AnyObject;
|
|
|
|
/**
|
|
|
|
* 请求数据
|
|
|
|
*/
|
2023-04-06 15:16:12 +08:00
|
|
|
data?: AxiosRequestData;
|
2023-03-28 20:35:40 +08:00
|
|
|
/**
|
|
|
|
* 请求头
|
|
|
|
*/
|
|
|
|
headers?: AxiosRequestHeaders;
|
|
|
|
/**
|
|
|
|
* 请求方法
|
|
|
|
*/
|
|
|
|
method?: AxiosRequestMethod;
|
|
|
|
/**
|
|
|
|
* 取消令牌
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
cancelToken?: CancelToken;
|
2023-03-28 20:35:40 +08:00
|
|
|
/**
|
|
|
|
* 上传
|
|
|
|
*/
|
|
|
|
upload?: boolean;
|
|
|
|
/**
|
|
|
|
* 下载
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
download?: boolean;
|
2023-03-28 20:35:40 +08:00
|
|
|
/**
|
|
|
|
* 转换请求数据
|
|
|
|
*/
|
|
|
|
transformRequest?: AxiosTransformer | AxiosTransformer[];
|
|
|
|
/**
|
|
|
|
* 转换响应数据
|
|
|
|
*/
|
|
|
|
transformResponse?: AxiosTransformer | AxiosTransformer[];
|
|
|
|
/**
|
|
|
|
* 异常梳处理
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
errorHandler?: (error: unknown) => Promise<unknown>;
|
2023-03-28 20:35:40 +08:00
|
|
|
/**
|
|
|
|
* 监听上传进度
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
onUploadProgress?: AxiosProgressCallback;
|
2023-03-28 20:35:40 +08:00
|
|
|
/**
|
|
|
|
* 监听下载进度
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
onDownloadProgress?: AxiosProgressCallback;
|
2023-03-28 20:35:40 +08:00
|
|
|
/**
|
|
|
|
* 请求参数系列化函数
|
|
|
|
*/
|
|
|
|
paramsSerializer?: (params?: AnyObject) => string;
|
|
|
|
/**
|
|
|
|
* 校验状态码
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
validateStatus?: (status: number) => boolean;
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:35:40 +08:00
|
|
|
export interface AxiosResponse<TData = unknown>
|
|
|
|
extends AxiosAdapterResponse<TData> {
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 请求配置
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
config?: AxiosRequestConfig;
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 请求任务
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
request?: AxiosAdapterTask;
|
|
|
|
}
|
|
|
|
|
2023-03-28 20:35:40 +08:00
|
|
|
export interface AxiosResponseError extends AxiosAdapterResponseError {
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 原生接口 fail 回调产生的响应错误
|
|
|
|
*/
|
2023-04-05 13:31:48 +08:00
|
|
|
isFail: true;
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 请求配置
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
config?: AxiosRequestConfig;
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 请求任务
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
request?: AxiosAdapterTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AxiosConstructor {
|
|
|
|
new (config: AxiosRequestConfig): Axios;
|
|
|
|
}
|
|
|
|
|
2023-04-06 15:16:12 +08:00
|
|
|
export interface AxiosAliasMethod {
|
|
|
|
<TData = unknown>(
|
|
|
|
/**
|
|
|
|
* 请求地址
|
|
|
|
*/
|
|
|
|
url: string,
|
|
|
|
/**
|
|
|
|
* 请求配置
|
|
|
|
*/
|
|
|
|
config?: AxiosRequestConfig,
|
|
|
|
): Promise<AxiosResponse<TData>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AxiosWithParamsAliasMethod {
|
|
|
|
<TData = unknown>(
|
|
|
|
/**
|
|
|
|
* 请求地址
|
|
|
|
*/
|
|
|
|
url: string,
|
|
|
|
/**
|
|
|
|
* 请求参数
|
|
|
|
*/
|
|
|
|
params?: AnyObject,
|
|
|
|
/**
|
|
|
|
* 请求配置
|
|
|
|
*/
|
|
|
|
config?: AxiosRequestConfig,
|
|
|
|
): Promise<AxiosResponse<TData>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AxiosWithDataAliasMethod {
|
|
|
|
<TData = unknown>(
|
|
|
|
/**
|
|
|
|
* 请求地址
|
|
|
|
*/
|
|
|
|
url: string,
|
|
|
|
/**
|
|
|
|
* 请求数据
|
|
|
|
*/
|
|
|
|
data?: AnyObject,
|
|
|
|
/**
|
|
|
|
* 请求配置
|
|
|
|
*/
|
|
|
|
config?: AxiosRequestConfig,
|
|
|
|
): Promise<AxiosResponse<TData>>;
|
|
|
|
}
|
|
|
|
|
2023-03-23 20:09:00 +08:00
|
|
|
export default class Axios {
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 普通请求别名
|
|
|
|
*/
|
2023-04-06 16:00:04 +08:00
|
|
|
static as = ['options', 'trace', 'connect'] as const;
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 带请求参数的请求别名
|
|
|
|
*/
|
2023-04-06 16:00:04 +08:00
|
|
|
static pas = ['head', 'get', 'delete'] as const;
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 带请求数据的请求别名
|
|
|
|
*/
|
2023-04-06 16:00:04 +08:00
|
|
|
static das = ['post', 'put'] as const;
|
2023-04-02 23:27:45 +08:00
|
|
|
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 默认请求配置
|
|
|
|
*/
|
2023-04-06 16:00:04 +08:00
|
|
|
defaults: AxiosRequestConfig;
|
2023-03-23 20:09:00 +08:00
|
|
|
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 拦截器
|
|
|
|
*/
|
2023-04-06 16:00:04 +08:00
|
|
|
interceptors = {
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 请求拦截器
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
request: new InterceptorManager<AxiosRequestConfig>(),
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 响应拦截器
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
response: new InterceptorManager<AxiosResponse>(),
|
|
|
|
};
|
|
|
|
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 发送 options 请求
|
|
|
|
*/
|
2023-04-06 16:00:04 +08:00
|
|
|
options!: AxiosAliasMethod;
|
2023-04-02 23:27:45 +08:00
|
|
|
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 发送 get 请求
|
|
|
|
*/
|
2023-04-06 16:00:04 +08:00
|
|
|
get!: AxiosWithParamsAliasMethod;
|
2023-04-02 23:27:45 +08:00
|
|
|
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 发送 head 请求
|
|
|
|
*/
|
2023-04-06 16:00:04 +08:00
|
|
|
head!: AxiosWithParamsAliasMethod;
|
2023-04-02 23:27:45 +08:00
|
|
|
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 发送 post 请求
|
|
|
|
*/
|
2023-04-06 16:00:04 +08:00
|
|
|
post!: AxiosWithDataAliasMethod;
|
2023-04-02 23:27:45 +08:00
|
|
|
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 发送 put 请求
|
|
|
|
*/
|
2023-04-06 16:00:04 +08:00
|
|
|
put!: AxiosWithDataAliasMethod;
|
2023-04-02 23:27:45 +08:00
|
|
|
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 发送 delete 请求
|
|
|
|
*/
|
2023-04-06 16:00:04 +08:00
|
|
|
delete!: AxiosWithParamsAliasMethod;
|
2023-04-02 23:27:45 +08:00
|
|
|
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 发送 trace 请求
|
|
|
|
*/
|
2023-04-06 16:00:04 +08:00
|
|
|
trace!: AxiosAliasMethod;
|
2023-04-02 23:27:45 +08:00
|
|
|
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 发送 connect 请求
|
|
|
|
*/
|
2023-04-06 16:00:04 +08:00
|
|
|
connect!: AxiosAliasMethod;
|
2023-04-02 23:27:45 +08:00
|
|
|
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 实例化
|
|
|
|
*
|
|
|
|
* @param defaults 默认配置
|
|
|
|
*/
|
2023-04-06 16:00:04 +08:00
|
|
|
constructor(defaults: AxiosRequestConfig = {}) {
|
2023-03-23 20:09:00 +08:00
|
|
|
this.defaults = defaults;
|
2023-04-02 23:27:45 +08:00
|
|
|
|
|
|
|
for (const alias of Axios.as) {
|
2023-04-03 21:03:33 +08:00
|
|
|
this[alias] = (url, config = {}) => {
|
|
|
|
return this.request({
|
|
|
|
...config,
|
|
|
|
method: alias,
|
|
|
|
url,
|
|
|
|
});
|
2023-04-02 23:27:45 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const alias of Axios.pas) {
|
2023-04-03 21:03:33 +08:00
|
|
|
this[alias] = (url, params, config = {}) => {
|
|
|
|
return this.request({
|
|
|
|
...config,
|
|
|
|
method: alias,
|
|
|
|
params,
|
|
|
|
url,
|
|
|
|
});
|
2023-04-02 23:27:45 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const alias of Axios.das) {
|
2023-04-06 15:16:12 +08:00
|
|
|
this[alias] = (url, data, config = {}) => {
|
2023-04-03 21:03:33 +08:00
|
|
|
return this.request({
|
|
|
|
...config,
|
|
|
|
method: alias,
|
|
|
|
data,
|
|
|
|
url,
|
|
|
|
});
|
2023-04-02 23:27:45 +08:00
|
|
|
};
|
|
|
|
}
|
2023-03-23 20:09:00 +08:00
|
|
|
}
|
|
|
|
|
2023-04-06 16:00:04 +08:00
|
|
|
getUri(config: AxiosRequestConfig): string {
|
2023-03-23 20:09:00 +08:00
|
|
|
const { url, params, paramsSerializer } = mergeConfig(
|
|
|
|
this.defaults,
|
|
|
|
config,
|
|
|
|
);
|
|
|
|
|
|
|
|
return buildURL(url, params, paramsSerializer).replace(/^\?/, '');
|
|
|
|
}
|
|
|
|
|
2023-04-06 15:16:12 +08:00
|
|
|
/**
|
|
|
|
* 发送请求
|
|
|
|
*
|
|
|
|
* @param config 请求配置
|
|
|
|
*/
|
2023-04-06 16:00:04 +08:00
|
|
|
request<TData = unknown>(
|
2023-03-23 20:09:00 +08:00
|
|
|
config: AxiosRequestConfig,
|
|
|
|
): Promise<AxiosResponse<TData>> {
|
|
|
|
const requestConfig = mergeConfig(this.defaults, config);
|
2023-04-05 08:40:00 +08:00
|
|
|
const { request, response } = this.interceptors;
|
2023-03-23 20:09:00 +08:00
|
|
|
|
|
|
|
let promiseRequest = Promise.resolve(requestConfig);
|
2023-04-05 08:40:00 +08:00
|
|
|
request.forEach(({ resolved, rejected }) => {
|
2023-03-23 20:09:00 +08:00
|
|
|
promiseRequest = promiseRequest.then(
|
|
|
|
resolved,
|
|
|
|
rejected,
|
|
|
|
) as Promise<AxiosRequestConfig>;
|
2023-04-05 08:40:00 +08:00
|
|
|
}, true);
|
2023-03-23 20:09:00 +08:00
|
|
|
let promiseResponse = promiseRequest.then(dispatchRequest);
|
2023-04-05 08:40:00 +08:00
|
|
|
response.forEach(({ resolved, rejected }) => {
|
2023-03-23 20:09:00 +08:00
|
|
|
promiseResponse = promiseResponse.then(resolved, rejected) as Promise<
|
|
|
|
AxiosResponse<unknown>
|
|
|
|
>;
|
|
|
|
});
|
|
|
|
|
|
|
|
return promiseResponse as Promise<AxiosResponse<TData>>;
|
|
|
|
}
|
|
|
|
}
|