2023-03-28 21:32:54 +08:00
|
|
|
|
import { buildURL } from '../helpers/buildURL';
|
2023-04-06 22:59:49 +08:00
|
|
|
|
import { isAbsoluteURL } from '../helpers/isAbsoluteURL';
|
|
|
|
|
import { combineURL } from '../helpers/combineURL';
|
2023-04-18 10:11:29 +08:00
|
|
|
|
import { isFunction, isPromise, isString } from '../helpers/isTypes';
|
2023-03-23 20:09:00 +08:00
|
|
|
|
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
|
|
|
|
AxiosAdapterRequestConfig,
|
2023-04-09 21:01:43 +08:00
|
|
|
|
AxiosAdapterResponseData,
|
2023-03-24 20:15:51 +08:00
|
|
|
|
} from '../adapter';
|
2023-04-17 19:27:44 +08:00
|
|
|
|
import InterceptorManager, { Interceptor } from './InterceptorManager';
|
2023-04-09 21:01:43 +08:00
|
|
|
|
import { mergeConfig } from './mergeConfig';
|
2023-03-23 20:09:00 +08:00
|
|
|
|
import { CancelToken } from './cancel';
|
2023-04-09 15:20:10 +08:00
|
|
|
|
import { dispatchRequest } from './dispatchRequest';
|
2023-03-23 20:09:00 +08:00
|
|
|
|
import { AxiosTransformer } from './transformData';
|
2023-04-17 19:27:44 +08:00
|
|
|
|
|
2023-04-06 22:59:49 +08:00
|
|
|
|
import AxiosDomain from './AxiosDomain';
|
2023-03-23 20:09:00 +08:00
|
|
|
|
|
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'
|
2023-04-14 18:16:39 +08:00
|
|
|
|
| 'patch'
|
2023-03-23 20:09:00 +08:00
|
|
|
|
| '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-04-09 21:01:43 +08:00
|
|
|
|
export type AxiosResponseData = undefined | number | AxiosAdapterResponseData;
|
2023-04-09 15:20:10 +08:00
|
|
|
|
|
2023-03-23 20:09:00 +08:00
|
|
|
|
export interface AxiosProgressEvent {
|
2023-04-15 16:21:54 +08:00
|
|
|
|
/**
|
|
|
|
|
* 下载进度
|
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
|
progress: number;
|
2023-04-15 16:21:54 +08:00
|
|
|
|
/**
|
|
|
|
|
* 已经下载的数据长度
|
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
|
totalBytesSent: number;
|
2023-04-15 16:21:54 +08:00
|
|
|
|
/**
|
|
|
|
|
* 预期需要下载的数据总长度
|
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
|
totalBytesExpectedToSend: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AxiosProgressCallback {
|
|
|
|
|
(event: AxiosProgressEvent): void;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-28 20:35:40 +08:00
|
|
|
|
export interface AxiosRequestConfig
|
2023-04-09 15:20:10 +08:00
|
|
|
|
extends Partial<
|
|
|
|
|
Omit<AxiosAdapterRequestConfig, 'type' | 'success' | 'fail'>
|
2023-03-28 20:35:40 +08:00
|
|
|
|
> {
|
|
|
|
|
/**
|
|
|
|
|
* 请求适配器
|
|
|
|
|
*/
|
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-04-09 15:20:10 +08:00
|
|
|
|
/**
|
|
|
|
|
* 请求的 URL
|
|
|
|
|
*/
|
|
|
|
|
url?: 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
|
|
|
|
/**
|
|
|
|
|
* 转换请求数据
|
|
|
|
|
*/
|
2023-04-09 15:20:10 +08:00
|
|
|
|
transformRequest?: AxiosTransformer<AxiosRequestData>;
|
2023-03-28 20:35:40 +08:00
|
|
|
|
/**
|
|
|
|
|
* 转换响应数据
|
|
|
|
|
*/
|
2023-04-09 15:20:10 +08:00
|
|
|
|
transformResponse?: AxiosTransformer<AxiosResponseData>;
|
2023-03-28 20:35:40 +08:00
|
|
|
|
/**
|
2023-04-16 13:39:10 +08:00
|
|
|
|
* 错误处理
|
2023-03-28 20:35:40 +08:00
|
|
|
|
*/
|
2023-04-09 21:01:43 +08:00
|
|
|
|
errorHandler?: (error: unknown) => Promise<void> | void;
|
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-04-09 15:20:10 +08:00
|
|
|
|
export interface AxiosResponse<
|
|
|
|
|
TData extends AxiosResponseData = AxiosResponseData,
|
2023-04-18 11:14:57 +08:00
|
|
|
|
> extends AnyObject {
|
2023-04-06 15:16:12 +08:00
|
|
|
|
/**
|
2023-04-18 11:14:57 +08:00
|
|
|
|
* 状态码
|
2023-04-06 15:16:12 +08:00
|
|
|
|
*/
|
2023-04-18 11:14:57 +08:00
|
|
|
|
status: number;
|
2023-04-06 15:16:12 +08:00
|
|
|
|
/**
|
2023-04-18 11:14:57 +08:00
|
|
|
|
* 状态字符
|
2023-04-06 15:16:12 +08:00
|
|
|
|
*/
|
2023-04-18 11:14:57 +08:00
|
|
|
|
statusText: string;
|
|
|
|
|
/**
|
|
|
|
|
* 响应头
|
|
|
|
|
*/
|
|
|
|
|
headers: AnyObject;
|
2023-04-09 15:20:10 +08:00
|
|
|
|
/**
|
|
|
|
|
* 响应数据
|
|
|
|
|
*/
|
|
|
|
|
data: TData;
|
2023-04-18 11:14:57 +08:00
|
|
|
|
/**
|
|
|
|
|
* 请求配置
|
|
|
|
|
*/
|
|
|
|
|
config: AxiosRequestConfig;
|
|
|
|
|
/**
|
|
|
|
|
* 请求任务
|
|
|
|
|
*/
|
|
|
|
|
request?: AxiosAdapterTask;
|
2023-03-23 20:09:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 11:14:57 +08:00
|
|
|
|
export interface AxiosResponseError extends AnyObject {
|
|
|
|
|
/**
|
|
|
|
|
* 状态码
|
|
|
|
|
*/
|
|
|
|
|
status: number;
|
|
|
|
|
/**
|
|
|
|
|
* 状态字符
|
|
|
|
|
*/
|
|
|
|
|
statusText: string;
|
|
|
|
|
/**
|
|
|
|
|
* 响应头
|
|
|
|
|
*/
|
|
|
|
|
headers: AnyObject;
|
|
|
|
|
/**
|
|
|
|
|
* 错误数据
|
|
|
|
|
*/
|
|
|
|
|
data?: AnyObject;
|
2023-04-06 15:16:12 +08:00
|
|
|
|
/**
|
2023-04-11 13:13:45 +08:00
|
|
|
|
* 失败的请求,指没能够成功响应的请求
|
2023-04-06 15:16:12 +08:00
|
|
|
|
*/
|
2023-04-05 13:31:48 +08:00
|
|
|
|
isFail: true;
|
2023-04-06 15:16:12 +08:00
|
|
|
|
/**
|
|
|
|
|
* 请求配置
|
|
|
|
|
*/
|
2023-04-18 11:14:57 +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 22:59:49 +08:00
|
|
|
|
export default class Axios extends AxiosDomain {
|
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 16:00:04 +08:00
|
|
|
|
constructor(defaults: AxiosRequestConfig = {}) {
|
2023-04-08 22:37:50 +08:00
|
|
|
|
super(defaults, (config) => this.#processRequest(config));
|
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
|
|
|
|
/**
|
2023-04-06 22:59:49 +08:00
|
|
|
|
* 派生领域
|
2023-04-06 15:16:12 +08:00
|
|
|
|
*/
|
2023-04-17 19:27:44 +08:00
|
|
|
|
fork = (defaults: AxiosRequestConfig = {}) => {
|
2023-04-10 22:53:15 +08:00
|
|
|
|
if (isString(defaults.baseURL) && !isAbsoluteURL(defaults.baseURL)) {
|
|
|
|
|
defaults.baseURL = combineURL(this.defaults.baseURL, defaults.baseURL);
|
2023-04-06 22:59:49 +08:00
|
|
|
|
}
|
2023-04-09 21:01:43 +08:00
|
|
|
|
return new AxiosDomain(mergeConfig(this.defaults, defaults), (config) =>
|
|
|
|
|
this.#processRequest(config),
|
2023-04-08 22:37:50 +08:00
|
|
|
|
);
|
2023-04-17 19:27:44 +08:00
|
|
|
|
};
|
2023-04-06 22:59:49 +08:00
|
|
|
|
|
2023-04-09 15:20:10 +08:00
|
|
|
|
#processRequest(config: AxiosRequestConfig) {
|
2023-04-17 19:27:44 +08:00
|
|
|
|
const chain: [
|
|
|
|
|
Interceptor<AxiosRequestConfig> | Interceptor<AxiosResponse>,
|
|
|
|
|
] = [
|
|
|
|
|
{
|
|
|
|
|
resolved: dispatchRequest,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
this.interceptors.request.forEach(chain.unshift.bind(chain));
|
|
|
|
|
this.interceptors.response.forEach(chain.push.bind(chain));
|
|
|
|
|
|
|
|
|
|
let next = Promise.resolve(config);
|
|
|
|
|
for (const { resolved, rejected } of chain) {
|
|
|
|
|
next = next.then(
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
resolved,
|
|
|
|
|
rejected,
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-03-23 20:09:00 +08:00
|
|
|
|
|
2023-04-18 10:11:29 +08:00
|
|
|
|
// 错误处理
|
|
|
|
|
next = next.catch((reason) => {
|
|
|
|
|
const { errorHandler } = config;
|
|
|
|
|
|
|
|
|
|
if (isFunction(errorHandler)) {
|
|
|
|
|
const promise = errorHandler(reason);
|
|
|
|
|
if (isPromise(promise)) {
|
|
|
|
|
return promise.then(() => Promise.reject(reason));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Promise.reject(reason);
|
|
|
|
|
});
|
|
|
|
|
|
2023-04-17 19:27:44 +08:00
|
|
|
|
return next as Promise<AxiosResponse>;
|
2023-04-07 12:39:23 +08:00
|
|
|
|
}
|
2023-03-23 20:09:00 +08:00
|
|
|
|
}
|