2023-04-25 11:54:10 +08:00
|
|
|
|
import { isString } from '../helpers/isTypes';
|
|
|
|
|
import { dispatchRequest } from '../request/dispatchRequest';
|
2023-04-21 18:09:32 +08:00
|
|
|
|
import { CancelToken } from '../request/cancel';
|
|
|
|
|
import { AxiosTransformer } from '../request/transformData';
|
2023-04-25 11:54:10 +08:00
|
|
|
|
import { deepMerge } from '../helpers/deepMerge';
|
2023-03-23 20:09:00 +08:00
|
|
|
|
import {
|
|
|
|
|
AxiosAdapter,
|
2023-03-28 20:35:40 +08:00
|
|
|
|
AxiosAdapterRequestMethod,
|
2023-04-18 23:08:49 +08:00
|
|
|
|
AxiosAdapterPlatformTask,
|
2023-03-28 20:35:40 +08:00
|
|
|
|
AxiosAdapterRequestConfig,
|
2023-04-09 21:01:43 +08:00
|
|
|
|
AxiosAdapterResponseData,
|
2023-04-22 16:12:32 +08:00
|
|
|
|
} from '../adpater/createAdapter';
|
2023-04-25 11:54:10 +08:00
|
|
|
|
import InterceptorManager, {
|
|
|
|
|
Interceptor,
|
|
|
|
|
InterceptorExecutor,
|
|
|
|
|
} from './InterceptorManager';
|
2023-04-09 21:01:43 +08:00
|
|
|
|
import { mergeConfig } from './mergeConfig';
|
2023-04-25 11:54:10 +08:00
|
|
|
|
import {
|
|
|
|
|
PLAIN_METHODS,
|
|
|
|
|
WITH_DATA_METHODS,
|
|
|
|
|
WITH_PARAMS_METHODS,
|
|
|
|
|
} from '../constants/methods';
|
2023-04-25 14:28:28 +08:00
|
|
|
|
import MiddlewareManager, {
|
2023-04-25 22:18:08 +08:00
|
|
|
|
MiddlewareContext,
|
2023-04-25 14:28:28 +08:00
|
|
|
|
MiddlewareNext,
|
|
|
|
|
MiddlewareUse,
|
|
|
|
|
} from './MiddlewareManager';
|
2023-03-23 20:09:00 +08:00
|
|
|
|
|
2023-04-18 23:08:49 +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-04-18 23:08:49 +08:00
|
|
|
|
/**
|
|
|
|
|
* 请求头
|
|
|
|
|
*/
|
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-04-18 23:08:49 +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-18 23:08:49 +08:00
|
|
|
|
/**
|
|
|
|
|
* 请求数据
|
|
|
|
|
*/
|
|
|
|
|
export type AxiosRequestData =
|
|
|
|
|
| string
|
|
|
|
|
| AnyObject
|
|
|
|
|
| ArrayBuffer
|
|
|
|
|
| AxiosRequestFormData;
|
2023-04-06 15:16:12 +08:00
|
|
|
|
|
2023-04-18 23:08:49 +08:00
|
|
|
|
/**
|
|
|
|
|
* 响应数据
|
|
|
|
|
*/
|
2023-04-20 13:56:17 +08:00
|
|
|
|
export type AxiosResponseData = number | AxiosAdapterResponseData;
|
2023-04-09 15:20:10 +08:00
|
|
|
|
|
2023-04-18 23:08:49 +08:00
|
|
|
|
/**
|
2023-04-20 21:49:26 +08:00
|
|
|
|
* 进度对象
|
2023-04-18 23:08:49 +08:00
|
|
|
|
*/
|
2023-04-20 21:49:26 +08:00
|
|
|
|
export interface AxiosProgressEvent extends AnyObject {
|
2023-04-15 16:21:54 +08:00
|
|
|
|
/**
|
2023-04-20 21:49:26 +08:00
|
|
|
|
* 上传进度百分比
|
2023-04-15 16:21:54 +08:00
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
|
progress: number;
|
2023-04-20 21:49:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 下载进度对象
|
|
|
|
|
*/
|
|
|
|
|
export interface AxiosDownloadProgressEvent extends AxiosProgressEvent {
|
|
|
|
|
/**
|
|
|
|
|
* 已经下载的数据长度,单位 Bytes
|
|
|
|
|
*/
|
|
|
|
|
totalBytesWritten: number;
|
|
|
|
|
/**
|
|
|
|
|
* 预预期需要下载的数据总长度,单位 Bytes
|
|
|
|
|
*/
|
|
|
|
|
totalBytesExpectedToWrite: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 监听下载进度
|
|
|
|
|
*/
|
|
|
|
|
export interface AxiosDownloadProgressCallback {
|
|
|
|
|
(event: AxiosDownloadProgressEvent): void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 上传进度对象
|
|
|
|
|
*/
|
|
|
|
|
export interface AxiosUploadProgressEvent extends AxiosProgressEvent {
|
2023-04-15 16:21:54 +08:00
|
|
|
|
/**
|
2023-04-20 21:49:26 +08:00
|
|
|
|
* 已经上传的数据长度,单位 Bytes
|
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-04-20 21:49:26 +08:00
|
|
|
|
* 预期需要上传的数据总长度,单位 Bytes
|
2023-04-15 16:21:54 +08:00
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
|
totalBytesExpectedToSend: number;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 23:08:49 +08:00
|
|
|
|
/**
|
2023-04-20 21:49:26 +08:00
|
|
|
|
* 监听上传进度
|
2023-04-18 23:08:49 +08:00
|
|
|
|
*/
|
2023-04-20 21:49:26 +08:00
|
|
|
|
export interface AxiosUploadProgressCallback {
|
|
|
|
|
(event: AxiosUploadProgressEvent): void;
|
2023-03-23 20:09:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 23:08:49 +08:00
|
|
|
|
/**
|
|
|
|
|
* 请求配置
|
|
|
|
|
*/
|
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
|
|
|
|
/**
|
2023-04-19 12:00:06 +08:00
|
|
|
|
* 下载文件
|
|
|
|
|
*/
|
|
|
|
|
download?: boolean;
|
|
|
|
|
/**
|
|
|
|
|
* 上传文件
|
2023-03-28 20:35:40 +08:00
|
|
|
|
*/
|
|
|
|
|
upload?: boolean;
|
|
|
|
|
/**
|
2023-04-19 12:00:06 +08:00
|
|
|
|
* 请求参数系列化函数
|
2023-03-28 20:35:40 +08:00
|
|
|
|
*/
|
2023-04-19 12:00:06 +08:00
|
|
|
|
paramsSerializer?: (params?: AnyObject) => string;
|
|
|
|
|
/**
|
|
|
|
|
* 校验状态码
|
|
|
|
|
*/
|
|
|
|
|
validateStatus?: (status: number) => 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-20 21:49:26 +08:00
|
|
|
|
errorHandler?: (error: unknown) => Promise<AxiosResponse>;
|
2023-03-28 20:35:40 +08:00
|
|
|
|
/**
|
2023-04-20 21:49:26 +08:00
|
|
|
|
* 监听下载进度
|
2023-03-28 20:35:40 +08:00
|
|
|
|
*/
|
2023-04-20 21:49:26 +08:00
|
|
|
|
onDownloadProgress?: AxiosUploadProgressCallback;
|
2023-03-28 20:35:40 +08:00
|
|
|
|
/**
|
2023-04-20 21:49:26 +08:00
|
|
|
|
* 监听上传进度
|
2023-03-28 20:35:40 +08:00
|
|
|
|
*/
|
2023-04-20 21:49:26 +08:00
|
|
|
|
onUploadProgress?: AxiosUploadProgressCallback;
|
2023-03-23 20:09:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 23:08:49 +08:00
|
|
|
|
/**
|
|
|
|
|
* 响应体
|
|
|
|
|
*/
|
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;
|
|
|
|
|
/**
|
|
|
|
|
* 请求任务
|
|
|
|
|
*/
|
2023-04-18 23:08:49 +08:00
|
|
|
|
request?: AxiosAdapterPlatformTask;
|
2023-03-23 20:09:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 23:08:49 +08:00
|
|
|
|
/**
|
|
|
|
|
* 错误体
|
|
|
|
|
*/
|
2023-04-18 11:14:57 +08:00
|
|
|
|
export interface AxiosResponseError extends AnyObject {
|
|
|
|
|
/**
|
|
|
|
|
* 状态码
|
|
|
|
|
*/
|
|
|
|
|
status: number;
|
|
|
|
|
/**
|
|
|
|
|
* 状态字符
|
|
|
|
|
*/
|
|
|
|
|
statusText: string;
|
|
|
|
|
/**
|
|
|
|
|
* 响应头
|
|
|
|
|
*/
|
|
|
|
|
headers: AnyObject;
|
|
|
|
|
/**
|
|
|
|
|
* 错误数据
|
|
|
|
|
*/
|
2023-04-20 13:56:17 +08:00
|
|
|
|
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-04-18 23:08:49 +08:00
|
|
|
|
request?: AxiosAdapterPlatformTask;
|
2023-03-23 20:09:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 11:54:10 +08:00
|
|
|
|
export interface AxiosRequest {
|
|
|
|
|
<TData extends AxiosResponseData>(config: AxiosRequestConfig): Promise<
|
|
|
|
|
AxiosResponse<TData>
|
|
|
|
|
>;
|
|
|
|
|
<TData extends AxiosResponseData>(
|
|
|
|
|
url: string,
|
|
|
|
|
config?: AxiosRequestConfig,
|
|
|
|
|
): Promise<AxiosResponse<TData>>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 普通的请求方法
|
|
|
|
|
*/
|
|
|
|
|
export type AxiosRequestMethodFn = <TData extends AxiosResponseData>(
|
|
|
|
|
url: string,
|
|
|
|
|
config?: AxiosRequestConfig,
|
|
|
|
|
) => Promise<AxiosResponse<TData>>;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 带参数的请求方法
|
|
|
|
|
*/
|
|
|
|
|
export type AxiosRequestMethodFnWithParams = <TData extends AxiosResponseData>(
|
|
|
|
|
url: string,
|
|
|
|
|
params?: AnyObject,
|
|
|
|
|
config?: AxiosRequestConfig,
|
|
|
|
|
) => Promise<AxiosResponse<TData>>;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 带数据的请求方法
|
|
|
|
|
*/
|
|
|
|
|
export type AxiosRequestMethodFnWithData = <TData extends AxiosResponseData>(
|
|
|
|
|
url: string,
|
|
|
|
|
data?: AxiosRequestData,
|
|
|
|
|
config?: AxiosRequestConfig,
|
|
|
|
|
) => Promise<AxiosResponse<TData>>;
|
|
|
|
|
|
|
|
|
|
export interface AxiosDomainRequestHandler {
|
|
|
|
|
(config: AxiosRequestConfig): Promise<AxiosResponse>;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 23:08:49 +08:00
|
|
|
|
/**
|
|
|
|
|
* Axios 构造函数
|
|
|
|
|
*/
|
2023-03-23 20:09:00 +08:00
|
|
|
|
export interface AxiosConstructor {
|
|
|
|
|
new (config: AxiosRequestConfig): Axios;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 11:54:10 +08:00
|
|
|
|
export default class Axios {
|
|
|
|
|
#parent?: Axios;
|
|
|
|
|
/**
|
|
|
|
|
* 默认请求配置
|
|
|
|
|
*/
|
|
|
|
|
defaults: AxiosRequestConfig;
|
|
|
|
|
|
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-25 11:54:10 +08:00
|
|
|
|
/**
|
|
|
|
|
* 中间件
|
|
|
|
|
*/
|
2023-04-25 22:18:08 +08:00
|
|
|
|
#middleware = new MiddlewareManager();
|
2023-03-23 20:09:00 +08:00
|
|
|
|
|
2023-04-25 11:54:10 +08:00
|
|
|
|
/**
|
|
|
|
|
* 发送 options 请求
|
|
|
|
|
*/
|
|
|
|
|
options!: AxiosRequestMethodFn;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送 get 请求
|
|
|
|
|
*/
|
|
|
|
|
get!: AxiosRequestMethodFnWithParams;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送 head 请求
|
|
|
|
|
*/
|
|
|
|
|
head!: AxiosRequestMethodFnWithParams;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送 post 请求
|
|
|
|
|
*/
|
|
|
|
|
post!: AxiosRequestMethodFnWithData;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送 put 请求
|
|
|
|
|
*/
|
|
|
|
|
put!: AxiosRequestMethodFnWithData;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送 patch 请求
|
|
|
|
|
*/
|
|
|
|
|
patch!: AxiosRequestMethodFnWithData;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送 delete 请求
|
|
|
|
|
*/
|
|
|
|
|
delete!: AxiosRequestMethodFnWithParams;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送 trace 请求
|
|
|
|
|
*/
|
|
|
|
|
trace!: AxiosRequestMethodFn;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送 connect 请求
|
|
|
|
|
*/
|
|
|
|
|
connect!: AxiosRequestMethodFn;
|
2023-03-23 20:09:00 +08:00
|
|
|
|
|
2023-04-06 15:16:12 +08:00
|
|
|
|
/**
|
2023-04-25 11:54:10 +08:00
|
|
|
|
* 添加中间件
|
2023-04-06 15:16:12 +08:00
|
|
|
|
*/
|
2023-04-25 22:18:08 +08:00
|
|
|
|
use: MiddlewareUse;
|
2023-04-25 11:54:10 +08:00
|
|
|
|
|
2023-04-25 22:18:08 +08:00
|
|
|
|
constructor(defaults: AxiosRequestConfig, parent?: Axios) {
|
2023-04-25 11:54:10 +08:00
|
|
|
|
this.defaults = defaults;
|
|
|
|
|
this.#parent = parent;
|
2023-04-25 14:28:28 +08:00
|
|
|
|
this.use = this.#middleware.use;
|
2023-04-25 11:54:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送请求
|
|
|
|
|
*/
|
|
|
|
|
request: AxiosRequest = (
|
|
|
|
|
urlOrConfig: string | AxiosRequestConfig,
|
|
|
|
|
config: AxiosRequestConfig = {},
|
|
|
|
|
) => {
|
|
|
|
|
if (isString(urlOrConfig)) {
|
|
|
|
|
config.url = urlOrConfig;
|
|
|
|
|
} else {
|
|
|
|
|
config = urlOrConfig;
|
|
|
|
|
}
|
|
|
|
|
config.method = config.method || 'get';
|
|
|
|
|
|
|
|
|
|
return this.#processRequest(mergeConfig(this.defaults, config));
|
2023-04-17 19:27:44 +08:00
|
|
|
|
};
|
2023-04-06 22:59:49 +08:00
|
|
|
|
|
2023-04-25 11:54:10 +08:00
|
|
|
|
#processRequest(config: AxiosRequestConfig) {
|
2023-04-20 21:49:26 +08:00
|
|
|
|
const requestHandler = {
|
2023-04-25 14:28:28 +08:00
|
|
|
|
resolved: this.#requestHandler,
|
2023-04-20 21:49:26 +08:00
|
|
|
|
};
|
|
|
|
|
const errorHandler = {
|
|
|
|
|
rejected: config.errorHandler,
|
|
|
|
|
};
|
|
|
|
|
const chain: (
|
|
|
|
|
| Partial<Interceptor<AxiosRequestConfig>>
|
|
|
|
|
| Partial<Interceptor<AxiosResponse>>
|
|
|
|
|
)[] = [];
|
2023-03-23 20:09:00 +08:00
|
|
|
|
|
2023-04-25 14:28:28 +08:00
|
|
|
|
this.#eachRequestInterceptors((requestInterceptor) => {
|
2023-04-20 21:49:26 +08:00
|
|
|
|
chain.unshift(requestInterceptor);
|
|
|
|
|
});
|
|
|
|
|
chain.push(requestHandler);
|
2023-04-25 14:28:28 +08:00
|
|
|
|
this.#eachResponseInterceptors((responseInterceptor) => {
|
2023-04-20 21:49:26 +08:00
|
|
|
|
chain.push(responseInterceptor);
|
2023-04-18 10:11:29 +08:00
|
|
|
|
});
|
2023-04-20 21:49:26 +08:00
|
|
|
|
chain.push(errorHandler);
|
2023-04-18 10:11:29 +08:00
|
|
|
|
|
2023-04-20 21:49:26 +08:00
|
|
|
|
return chain.reduce(
|
|
|
|
|
(next, { resolved, rejected }) =>
|
|
|
|
|
next.then(
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
resolved,
|
|
|
|
|
rejected,
|
|
|
|
|
),
|
|
|
|
|
Promise.resolve(config),
|
|
|
|
|
) as Promise<AxiosResponse>;
|
2023-04-07 12:39:23 +08:00
|
|
|
|
}
|
2023-04-25 11:54:10 +08:00
|
|
|
|
|
2023-04-25 14:28:28 +08:00
|
|
|
|
#eachRequestInterceptors(executor: InterceptorExecutor<AxiosRequestConfig>) {
|
2023-04-25 11:54:10 +08:00
|
|
|
|
this.interceptors.request.forEach(executor);
|
|
|
|
|
if (this.#parent) {
|
2023-04-25 14:28:28 +08:00
|
|
|
|
this.#parent.#eachRequestInterceptors(executor);
|
2023-04-25 11:54:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 14:28:28 +08:00
|
|
|
|
#eachResponseInterceptors(executor: InterceptorExecutor<AxiosResponse>) {
|
2023-04-25 11:54:10 +08:00
|
|
|
|
this.interceptors.response.forEach(executor);
|
|
|
|
|
if (this.#parent) {
|
2023-04-25 14:28:28 +08:00
|
|
|
|
this.#parent.#eachResponseInterceptors(executor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#requestHandler = async (config: AxiosRequestConfig) => {
|
2023-04-25 22:18:08 +08:00
|
|
|
|
const ctx: MiddlewareContext = {
|
2023-04-25 14:28:28 +08:00
|
|
|
|
req: config,
|
|
|
|
|
res: null,
|
|
|
|
|
};
|
2023-04-25 22:18:08 +08:00
|
|
|
|
await this.#wrap(ctx, async () => {
|
2023-04-25 14:28:28 +08:00
|
|
|
|
ctx.res = await dispatchRequest(ctx.req);
|
|
|
|
|
});
|
|
|
|
|
return ctx.res as AxiosResponse;
|
|
|
|
|
};
|
|
|
|
|
|
2023-04-25 22:18:08 +08:00
|
|
|
|
#wrap(ctx: MiddlewareContext, flush: MiddlewareNext): Promise<void> {
|
2023-04-25 14:28:28 +08:00
|
|
|
|
if (this.#parent) {
|
2023-04-25 22:18:08 +08:00
|
|
|
|
return this.#parent.#wrap(ctx, () => {
|
|
|
|
|
return this.#middleware.wrap(ctx, flush);
|
2023-04-25 14:28:28 +08:00
|
|
|
|
});
|
2023-04-25 11:54:10 +08:00
|
|
|
|
}
|
2023-04-25 22:18:08 +08:00
|
|
|
|
return this.#middleware.wrap(ctx, flush);
|
2023-04-25 11:54:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const method of PLAIN_METHODS) {
|
|
|
|
|
Axios.prototype[method] = function processRequestMethod(url, config = {}) {
|
|
|
|
|
config.method = method;
|
|
|
|
|
return this.request(url, config);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const method of WITH_PARAMS_METHODS) {
|
|
|
|
|
Axios.prototype[method] = function processRequestMethodWithParams(
|
|
|
|
|
url,
|
|
|
|
|
params = {},
|
|
|
|
|
config = {},
|
|
|
|
|
) {
|
|
|
|
|
config.method = method;
|
|
|
|
|
config.params = deepMerge(params, config.params ?? {});
|
|
|
|
|
return this.request(url, config);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const method of WITH_DATA_METHODS) {
|
|
|
|
|
Axios.prototype[method] = function processRequestMethodWithData(
|
|
|
|
|
url,
|
|
|
|
|
data,
|
|
|
|
|
config = {},
|
|
|
|
|
) {
|
|
|
|
|
config.method = method;
|
|
|
|
|
config.data = data;
|
|
|
|
|
return this.request(url, config);
|
|
|
|
|
};
|
2023-03-23 20:09:00 +08:00
|
|
|
|
}
|