fix: data 不支持对象以外的类型
parent
d714ed23c0
commit
4d8ec80f29
|
@ -11,8 +11,14 @@ import {
|
||||||
AxiosRequestHeaders,
|
AxiosRequestHeaders,
|
||||||
} from './core/Axios';
|
} from './core/Axios';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 适配器请求类型
|
||||||
|
*/
|
||||||
export type AxiosAdapterRequestType = 'request' | 'download' | 'upload';
|
export type AxiosAdapterRequestType = 'request' | 'download' | 'upload';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 适配器请求方法
|
||||||
|
*/
|
||||||
export type AxiosAdapterRequestMethod =
|
export type AxiosAdapterRequestMethod =
|
||||||
| 'OPTIONS'
|
| 'OPTIONS'
|
||||||
| 'GET'
|
| 'GET'
|
||||||
|
@ -24,8 +30,19 @@ export type AxiosAdapterRequestMethod =
|
||||||
| 'TRACE'
|
| 'TRACE'
|
||||||
| 'CONNECT';
|
| 'CONNECT';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 适配器请求数据
|
||||||
|
*/
|
||||||
|
export type AxiosAdapterRequestData = string | AnyObject | ArrayBuffer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 适配器响应数据
|
||||||
|
*/
|
||||||
export type AxiosAdapterResponseData = string | ArrayBuffer | AnyObject;
|
export type AxiosAdapterResponseData = string | ArrayBuffer | AnyObject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 适配器响应体
|
||||||
|
*/
|
||||||
export interface AxiosAdapterResponse extends AnyObject {
|
export interface AxiosAdapterResponse extends AnyObject {
|
||||||
/**
|
/**
|
||||||
* 状态码
|
* 状态码
|
||||||
|
@ -45,6 +62,9 @@ export interface AxiosAdapterResponse extends AnyObject {
|
||||||
data: AxiosAdapterResponseData;
|
data: AxiosAdapterResponseData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 适配器错误体
|
||||||
|
*/
|
||||||
export interface AxiosAdapterResponseError extends AnyObject {
|
export interface AxiosAdapterResponseError extends AnyObject {
|
||||||
/**
|
/**
|
||||||
* 状态码
|
* 状态码
|
||||||
|
@ -64,6 +84,9 @@ export interface AxiosAdapterResponseError extends AnyObject {
|
||||||
data?: AnyObject;
|
data?: AnyObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 适配器请求配置
|
||||||
|
*/
|
||||||
export interface AxiosAdapterRequestConfig extends AnyObject {
|
export interface AxiosAdapterRequestConfig extends AnyObject {
|
||||||
/**
|
/**
|
||||||
* 请求类型
|
* 请求类型
|
||||||
|
@ -84,7 +107,7 @@ export interface AxiosAdapterRequestConfig extends AnyObject {
|
||||||
/**
|
/**
|
||||||
* 请求数据
|
* 请求数据
|
||||||
*/
|
*/
|
||||||
data?: AnyObject;
|
data?: AxiosAdapterRequestData;
|
||||||
/**
|
/**
|
||||||
* 请求头
|
* 请求头
|
||||||
*/
|
*/
|
||||||
|
@ -111,14 +134,30 @@ export interface AxiosAdapterRequestConfig extends AnyObject {
|
||||||
fail(error: AxiosAdapterResponseError): void;
|
fail(error: AxiosAdapterResponseError): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求函数基本选项
|
||||||
|
*/
|
||||||
export interface AxiosAdapterBaseOptions extends AxiosAdapterRequestConfig {
|
export interface AxiosAdapterBaseOptions extends AxiosAdapterRequestConfig {
|
||||||
header?: AxiosRequestHeaders;
|
header?: AxiosRequestHeaders;
|
||||||
success(response: AxiosAdapterResponse): void;
|
success(response: AxiosAdapterResponse): void;
|
||||||
fail(error: AxiosAdapterResponseError): void;
|
fail(error: AxiosAdapterResponseError): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求函数选项
|
||||||
|
*/
|
||||||
export type AxiosAdapterRequestOptions = AxiosAdapterBaseOptions;
|
export type AxiosAdapterRequestOptions = AxiosAdapterBaseOptions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载函数选项
|
||||||
|
*/
|
||||||
|
export interface AxiosAdapterDownloadOptions extends AxiosAdapterBaseOptions {
|
||||||
|
filePath?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传函数选项
|
||||||
|
*/
|
||||||
export interface AxiosAdapterUploadOptions
|
export interface AxiosAdapterUploadOptions
|
||||||
extends AxiosAdapterBaseOptions,
|
extends AxiosAdapterBaseOptions,
|
||||||
AxiosRequestFormData {
|
AxiosRequestFormData {
|
||||||
|
@ -126,22 +165,30 @@ export interface AxiosAdapterUploadOptions
|
||||||
formData?: AnyObject;
|
formData?: AnyObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AxiosAdapterDownloadOptions extends AxiosAdapterBaseOptions {
|
/**
|
||||||
filePath?: string;
|
* 请求函数
|
||||||
}
|
*/
|
||||||
|
|
||||||
export interface AxiosAdapterRequest {
|
export interface AxiosAdapterRequest {
|
||||||
(config: AxiosAdapterRequestOptions): AxiosAdapterTask;
|
(config: AxiosAdapterRequestOptions): AxiosAdapterPlatformTask;
|
||||||
}
|
|
||||||
|
|
||||||
export interface AxiosAdapterUpload {
|
|
||||||
(config: AxiosAdapterUploadOptions): AxiosAdapterTask;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载函数
|
||||||
|
*/
|
||||||
export interface AxiosAdapterDownload {
|
export interface AxiosAdapterDownload {
|
||||||
(config: AxiosAdapterDownloadOptions): AxiosAdapterTask;
|
(config: AxiosAdapterDownloadOptions): AxiosAdapterPlatformTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传函数
|
||||||
|
*/
|
||||||
|
export interface AxiosAdapterUpload {
|
||||||
|
(config: AxiosAdapterUploadOptions): AxiosAdapterPlatformTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 适配器平台
|
||||||
|
*/
|
||||||
export interface AxiosAdapterPlatform {
|
export interface AxiosAdapterPlatform {
|
||||||
/**
|
/**
|
||||||
* 发送请求
|
* 发送请求
|
||||||
|
@ -157,7 +204,10 @@ export interface AxiosAdapterPlatform {
|
||||||
upload: AxiosAdapterUpload;
|
upload: AxiosAdapterUpload;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type AxiosAdapterTask =
|
/**
|
||||||
|
* 适配器平台请求任务
|
||||||
|
*/
|
||||||
|
export type AxiosAdapterPlatformTask =
|
||||||
| undefined
|
| undefined
|
||||||
| void
|
| void
|
||||||
| {
|
| {
|
||||||
|
@ -166,10 +216,16 @@ export type AxiosAdapterTask =
|
||||||
offProgressUpdate?(callback: AxiosProgressCallback): void;
|
offProgressUpdate?(callback: AxiosProgressCallback): void;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 适配器函数
|
||||||
|
*/
|
||||||
export interface AxiosAdapter {
|
export interface AxiosAdapter {
|
||||||
(config: AxiosAdapterRequestConfig): AxiosAdapterTask;
|
(config: AxiosAdapterRequestConfig): AxiosAdapterPlatformTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取支持的平台适配器
|
||||||
|
*/
|
||||||
export function getDefaultAdapter() {
|
export function getDefaultAdapter() {
|
||||||
const platform = revisePlatformApiNames(getPlatform());
|
const platform = revisePlatformApiNames(getPlatform());
|
||||||
|
|
||||||
|
@ -220,13 +276,20 @@ export function getDefaultAdapter() {
|
||||||
return createAdapter(platform);
|
return createAdapter(platform);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建适配器
|
||||||
|
*
|
||||||
|
* @param platform 平台 API 对象
|
||||||
|
*/
|
||||||
export function createAdapter(platform: AxiosAdapterPlatform) {
|
export function createAdapter(platform: AxiosAdapterPlatform) {
|
||||||
assert(isPlainObject(platform), 'platform 不是一个 object');
|
assert(isPlainObject(platform), 'platform 不是一个 object');
|
||||||
assert(isFunction(platform.request), 'request 不是一个 function');
|
assert(isFunction(platform.request), 'request 不是一个 function');
|
||||||
assert(isFunction(platform.upload), 'upload 不是一个 function');
|
assert(isFunction(platform.upload), 'upload 不是一个 function');
|
||||||
assert(isFunction(platform.download), 'download 不是一个 function');
|
assert(isFunction(platform.download), 'download 不是一个 function');
|
||||||
|
|
||||||
function adapter(config: AxiosAdapterRequestConfig): AxiosAdapterTask {
|
function adapter(
|
||||||
|
config: AxiosAdapterRequestConfig,
|
||||||
|
): AxiosAdapterPlatformTask {
|
||||||
const baseOptions = transformOptions(config);
|
const baseOptions = transformOptions(config);
|
||||||
|
|
||||||
switch (config.type) {
|
switch (config.type) {
|
||||||
|
@ -242,14 +305,14 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
|
||||||
function processRequest(
|
function processRequest(
|
||||||
request: AxiosAdapterRequest,
|
request: AxiosAdapterRequest,
|
||||||
baseOptions: AxiosAdapterBaseOptions,
|
baseOptions: AxiosAdapterBaseOptions,
|
||||||
): AxiosAdapterTask {
|
): AxiosAdapterPlatformTask {
|
||||||
return request(baseOptions);
|
return request(baseOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
function processUpload(
|
function processUpload(
|
||||||
upload: AxiosAdapterUpload,
|
upload: AxiosAdapterUpload,
|
||||||
baseOptions: AxiosAdapterBaseOptions,
|
baseOptions: AxiosAdapterBaseOptions,
|
||||||
): AxiosAdapterTask {
|
): AxiosAdapterPlatformTask {
|
||||||
const { name, filePath, fileType, ...formData } =
|
const { name, filePath, fileType, ...formData } =
|
||||||
baseOptions.data as AxiosRequestFormData;
|
baseOptions.data as AxiosRequestFormData;
|
||||||
const options = {
|
const options = {
|
||||||
|
@ -273,7 +336,7 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
|
||||||
function processDownload(
|
function processDownload(
|
||||||
download: AxiosAdapterDownload,
|
download: AxiosAdapterDownload,
|
||||||
baseOptions: AxiosAdapterBaseOptions,
|
baseOptions: AxiosAdapterBaseOptions,
|
||||||
): AxiosAdapterTask {
|
): AxiosAdapterPlatformTask {
|
||||||
const options: AxiosAdapterDownloadOptions = {
|
const options: AxiosAdapterDownloadOptions = {
|
||||||
...baseOptions,
|
...baseOptions,
|
||||||
filePath: baseOptions.params?.filePath,
|
filePath: baseOptions.params?.filePath,
|
||||||
|
|
13
src/axios.ts
13
src/axios.ts
|
@ -10,6 +10,9 @@ import { mergeConfig } from './core/mergeConfig';
|
||||||
import { createAdapter } from './adapter';
|
import { createAdapter } from './adapter';
|
||||||
import defaults from './defaults';
|
import defaults from './defaults';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* axios 实例默认配置
|
||||||
|
*/
|
||||||
export interface AxiosInstanceDefaults extends AxiosRequestConfig {
|
export interface AxiosInstanceDefaults extends AxiosRequestConfig {
|
||||||
/**
|
/**
|
||||||
* 请求头
|
* 请求头
|
||||||
|
@ -17,6 +20,9 @@ export interface AxiosInstanceDefaults extends AxiosRequestConfig {
|
||||||
headers: Required<AxiosRequestHeaders>;
|
headers: Required<AxiosRequestHeaders>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* axios 实例
|
||||||
|
*/
|
||||||
export interface AxiosInstance extends AxiosDomainRequest, Axios {
|
export interface AxiosInstance extends AxiosDomainRequest, Axios {
|
||||||
/**
|
/**
|
||||||
* 默认请求配置
|
* 默认请求配置
|
||||||
|
@ -24,6 +30,9 @@ export interface AxiosInstance extends AxiosDomainRequest, Axios {
|
||||||
defaults: AxiosInstanceDefaults;
|
defaults: AxiosInstanceDefaults;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* axios 静态对象
|
||||||
|
*/
|
||||||
export interface AxiosStatic extends AxiosInstance {
|
export interface AxiosStatic extends AxiosInstance {
|
||||||
/**
|
/**
|
||||||
* Axios 类
|
* Axios 类
|
||||||
|
@ -44,11 +53,11 @@ export interface AxiosStatic extends AxiosInstance {
|
||||||
*/
|
*/
|
||||||
createAdapter: typeof createAdapter;
|
createAdapter: typeof createAdapter;
|
||||||
/**
|
/**
|
||||||
* 判断 Cancel
|
* 传入取消请求错误返回 true
|
||||||
*/
|
*/
|
||||||
isCancel: typeof isCancel;
|
isCancel: typeof isCancel;
|
||||||
/**
|
/**
|
||||||
* 判断 AxiosError
|
* 传入响应错误返回 true
|
||||||
*/
|
*/
|
||||||
isAxiosError: typeof isAxiosError;
|
isAxiosError: typeof isAxiosError;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { isFunction, isPromise, isString } from '../helpers/isTypes';
|
||||||
import {
|
import {
|
||||||
AxiosAdapter,
|
AxiosAdapter,
|
||||||
AxiosAdapterRequestMethod,
|
AxiosAdapterRequestMethod,
|
||||||
AxiosAdapterTask,
|
AxiosAdapterPlatformTask,
|
||||||
AxiosAdapterRequestConfig,
|
AxiosAdapterRequestConfig,
|
||||||
AxiosAdapterResponseData,
|
AxiosAdapterResponseData,
|
||||||
} from '../adapter';
|
} from '../adapter';
|
||||||
|
@ -14,9 +14,11 @@ import { mergeConfig } from './mergeConfig';
|
||||||
import { CancelToken } from './cancel';
|
import { CancelToken } from './cancel';
|
||||||
import { dispatchRequest } from './dispatchRequest';
|
import { dispatchRequest } from './dispatchRequest';
|
||||||
import { AxiosTransformer } from './transformData';
|
import { AxiosTransformer } from './transformData';
|
||||||
|
|
||||||
import AxiosDomain from './AxiosDomain';
|
import AxiosDomain from './AxiosDomain';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求方法
|
||||||
|
*/
|
||||||
export type AxiosRequestMethod =
|
export type AxiosRequestMethod =
|
||||||
| AxiosAdapterRequestMethod
|
| AxiosAdapterRequestMethod
|
||||||
| 'options'
|
| 'options'
|
||||||
|
@ -29,6 +31,9 @@ export type AxiosRequestMethod =
|
||||||
| 'trace'
|
| 'trace'
|
||||||
| 'connect';
|
| 'connect';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求头
|
||||||
|
*/
|
||||||
export interface AxiosRequestHeaders extends AnyObject {
|
export interface AxiosRequestHeaders extends AnyObject {
|
||||||
/**
|
/**
|
||||||
* 通用请求头
|
* 通用请求头
|
||||||
|
@ -68,6 +73,9 @@ export interface AxiosRequestHeaders extends AnyObject {
|
||||||
connect?: AnyObject;
|
connect?: AnyObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 表单数据(上传会用到)
|
||||||
|
*/
|
||||||
export interface AxiosRequestFormData extends AnyObject {
|
export interface AxiosRequestFormData extends AnyObject {
|
||||||
/**
|
/**
|
||||||
* 文件名
|
* 文件名
|
||||||
|
@ -79,10 +87,23 @@ export interface AxiosRequestFormData extends AnyObject {
|
||||||
filePath: string;
|
filePath: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type AxiosRequestData = AnyObject | AxiosRequestFormData;
|
/**
|
||||||
|
* 请求数据
|
||||||
|
*/
|
||||||
|
export type AxiosRequestData =
|
||||||
|
| string
|
||||||
|
| AnyObject
|
||||||
|
| ArrayBuffer
|
||||||
|
| AxiosRequestFormData;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 响应数据
|
||||||
|
*/
|
||||||
export type AxiosResponseData = undefined | number | AxiosAdapterResponseData;
|
export type AxiosResponseData = undefined | number | AxiosAdapterResponseData;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监听进度回调事件对象
|
||||||
|
*/
|
||||||
export interface AxiosProgressEvent {
|
export interface AxiosProgressEvent {
|
||||||
/**
|
/**
|
||||||
* 下载进度
|
* 下载进度
|
||||||
|
@ -98,10 +119,21 @@ export interface AxiosProgressEvent {
|
||||||
totalBytesExpectedToSend: number;
|
totalBytesExpectedToSend: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监听进度回调
|
||||||
|
*/
|
||||||
export interface AxiosProgressCallback {
|
export interface AxiosProgressCallback {
|
||||||
(event: AxiosProgressEvent): void;
|
(
|
||||||
|
/**
|
||||||
|
* 事件对象
|
||||||
|
*/
|
||||||
|
event: AxiosProgressEvent,
|
||||||
|
): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求配置
|
||||||
|
*/
|
||||||
export interface AxiosRequestConfig
|
export interface AxiosRequestConfig
|
||||||
extends Partial<
|
extends Partial<
|
||||||
Omit<AxiosAdapterRequestConfig, 'type' | 'success' | 'fail'>
|
Omit<AxiosAdapterRequestConfig, 'type' | 'success' | 'fail'>
|
||||||
|
@ -176,6 +208,9 @@ export interface AxiosRequestConfig
|
||||||
validateStatus?: (status: number) => boolean;
|
validateStatus?: (status: number) => boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 响应体
|
||||||
|
*/
|
||||||
export interface AxiosResponse<
|
export interface AxiosResponse<
|
||||||
TData extends AxiosResponseData = AxiosResponseData,
|
TData extends AxiosResponseData = AxiosResponseData,
|
||||||
> extends AnyObject {
|
> extends AnyObject {
|
||||||
|
@ -202,9 +237,12 @@ export interface AxiosResponse<
|
||||||
/**
|
/**
|
||||||
* 请求任务
|
* 请求任务
|
||||||
*/
|
*/
|
||||||
request?: AxiosAdapterTask;
|
request?: AxiosAdapterPlatformTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 错误体
|
||||||
|
*/
|
||||||
export interface AxiosResponseError extends AnyObject {
|
export interface AxiosResponseError extends AnyObject {
|
||||||
/**
|
/**
|
||||||
* 状态码
|
* 状态码
|
||||||
|
@ -233,9 +271,12 @@ export interface AxiosResponseError extends AnyObject {
|
||||||
/**
|
/**
|
||||||
* 请求任务
|
* 请求任务
|
||||||
*/
|
*/
|
||||||
request?: AxiosAdapterTask;
|
request?: AxiosAdapterPlatformTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Axios 构造函数
|
||||||
|
*/
|
||||||
export interface AxiosConstructor {
|
export interface AxiosConstructor {
|
||||||
new (config: AxiosRequestConfig): Axios;
|
new (config: AxiosRequestConfig): Axios;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,71 +8,48 @@ import {
|
||||||
AxiosResponseData,
|
AxiosResponseData,
|
||||||
} from './Axios';
|
} from './Axios';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请求函数
|
||||||
|
*/
|
||||||
export interface AxiosDomainRequest {
|
export interface AxiosDomainRequest {
|
||||||
|
<TData extends AxiosResponseData>(config: AxiosRequestConfig): Promise<
|
||||||
|
AxiosResponse<TData>
|
||||||
|
>;
|
||||||
<TData extends AxiosResponseData>(
|
<TData extends AxiosResponseData>(
|
||||||
/**
|
|
||||||
* 请求配置
|
|
||||||
*/
|
|
||||||
config: AxiosRequestConfig,
|
|
||||||
): Promise<AxiosResponse<TData>>;
|
|
||||||
<TData extends AxiosResponseData>(
|
|
||||||
/**
|
|
||||||
* 请求地址
|
|
||||||
*/
|
|
||||||
url: string,
|
url: string,
|
||||||
/**
|
|
||||||
* 请求配置
|
|
||||||
*/
|
|
||||||
config?: AxiosRequestConfig,
|
config?: AxiosRequestConfig,
|
||||||
): Promise<AxiosResponse<TData>>;
|
): Promise<AxiosResponse<TData>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface AxiosDomainRequestMethod {
|
/**
|
||||||
<TData extends AxiosResponseData>(
|
* 普通的请求方法
|
||||||
/**
|
*/
|
||||||
* 请求地址
|
export type AxiosDomainRequestMethod = <TData extends AxiosResponseData>(
|
||||||
*/
|
url: string,
|
||||||
url: string,
|
config?: AxiosRequestConfig,
|
||||||
/**
|
) => Promise<AxiosResponse<TData>>;
|
||||||
* 请求配置
|
|
||||||
*/
|
|
||||||
config?: AxiosRequestConfig,
|
|
||||||
): Promise<AxiosResponse<TData>>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface AxiosDomainRequestMethodWithParams {
|
/**
|
||||||
<TData extends AxiosResponseData>(
|
* 带参数的请求方法
|
||||||
/**
|
*/
|
||||||
* 请求地址
|
export type AxiosDomainRequestMethodWithParams = <
|
||||||
*/
|
TData extends AxiosResponseData,
|
||||||
url: string,
|
>(
|
||||||
/**
|
url: string,
|
||||||
* 请求参数
|
params?: AnyObject,
|
||||||
*/
|
config?: AxiosRequestConfig,
|
||||||
params?: AnyObject,
|
) => Promise<AxiosResponse<TData>>;
|
||||||
/**
|
|
||||||
* 请求配置
|
|
||||||
*/
|
|
||||||
config?: AxiosRequestConfig,
|
|
||||||
): Promise<AxiosResponse<TData>>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface AxiosDomainRequestMethodWithData {
|
/**
|
||||||
<TData extends AxiosResponseData>(
|
* 带数据的请求方法
|
||||||
/**
|
*/
|
||||||
* 请求地址
|
export type AxiosDomainRequestMethodWithData = <
|
||||||
*/
|
TData extends AxiosResponseData,
|
||||||
url: string,
|
>(
|
||||||
/**
|
url: string,
|
||||||
* 请求数据
|
data?: AxiosRequestData,
|
||||||
*/
|
config?: AxiosRequestConfig,
|
||||||
data?: AxiosRequestData,
|
) => Promise<AxiosResponse<TData>>;
|
||||||
/**
|
|
||||||
* 请求配置
|
|
||||||
*/
|
|
||||||
config?: AxiosRequestConfig,
|
|
||||||
): Promise<AxiosResponse<TData>>;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 普通的请求方法名称
|
* 普通的请求方法名称
|
||||||
|
@ -187,7 +164,7 @@ for (const method of requestMethodWithParamsNames) {
|
||||||
config = {},
|
config = {},
|
||||||
) {
|
) {
|
||||||
config.method = method;
|
config.method = method;
|
||||||
config.params = config.params ? deepMerge(params, config.params) : params;
|
config.params = deepMerge(params, config.params ?? {});
|
||||||
return this.request(url, config);
|
return this.request(url, config);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -195,11 +172,11 @@ for (const method of requestMethodWithParamsNames) {
|
||||||
for (const method of requestMethodWithDataNames) {
|
for (const method of requestMethodWithDataNames) {
|
||||||
AxiosDomain.prototype[method] = function processRequestMethodWithData(
|
AxiosDomain.prototype[method] = function processRequestMethodWithData(
|
||||||
url,
|
url,
|
||||||
data = {},
|
data,
|
||||||
config = {},
|
config = {},
|
||||||
) {
|
) {
|
||||||
config.method = method;
|
config.method = method;
|
||||||
config.data = config.data ? deepMerge(data, config.data) : data;
|
config.data = data;
|
||||||
return this.request(url, config);
|
return this.request(url, config);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,12 @@
|
||||||
export interface CancelAction {
|
export interface CancelAction {
|
||||||
(
|
/**
|
||||||
/**
|
* 取消信息
|
||||||
* 取消信息
|
*/
|
||||||
*/
|
(message?: string): void;
|
||||||
message?: string,
|
|
||||||
): void;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CancelExecutor {
|
export interface CancelExecutor {
|
||||||
(
|
(cancel: CancelAction): void;
|
||||||
/**
|
|
||||||
* 取消函数
|
|
||||||
*/
|
|
||||||
cancel: CancelAction,
|
|
||||||
): void;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CancelTokenSource {
|
export interface CancelTokenSource {
|
||||||
|
|
|
@ -1,19 +1,19 @@
|
||||||
import { cleanStack } from '../helpers/error';
|
import { cleanStack } from '../helpers/error';
|
||||||
import { AxiosAdapterTask } from '../adapter';
|
import { AxiosAdapterPlatformTask } from '../adapter';
|
||||||
import { AxiosRequestConfig, AxiosResponse, AxiosResponseError } from './Axios';
|
import { AxiosRequestConfig, AxiosResponse, AxiosResponseError } from './Axios';
|
||||||
|
|
||||||
export type AxiosErrorResponse = AxiosResponse | AxiosResponseError;
|
export type AxiosErrorResponse = AxiosResponse | AxiosResponseError;
|
||||||
|
|
||||||
class AxiosError extends Error {
|
class AxiosError extends Error {
|
||||||
config: AxiosRequestConfig;
|
config: AxiosRequestConfig;
|
||||||
request: AxiosAdapterTask;
|
request: AxiosAdapterPlatformTask;
|
||||||
response: AxiosErrorResponse;
|
response: AxiosErrorResponse;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
message: string,
|
message: string,
|
||||||
config: AxiosRequestConfig,
|
config: AxiosRequestConfig,
|
||||||
response: AxiosErrorResponse,
|
response: AxiosErrorResponse,
|
||||||
request: AxiosAdapterTask,
|
request: AxiosAdapterPlatformTask,
|
||||||
) {
|
) {
|
||||||
super(message);
|
super(message);
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ export function createError(
|
||||||
message: string,
|
message: string,
|
||||||
config: AxiosRequestConfig,
|
config: AxiosRequestConfig,
|
||||||
response: AxiosErrorResponse,
|
response: AxiosErrorResponse,
|
||||||
request: AxiosAdapterTask,
|
request: AxiosAdapterPlatformTask,
|
||||||
) {
|
) {
|
||||||
const axiosError = new AxiosError(message, config, response, request);
|
const axiosError = new AxiosError(message, config, response, request);
|
||||||
cleanStack(axiosError);
|
cleanStack(axiosError);
|
||||||
|
|
|
@ -9,13 +9,6 @@ import { transformURL } from './transformURL';
|
||||||
import { AxiosErrorResponse } from './createError';
|
import { AxiosErrorResponse } from './createError';
|
||||||
import { requestMethodWithDataNames } from './AxiosDomain';
|
import { requestMethodWithDataNames } from './AxiosDomain';
|
||||||
|
|
||||||
function throwIfCancellationRequested(config: AxiosRequestConfig) {
|
|
||||||
const { cancelToken } = config;
|
|
||||||
if (isCancelToken(cancelToken)) {
|
|
||||||
cancelToken.throwIfRequested();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 可以携带 data 的请求方法
|
* 可以携带 data 的请求方法
|
||||||
*/
|
*/
|
||||||
|
@ -24,6 +17,13 @@ const requestMethodWithDataRE = new RegExp(
|
||||||
'i',
|
'i',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 发送请求
|
||||||
|
*
|
||||||
|
* 校验配置,转换配置,转换数据,捕获取消请求。
|
||||||
|
*
|
||||||
|
* @param config 请求配置
|
||||||
|
*/
|
||||||
export function dispatchRequest(config: AxiosRequestConfig) {
|
export function dispatchRequest(config: AxiosRequestConfig) {
|
||||||
throwIfCancellationRequested(config);
|
throwIfCancellationRequested(config);
|
||||||
|
|
||||||
|
@ -67,3 +67,10 @@ export function dispatchRequest(config: AxiosRequestConfig) {
|
||||||
|
|
||||||
return request(config).then(onSuccess, onError);
|
return request(config).then(onSuccess, onError);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function throwIfCancellationRequested(config: AxiosRequestConfig) {
|
||||||
|
const { cancelToken } = config;
|
||||||
|
if (isCancelToken(cancelToken)) {
|
||||||
|
cancelToken.throwIfRequested();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import {
|
||||||
AxiosAdapterRequestMethod,
|
AxiosAdapterRequestMethod,
|
||||||
AxiosAdapterResponse,
|
AxiosAdapterResponse,
|
||||||
AxiosAdapterResponseError,
|
AxiosAdapterResponseError,
|
||||||
AxiosAdapterTask,
|
AxiosAdapterPlatformTask,
|
||||||
} from '../adapter';
|
} from '../adapter';
|
||||||
import {
|
import {
|
||||||
AxiosProgressCallback,
|
AxiosProgressCallback,
|
||||||
|
@ -16,27 +16,13 @@ import { isCancelToken } from './cancel';
|
||||||
import { AxiosErrorResponse, createError } from './createError';
|
import { AxiosErrorResponse, createError } from './createError';
|
||||||
import { generateType } from './generateType';
|
import { generateType } from './generateType';
|
||||||
|
|
||||||
function tryToggleProgressUpdate(
|
/**
|
||||||
adapterConfig: AxiosAdapterRequestConfig,
|
* 开始请求
|
||||||
progressUpdate?: (callback: AxiosProgressCallback) => void,
|
*
|
||||||
) {
|
* 创建适配器配置并调用适配器,监听取消请求,注册监听回调,处理响应体和错误体,抛出异常。
|
||||||
const { onUploadProgress, onDownloadProgress } = adapterConfig;
|
*
|
||||||
if (isFunction(progressUpdate)) {
|
* @param config 请求配置
|
||||||
switch (adapterConfig.type) {
|
*/
|
||||||
case 'upload':
|
|
||||||
if (isFunction(onUploadProgress)) {
|
|
||||||
progressUpdate(onUploadProgress);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'download':
|
|
||||||
if (isFunction(onDownloadProgress)) {
|
|
||||||
progressUpdate(onDownloadProgress);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function request(config: AxiosRequestConfig) {
|
export function request(config: AxiosRequestConfig) {
|
||||||
return new Promise<AxiosResponse>((resolve, reject) => {
|
return new Promise<AxiosResponse>((resolve, reject) => {
|
||||||
const { adapter, url, method, cancelToken } = config;
|
const { adapter, url, method, cancelToken } = config;
|
||||||
|
@ -50,7 +36,7 @@ export function request(config: AxiosRequestConfig) {
|
||||||
fail,
|
fail,
|
||||||
};
|
};
|
||||||
|
|
||||||
let adapterTask: AxiosAdapterTask;
|
let adapterTask: AxiosAdapterPlatformTask;
|
||||||
try {
|
try {
|
||||||
adapterTask = adapter!(adapterConfig);
|
adapterTask = adapter!(adapterConfig);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -60,8 +46,8 @@ export function request(config: AxiosRequestConfig) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function success(adapterResponse: AxiosAdapterResponse): void {
|
function success(baseResponse: AxiosAdapterResponse): void {
|
||||||
const response = adapterResponse as AxiosResponse;
|
const response = baseResponse as AxiosResponse;
|
||||||
response.status = response.status ?? 200;
|
response.status = response.status ?? 200;
|
||||||
response.statusText = response.statusText ?? 'OK';
|
response.statusText = response.statusText ?? 'OK';
|
||||||
response.headers = response.headers ?? {};
|
response.headers = response.headers ?? {};
|
||||||
|
@ -75,8 +61,8 @@ export function request(config: AxiosRequestConfig) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function fail(adapterResponseError: AxiosAdapterResponseError): void {
|
function fail(baseResponseError: AxiosAdapterResponseError): void {
|
||||||
const responseError = adapterResponseError as AxiosResponseError;
|
const responseError = baseResponseError as AxiosResponseError;
|
||||||
responseError.isFail = true;
|
responseError.isFail = true;
|
||||||
responseError.status = responseError.status ?? 400;
|
responseError.status = responseError.status ?? 400;
|
||||||
responseError.statusText = responseError.statusText ?? 'Fail Adapter';
|
responseError.statusText = responseError.statusText ?? 'Fail Adapter';
|
||||||
|
@ -111,3 +97,24 @@ export function request(config: AxiosRequestConfig) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function tryToggleProgressUpdate(
|
||||||
|
adapterConfig: AxiosAdapterRequestConfig,
|
||||||
|
progressUpdate?: (callback: AxiosProgressCallback) => void,
|
||||||
|
) {
|
||||||
|
const { onUploadProgress, onDownloadProgress } = adapterConfig;
|
||||||
|
if (isFunction(progressUpdate)) {
|
||||||
|
switch (adapterConfig.type) {
|
||||||
|
case 'upload':
|
||||||
|
if (isFunction(onUploadProgress)) {
|
||||||
|
progressUpdate(onUploadProgress);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'download':
|
||||||
|
if (isFunction(onDownloadProgress)) {
|
||||||
|
progressUpdate(onDownloadProgress);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { isPlainObject } from '../helpers/isTypes';
|
||||||
import { buildURL } from '../helpers/buildURL';
|
import { buildURL } from '../helpers/buildURL';
|
||||||
import { combineURL } from '../helpers/combineURL';
|
import { combineURL } from '../helpers/combineURL';
|
||||||
import { dynamicURL } from '../helpers/dynamicURL';
|
import { dynamicURL } from '../helpers/dynamicURL';
|
||||||
|
@ -8,7 +9,11 @@ export function transformURL(config: AxiosRequestConfig) {
|
||||||
let url = config.url ?? '';
|
let url = config.url ?? '';
|
||||||
|
|
||||||
if (!isAbsoluteURL(url)) url = combineURL(config.baseURL ?? '', url);
|
if (!isAbsoluteURL(url)) url = combineURL(config.baseURL ?? '', url);
|
||||||
url = dynamicURL(url, config.params, config.data);
|
url = dynamicURL(
|
||||||
|
url,
|
||||||
|
config.params,
|
||||||
|
isPlainObject(config.data) ? config.data : {},
|
||||||
|
);
|
||||||
url = buildURL(url, config.params, config.paramsSerializer);
|
url = buildURL(url, config.params, config.paramsSerializer);
|
||||||
|
|
||||||
return url;
|
return url;
|
||||||
|
|
|
@ -19,14 +19,14 @@ export type {
|
||||||
AxiosAdapterResponse,
|
AxiosAdapterResponse,
|
||||||
AxiosAdapterResponseData,
|
AxiosAdapterResponseData,
|
||||||
AxiosAdapterResponseError,
|
AxiosAdapterResponseError,
|
||||||
AxiosAdapterPlatform,
|
|
||||||
AxiosAdapterRequest,
|
AxiosAdapterRequest,
|
||||||
AxiosAdapterRequestOptions,
|
AxiosAdapterRequestOptions,
|
||||||
AxiosAdapterDownload,
|
AxiosAdapterDownload,
|
||||||
AxiosAdapterDownloadOptions,
|
AxiosAdapterDownloadOptions,
|
||||||
AxiosAdapterUpload,
|
AxiosAdapterUpload,
|
||||||
AxiosAdapterUploadOptions,
|
AxiosAdapterUploadOptions,
|
||||||
AxiosAdapterTask,
|
AxiosAdapterPlatform,
|
||||||
|
AxiosAdapterPlatformTask,
|
||||||
} from './adapter';
|
} from './adapter';
|
||||||
export type {
|
export type {
|
||||||
AxiosInstanceDefaults,
|
AxiosInstanceDefaults,
|
||||||
|
|
|
@ -196,33 +196,20 @@ describe('src/core/AxiosDomain.ts', () => {
|
||||||
requestMethodWithParamsNames.forEach((k) => a[k]('test', p, c));
|
requestMethodWithParamsNames.forEach((k) => a[k]('test', p, c));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('应该支持深度合并 data', () => {
|
test('应该只取传入的 data', () => {
|
||||||
const ds = {
|
const ds = {
|
||||||
baseURL: 'http://api.com',
|
baseURL: 'http://api.com',
|
||||||
};
|
};
|
||||||
const d = {
|
const d = {
|
||||||
v1: 1,
|
v: 1,
|
||||||
v2: {
|
|
||||||
v1: 1,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
const c = {
|
const c = {
|
||||||
data: {
|
v: 2,
|
||||||
v2: {
|
|
||||||
v2: 2,
|
|
||||||
},
|
|
||||||
v3: 3,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const a = new AxiosDomain(ds, async (config) => {
|
const a = new AxiosDomain(ds, async (config) => {
|
||||||
expect(config.data).toEqual({
|
expect(config.data).toEqual({
|
||||||
v1: 1,
|
v: 1,
|
||||||
v2: {
|
|
||||||
v1: 1,
|
|
||||||
v2: 2,
|
|
||||||
},
|
|
||||||
v3: 3,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return {} as AxiosResponse;
|
return {} as AxiosResponse;
|
||||||
|
@ -230,4 +217,36 @@ describe('src/core/AxiosDomain.ts', () => {
|
||||||
|
|
||||||
requestMethodWithDataNames.forEach((k) => a[k]('test', d, c));
|
requestMethodWithDataNames.forEach((k) => a[k]('test', d, c));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('应该支持多种类型 data', () => {
|
||||||
|
const ds = {
|
||||||
|
baseURL: 'http://api.com',
|
||||||
|
};
|
||||||
|
|
||||||
|
const str = '11';
|
||||||
|
const obj = {};
|
||||||
|
const buff = new ArrayBuffer(0);
|
||||||
|
|
||||||
|
const testStr = new AxiosDomain(ds, async (config) => {
|
||||||
|
expect(config.data).toBe(str);
|
||||||
|
|
||||||
|
return {} as AxiosResponse;
|
||||||
|
});
|
||||||
|
const testObj = new AxiosDomain(ds, async (config) => {
|
||||||
|
expect(config.data).toBe(obj);
|
||||||
|
|
||||||
|
return {} as AxiosResponse;
|
||||||
|
});
|
||||||
|
const testBuff = new AxiosDomain(ds, async (config) => {
|
||||||
|
expect(config.data).toBe(buff);
|
||||||
|
|
||||||
|
return {} as AxiosResponse;
|
||||||
|
});
|
||||||
|
|
||||||
|
requestMethodWithDataNames.forEach((k) => {
|
||||||
|
testStr[k]('test', str);
|
||||||
|
testObj[k]('test', obj);
|
||||||
|
testBuff[k]('test', buff);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -7,7 +7,13 @@ import {
|
||||||
asyncTimeout,
|
asyncTimeout,
|
||||||
} from 'scripts/test.utils';
|
} from 'scripts/test.utils';
|
||||||
import axios from 'src/axios';
|
import axios from 'src/axios';
|
||||||
import { Cancel, isCancel, CancelToken, isCancelToken } from '@/core/cancel';
|
import {
|
||||||
|
Cancel,
|
||||||
|
isCancel,
|
||||||
|
CancelToken,
|
||||||
|
isCancelToken,
|
||||||
|
CancelAction,
|
||||||
|
} from '@/core/cancel';
|
||||||
|
|
||||||
describe('src/helpers/cancel.ts', () => {
|
describe('src/helpers/cancel.ts', () => {
|
||||||
test('应该支持空参数', () => {
|
test('应该支持空参数', () => {
|
||||||
|
@ -31,7 +37,7 @@ describe('src/helpers/cancel.ts', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('应该可以取消', () => {
|
test('应该可以取消', () => {
|
||||||
let ca!: () => void;
|
let ca!: CancelAction;
|
||||||
const ct = new CancelToken((a) => (ca = a));
|
const ct = new CancelToken((a) => (ca = a));
|
||||||
|
|
||||||
expect(ct.throwIfRequested()).toBeUndefined();
|
expect(ct.throwIfRequested()).toBeUndefined();
|
||||||
|
|
Loading…
Reference in New Issue