fix: 下载进度/上传进度 类型错误
parent
1867273460
commit
2d691b69cb
|
@ -71,14 +71,14 @@ axios('https://api.com/test', {
|
||||||
},
|
},
|
||||||
onDownloadProgress(event) {
|
onDownloadProgress(event) {
|
||||||
const {
|
const {
|
||||||
// 下载进度
|
// 下载进度百分比
|
||||||
progress,
|
progress,
|
||||||
|
|
||||||
// 已经下载的数据长度
|
// 已经下载的数据长度,单位 Bytes
|
||||||
totalBytesSent,
|
totalBytesWritten,
|
||||||
|
|
||||||
// 预期需要下载的数据总长度
|
// 预预期需要下载的数据总长度,单位 Bytes
|
||||||
totalBytesExpectedToSend,
|
totalBytesExpectedToWrite,
|
||||||
} = event;
|
} = event;
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
|
@ -101,13 +101,13 @@ axios('https://api.com/test', {
|
||||||
},
|
},
|
||||||
onUploadProgress(event) {
|
onUploadProgress(event) {
|
||||||
const {
|
const {
|
||||||
// 上传进度
|
// 上传进度百分比
|
||||||
progress,
|
progress,
|
||||||
|
|
||||||
// 已经上传的数据长度
|
// 已经上传的数据长度,单位 Bytes
|
||||||
totalBytesSent,
|
totalBytesSent,
|
||||||
|
|
||||||
// 预期需要上传的数据总长度
|
// 预期需要上传的数据总长度,单位 Bytes
|
||||||
totalBytesExpectedToSend,
|
totalBytesExpectedToSend,
|
||||||
} = event;
|
} = event;
|
||||||
},
|
},
|
||||||
|
|
|
@ -80,7 +80,6 @@ axios('test');
|
||||||
const {
|
const {
|
||||||
// 静态对象
|
// 静态对象
|
||||||
// 注意:默认导出的 axios 在 CommonJS 里是以 default 属性的方式存在
|
// 注意:默认导出的 axios 在 CommonJS 里是以 default 属性的方式存在
|
||||||
// import axios from 'axios-miniprogram' 等于 const axios = require('axios-miniprogram').default
|
|
||||||
default: axios,
|
default: axios,
|
||||||
|
|
||||||
// 取消令牌
|
// 取消令牌
|
||||||
|
@ -125,6 +124,36 @@ const {
|
||||||
|
|
||||||
::::
|
::::
|
||||||
|
|
||||||
|
不同的模块系统存在一些小差异,`esm` 会自动处理默认导入,但 `cjs` 不会处理默认导入。
|
||||||
|
|
||||||
|
```ts
|
||||||
|
// 默认导入,esm 和 cjs 这两种写法是等价关系
|
||||||
|
import axios from 'axios-miniprogram';
|
||||||
|
const axios = require('axios-miniprogram').default;
|
||||||
|
|
||||||
|
// 别名导入,esm 和 cjs 这两种写法是等价关系
|
||||||
|
import * as axios from 'axios-miniprogram';
|
||||||
|
const axios = require('axios-miniprogram');
|
||||||
|
|
||||||
|
// 具名导入,esm 和 cjs 这两种写法是等价关系
|
||||||
|
import {
|
||||||
|
default as axios,
|
||||||
|
CancelToken,
|
||||||
|
isCancel,
|
||||||
|
Axios,
|
||||||
|
isAxiosError,
|
||||||
|
createAdapter,
|
||||||
|
} from 'axios-miniprogram';
|
||||||
|
const {
|
||||||
|
default: axios,
|
||||||
|
CancelToken,
|
||||||
|
isCancel,
|
||||||
|
Axios,
|
||||||
|
isAxiosError,
|
||||||
|
createAdapter,
|
||||||
|
} = require('axios-miniprogram');
|
||||||
|
```
|
||||||
|
|
||||||
## 使用
|
## 使用
|
||||||
|
|
||||||
### `axios(url, config?)`
|
### `axios(url, config?)`
|
||||||
|
|
|
@ -113,8 +113,14 @@ export function mockAdapterFail(options: MockAdapterOptions = {}) {
|
||||||
return mockAdapterBase('fail', options);
|
return mockAdapterBase('fail', options);
|
||||||
}
|
}
|
||||||
|
|
||||||
export const testEachMethods = test.each([
|
export const methods = [
|
||||||
...requestMethodNames,
|
...requestMethodNames,
|
||||||
...requestMethodWithParamsNames,
|
...requestMethodWithParamsNames,
|
||||||
...requestMethodWithDataNames,
|
...requestMethodWithDataNames,
|
||||||
]);
|
];
|
||||||
|
|
||||||
|
export const testEachMethods = test.each(methods);
|
||||||
|
|
||||||
|
export function eachMethods(cb: (k: (typeof methods)[number]) => void) {
|
||||||
|
methods.forEach(cb);
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { isFunction, isPlainObject } from './helpers/isTypes';
|
import { isFunction, isPlainObject } from './helpers/isTypes';
|
||||||
import { assert } from './helpers/error';
|
import { assert } from './helpers/error';
|
||||||
import {
|
import {
|
||||||
AxiosProgressCallback,
|
AxiosProgressEvent,
|
||||||
AxiosRequestFormData,
|
AxiosRequestFormData,
|
||||||
AxiosRequestHeaders,
|
AxiosRequestHeaders,
|
||||||
} from './core/Axios';
|
} from './core/Axios';
|
||||||
|
@ -229,8 +229,8 @@ export type AxiosAdapterPlatformTask =
|
||||||
| void
|
| void
|
||||||
| {
|
| {
|
||||||
abort?(): void;
|
abort?(): void;
|
||||||
onProgressUpdate?(callback: AxiosProgressCallback): void;
|
onProgressUpdate?(callback: (event: AxiosProgressEvent) => void): void;
|
||||||
offProgressUpdate?(callback: AxiosProgressCallback): void;
|
offProgressUpdate?(callback: (event: AxiosProgressEvent) => void): void;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { buildURL } from '../helpers/buildURL';
|
import { buildURL } from '../helpers/buildURL';
|
||||||
import { isAbsoluteURL } from '../helpers/isAbsoluteURL';
|
import { isAbsoluteURL } from '../helpers/isAbsoluteURL';
|
||||||
import { combineURL } from '../helpers/combineURL';
|
import { combineURL } from '../helpers/combineURL';
|
||||||
import { isFunction, isPromise, isString } from '../helpers/isTypes';
|
import { isString } from '../helpers/isTypes';
|
||||||
import {
|
import {
|
||||||
AxiosAdapter,
|
AxiosAdapter,
|
||||||
AxiosAdapterRequestMethod,
|
AxiosAdapterRequestMethod,
|
||||||
|
@ -102,33 +102,55 @@ export type AxiosRequestData =
|
||||||
export type AxiosResponseData = number | AxiosAdapterResponseData;
|
export type AxiosResponseData = number | AxiosAdapterResponseData;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 监听进度回调事件对象
|
* 进度对象
|
||||||
*/
|
*/
|
||||||
export interface AxiosProgressEvent {
|
export interface AxiosProgressEvent extends AnyObject {
|
||||||
/**
|
/**
|
||||||
* 下载进度
|
* 上传进度百分比
|
||||||
*/
|
*/
|
||||||
progress: number;
|
progress: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载进度对象
|
||||||
|
*/
|
||||||
|
export interface AxiosDownloadProgressEvent extends AxiosProgressEvent {
|
||||||
/**
|
/**
|
||||||
* 已经下载的数据长度
|
* 已经下载的数据长度,单位 Bytes
|
||||||
|
*/
|
||||||
|
totalBytesWritten: number;
|
||||||
|
/**
|
||||||
|
* 预预期需要下载的数据总长度,单位 Bytes
|
||||||
|
*/
|
||||||
|
totalBytesExpectedToWrite: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 监听下载进度
|
||||||
|
*/
|
||||||
|
export interface AxiosDownloadProgressCallback {
|
||||||
|
(event: AxiosDownloadProgressEvent): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传进度对象
|
||||||
|
*/
|
||||||
|
export interface AxiosUploadProgressEvent extends AxiosProgressEvent {
|
||||||
|
/**
|
||||||
|
* 已经上传的数据长度,单位 Bytes
|
||||||
*/
|
*/
|
||||||
totalBytesSent: number;
|
totalBytesSent: number;
|
||||||
/**
|
/**
|
||||||
* 预期需要下载的数据总长度
|
* 预期需要上传的数据总长度,单位 Bytes
|
||||||
*/
|
*/
|
||||||
totalBytesExpectedToSend: number;
|
totalBytesExpectedToSend: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 监听进度回调
|
* 监听上传进度
|
||||||
*/
|
*/
|
||||||
export interface AxiosProgressCallback {
|
export interface AxiosUploadProgressCallback {
|
||||||
(
|
(event: AxiosUploadProgressEvent): void;
|
||||||
/**
|
|
||||||
* 事件对象
|
|
||||||
*/
|
|
||||||
event: AxiosProgressEvent,
|
|
||||||
): void;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -197,15 +219,15 @@ export interface AxiosRequestConfig
|
||||||
/**
|
/**
|
||||||
* 错误处理
|
* 错误处理
|
||||||
*/
|
*/
|
||||||
errorHandler?: (error: unknown) => Promise<void> | void;
|
errorHandler?: (error: unknown) => Promise<AxiosResponse>;
|
||||||
/**
|
|
||||||
* 监听上传进度
|
|
||||||
*/
|
|
||||||
onUploadProgress?: AxiosProgressCallback;
|
|
||||||
/**
|
/**
|
||||||
* 监听下载进度
|
* 监听下载进度
|
||||||
*/
|
*/
|
||||||
onDownloadProgress?: AxiosProgressCallback;
|
onDownloadProgress?: AxiosUploadProgressCallback;
|
||||||
|
/**
|
||||||
|
* 监听上传进度
|
||||||
|
*/
|
||||||
|
onUploadProgress?: AxiosUploadProgressCallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -321,40 +343,34 @@ export default class Axios extends AxiosDomain {
|
||||||
};
|
};
|
||||||
|
|
||||||
#processRequest(config: AxiosRequestConfig) {
|
#processRequest(config: AxiosRequestConfig) {
|
||||||
const chain: [
|
const requestHandler = {
|
||||||
Interceptor<AxiosRequestConfig> | Interceptor<AxiosResponse>,
|
|
||||||
] = [
|
|
||||||
{
|
|
||||||
resolved: dispatchRequest,
|
resolved: dispatchRequest,
|
||||||
},
|
};
|
||||||
];
|
const errorHandler = {
|
||||||
|
rejected: config.errorHandler,
|
||||||
|
};
|
||||||
|
const chain: (
|
||||||
|
| Partial<Interceptor<AxiosRequestConfig>>
|
||||||
|
| Partial<Interceptor<AxiosResponse>>
|
||||||
|
)[] = [];
|
||||||
|
|
||||||
this.interceptors.request.forEach(chain.unshift.bind(chain));
|
this.interceptors.request.forEach((requestInterceptor) => {
|
||||||
this.interceptors.response.forEach(chain.push.bind(chain));
|
chain.unshift(requestInterceptor);
|
||||||
|
});
|
||||||
|
chain.push(requestHandler);
|
||||||
|
this.interceptors.response.forEach((responseInterceptor) => {
|
||||||
|
chain.push(responseInterceptor);
|
||||||
|
});
|
||||||
|
chain.push(errorHandler);
|
||||||
|
|
||||||
let next = Promise.resolve(config);
|
return chain.reduce(
|
||||||
for (const { resolved, rejected } of chain) {
|
(next, { resolved, rejected }) =>
|
||||||
next = next.then(
|
next.then(
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
resolved,
|
resolved,
|
||||||
rejected,
|
rejected,
|
||||||
);
|
),
|
||||||
}
|
Promise.resolve(config),
|
||||||
|
) as Promise<AxiosResponse>;
|
||||||
// 错误处理
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
|
|
||||||
return next as Promise<AxiosResponse>;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,12 +6,7 @@ import {
|
||||||
AxiosAdapterResponseError,
|
AxiosAdapterResponseError,
|
||||||
AxiosAdapterPlatformTask,
|
AxiosAdapterPlatformTask,
|
||||||
} from '../adapter';
|
} from '../adapter';
|
||||||
import {
|
import { AxiosRequestConfig, AxiosResponse, AxiosResponseError } from './Axios';
|
||||||
AxiosProgressCallback,
|
|
||||||
AxiosRequestConfig,
|
|
||||||
AxiosResponse,
|
|
||||||
AxiosResponseError,
|
|
||||||
} from './Axios';
|
|
||||||
import { isCancelToken } from './cancel';
|
import { isCancelToken } from './cancel';
|
||||||
import { AxiosErrorResponse, createError } from './createError';
|
import { AxiosErrorResponse, createError } from './createError';
|
||||||
import { generateType } from './generateType';
|
import { generateType } from './generateType';
|
||||||
|
@ -103,7 +98,7 @@ export function request(config: AxiosRequestConfig) {
|
||||||
|
|
||||||
function tryToggleProgressUpdate(
|
function tryToggleProgressUpdate(
|
||||||
adapterConfig: AxiosAdapterRequestConfig,
|
adapterConfig: AxiosAdapterRequestConfig,
|
||||||
adapterProgress?: (callback: AxiosProgressCallback) => void,
|
adapterProgress?: (cb: (event: AnyObject) => void) => void,
|
||||||
) {
|
) {
|
||||||
const { onUploadProgress, onDownloadProgress } = adapterConfig;
|
const { onUploadProgress, onDownloadProgress } = adapterConfig;
|
||||||
if (isFunction(adapterProgress)) {
|
if (isFunction(adapterProgress)) {
|
||||||
|
|
|
@ -30,10 +30,3 @@ export function isDate(date: any): date is Date {
|
||||||
export function isFunction<T extends Function>(value: any): value is T {
|
export function isFunction<T extends Function>(value: any): value is T {
|
||||||
return typeof value === 'function';
|
return typeof value === 'function';
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isPromise<T = unknown>(value: any): value is Promise<T> {
|
|
||||||
return (
|
|
||||||
_toString.call(value) === '[object Promise]' ||
|
|
||||||
(isPlainObject(value) && isFunction(value.then))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
|
@ -9,8 +9,10 @@ export type {
|
||||||
AxiosResponse,
|
AxiosResponse,
|
||||||
AxiosResponseData,
|
AxiosResponseData,
|
||||||
AxiosResponseError,
|
AxiosResponseError,
|
||||||
AxiosProgressEvent,
|
AxiosDownloadProgressEvent,
|
||||||
AxiosProgressCallback,
|
AxiosDownloadProgressCallback,
|
||||||
|
AxiosUploadProgressEvent,
|
||||||
|
AxiosUploadProgressCallback,
|
||||||
} from './core/Axios';
|
} from './core/Axios';
|
||||||
export type {
|
export type {
|
||||||
AxiosAdapter,
|
AxiosAdapter,
|
||||||
|
|
|
@ -2,14 +2,10 @@ import { describe, test, expect } from 'vitest';
|
||||||
import Axios from '@/core/Axios';
|
import Axios from '@/core/Axios';
|
||||||
import { CancelToken, isCancel } from '@/core/cancel';
|
import { CancelToken, isCancel } from '@/core/cancel';
|
||||||
import { isAxiosError } from '@/core/createError';
|
import { isAxiosError } from '@/core/createError';
|
||||||
import {
|
|
||||||
requestMethodNames,
|
|
||||||
requestMethodWithDataNames,
|
|
||||||
requestMethodWithParamsNames,
|
|
||||||
} from '@/core/AxiosDomain';
|
|
||||||
import { createAdapter } from '@/adapter';
|
import { createAdapter } from '@/adapter';
|
||||||
import axios from '@/axios';
|
import axios from '@/axios';
|
||||||
import defaults from '@/defaults';
|
import defaults from '@/defaults';
|
||||||
|
import { eachMethods } from 'scripts/test.utils';
|
||||||
|
|
||||||
describe('src/axios.ts', () => {
|
describe('src/axios.ts', () => {
|
||||||
test('应该有这些静态属性', () => {
|
test('应该有这些静态属性', () => {
|
||||||
|
@ -35,11 +31,7 @@ describe('src/axios.ts', () => {
|
||||||
expect(instance.fork).toBeTypeOf('function');
|
expect(instance.fork).toBeTypeOf('function');
|
||||||
expect(instance.request).toBeTypeOf('function');
|
expect(instance.request).toBeTypeOf('function');
|
||||||
|
|
||||||
[
|
eachMethods((k) => {
|
||||||
...requestMethodNames,
|
|
||||||
...requestMethodWithParamsNames,
|
|
||||||
...requestMethodWithDataNames,
|
|
||||||
].forEach((k) => {
|
|
||||||
expect(instance[k]).toBeTypeOf('function');
|
expect(instance[k]).toBeTypeOf('function');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,11 +1,7 @@
|
||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
import {
|
|
||||||
requestMethodNames,
|
|
||||||
requestMethodWithDataNames,
|
|
||||||
requestMethodWithParamsNames,
|
|
||||||
} from '@/core/AxiosDomain';
|
|
||||||
import axios from '@/axios';
|
import axios from '@/axios';
|
||||||
import defaults from '@/defaults';
|
import defaults from '@/defaults';
|
||||||
|
import { testEachMethods } from 'scripts/test.utils';
|
||||||
|
|
||||||
describe('src/axios.ts', () => {
|
describe('src/axios.ts', () => {
|
||||||
test('应该有这些实例属性及方法', () => {
|
test('应该有这些实例属性及方法', () => {
|
||||||
|
@ -14,13 +10,9 @@ describe('src/axios.ts', () => {
|
||||||
expect(axios.getUri).toBeTypeOf('function');
|
expect(axios.getUri).toBeTypeOf('function');
|
||||||
expect(axios.fork).toBeTypeOf('function');
|
expect(axios.fork).toBeTypeOf('function');
|
||||||
expect(axios.request).toBeTypeOf('function');
|
expect(axios.request).toBeTypeOf('function');
|
||||||
|
});
|
||||||
|
|
||||||
[
|
testEachMethods('%s 应该是一个函数', (k) => {
|
||||||
...requestMethodNames,
|
|
||||||
...requestMethodWithParamsNames,
|
|
||||||
...requestMethodWithDataNames,
|
|
||||||
].forEach((k) => {
|
|
||||||
expect(axios[k]).toBeTypeOf('function');
|
expect(axios[k]).toBeTypeOf('function');
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -3,11 +3,12 @@ import {
|
||||||
mockAdapter,
|
mockAdapter,
|
||||||
mockAdapterError,
|
mockAdapterError,
|
||||||
mockAdapterFail,
|
mockAdapterFail,
|
||||||
|
testEachMethods,
|
||||||
} from 'scripts/test.utils';
|
} from 'scripts/test.utils';
|
||||||
import AxiosDomain, {
|
import AxiosDomain, {
|
||||||
requestMethodNames,
|
requestMethodNames,
|
||||||
requestMethodWithDataNames,
|
|
||||||
requestMethodWithParamsNames,
|
requestMethodWithParamsNames,
|
||||||
|
requestMethodWithDataNames,
|
||||||
} from '@/core/AxiosDomain';
|
} from '@/core/AxiosDomain';
|
||||||
import Axios from '@/core/Axios';
|
import Axios from '@/core/Axios';
|
||||||
import axios from '@/axios';
|
import axios from '@/axios';
|
||||||
|
@ -34,14 +35,10 @@ describe('src/core/Axios.ts', () => {
|
||||||
expect(axiosObj.request).toBeTypeOf('function');
|
expect(axiosObj.request).toBeTypeOf('function');
|
||||||
expect(axiosObj.getUri).toBeTypeOf('function');
|
expect(axiosObj.getUri).toBeTypeOf('function');
|
||||||
expect(axiosObj.fork).toBeTypeOf('function');
|
expect(axiosObj.fork).toBeTypeOf('function');
|
||||||
|
|
||||||
[
|
|
||||||
...requestMethodNames,
|
|
||||||
...requestMethodWithParamsNames,
|
|
||||||
...requestMethodWithDataNames,
|
|
||||||
].forEach((k) => {
|
|
||||||
expect(axiosObj[k]).toBeTypeOf('function');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testEachMethods('%s 应该是一个函数', (k) => {
|
||||||
|
expect(axiosObj[k]).toBeTypeOf('function');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('应该可以发送普通别名请求', () => {
|
test('应该可以发送普通别名请求', () => {
|
||||||
|
@ -162,6 +159,7 @@ describe('src/core/Axios.ts', () => {
|
||||||
url: 'test',
|
url: 'test',
|
||||||
errorHandler: async (err: unknown) => {
|
errorHandler: async (err: unknown) => {
|
||||||
e1(err);
|
e1(err);
|
||||||
|
return Promise.reject(err);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const c2 = {
|
const c2 = {
|
||||||
|
@ -169,6 +167,7 @@ describe('src/core/Axios.ts', () => {
|
||||||
url: 'test',
|
url: 'test',
|
||||||
errorHandler: async (err: unknown) => {
|
errorHandler: async (err: unknown) => {
|
||||||
e2(err);
|
e2(err);
|
||||||
|
return Promise.reject(err);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ import AxiosDomain, {
|
||||||
requestMethodWithDataNames,
|
requestMethodWithDataNames,
|
||||||
} from '@/core/AxiosDomain';
|
} from '@/core/AxiosDomain';
|
||||||
import { AxiosResponse } from '@/core/Axios';
|
import { AxiosResponse } from '@/core/Axios';
|
||||||
|
import { eachMethods } from 'scripts/test.utils';
|
||||||
|
|
||||||
describe('src/core/AxiosDomain.ts', () => {
|
describe('src/core/AxiosDomain.ts', () => {
|
||||||
test('应该有这些常量', () => {
|
test('应该有这些常量', () => {
|
||||||
|
@ -23,11 +24,7 @@ describe('src/core/AxiosDomain.ts', () => {
|
||||||
expect(a.defaults).toEqual(c);
|
expect(a.defaults).toEqual(c);
|
||||||
expect(a.request).toBeTypeOf('function');
|
expect(a.request).toBeTypeOf('function');
|
||||||
|
|
||||||
[
|
eachMethods((k) => {
|
||||||
...requestMethodNames,
|
|
||||||
...requestMethodWithParamsNames,
|
|
||||||
...requestMethodWithDataNames,
|
|
||||||
].forEach((k) => {
|
|
||||||
expect(a[k]).toBeTypeOf('function');
|
expect(a[k]).toBeTypeOf('function');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { describe, test, expect, vi } from 'vitest';
|
import { describe, test, expect, vi } from 'vitest';
|
||||||
import { asyncNext, mockAdapter } from 'scripts/test.utils';
|
import { asyncNext, mockAdapter, testEachMethods } from 'scripts/test.utils';
|
||||||
import { dispatchRequest } from '@/core/dispatchRequest';
|
import { dispatchRequest } from '@/core/dispatchRequest';
|
||||||
import {
|
import {
|
||||||
requestMethodNames,
|
requestMethodNames,
|
||||||
|
@ -38,11 +38,7 @@ describe('src/core/dispatchRequest.ts', () => {
|
||||||
).not.toThrowError();
|
).not.toThrowError();
|
||||||
});
|
});
|
||||||
|
|
||||||
test.each([
|
testEachMethods('应该支持 %s 转全大写', (k) => {
|
||||||
...requestMethodNames,
|
|
||||||
...requestMethodWithDataNames,
|
|
||||||
...requestMethodWithParamsNames,
|
|
||||||
])('应该支持 %s 转全大写', (k) => {
|
|
||||||
const c = {
|
const c = {
|
||||||
adapter: mockAdapter(),
|
adapter: mockAdapter(),
|
||||||
url: '/',
|
url: '/',
|
||||||
|
|
|
@ -1,20 +1,11 @@
|
||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
import { flattenHeaders } from '@/core/flattenHeaders';
|
import { flattenHeaders } from '@/core/flattenHeaders';
|
||||||
import {
|
import { eachMethods, methods } from 'scripts/test.utils';
|
||||||
requestMethodNames,
|
|
||||||
requestMethodWithDataNames,
|
|
||||||
requestMethodWithParamsNames,
|
|
||||||
} from '@/core/AxiosDomain';
|
|
||||||
|
|
||||||
describe('src/core/flattenHeaders.ts', () => {
|
describe('src/core/flattenHeaders.ts', () => {
|
||||||
const keys = [
|
|
||||||
...requestMethodNames,
|
|
||||||
...requestMethodWithParamsNames,
|
|
||||||
...requestMethodWithDataNames,
|
|
||||||
];
|
|
||||||
const baseHeaders = Object.fromEntries(
|
const baseHeaders = Object.fromEntries(
|
||||||
keys.map((k) => [k, { v1: `${k}1`, v2: `${k}2` }]),
|
methods.map((k) => [k, { v1: `${k}1`, v2: `${k}2` }]),
|
||||||
) as unknown as Record<(typeof keys)[number], AnyObject>;
|
) as unknown as Record<(typeof methods)[number], AnyObject>;
|
||||||
|
|
||||||
test('应该支持空配置', () => {
|
test('应该支持空配置', () => {
|
||||||
expect(flattenHeaders({})).toEqual({});
|
expect(flattenHeaders({})).toEqual({});
|
||||||
|
@ -33,7 +24,7 @@ describe('src/core/flattenHeaders.ts', () => {
|
||||||
const h2 = { v1: 1, v2: 2 };
|
const h2 = { v1: 1, v2: 2 };
|
||||||
const h3 = { ...h1, ...h2 };
|
const h3 = { ...h1, ...h2 };
|
||||||
|
|
||||||
keys.forEach((a) => {
|
eachMethods((a) => {
|
||||||
expect(flattenHeaders({ headers: h1, method: a })).toEqual(h1[a]);
|
expect(flattenHeaders({ headers: h1, method: a })).toEqual(h1[a]);
|
||||||
expect(flattenHeaders({ headers: h3, method: a })).toEqual(h2);
|
expect(flattenHeaders({ headers: h3, method: a })).toEqual(h2);
|
||||||
});
|
});
|
||||||
|
@ -48,14 +39,14 @@ describe('src/core/flattenHeaders.ts', () => {
|
||||||
};
|
};
|
||||||
const h2 = { ...baseHeaders, ...h1 };
|
const h2 = { ...baseHeaders, ...h1 };
|
||||||
|
|
||||||
keys.forEach((a) => {
|
eachMethods((a) => {
|
||||||
expect(flattenHeaders({ headers: h1, method: a })).toEqual(h1.common);
|
expect(flattenHeaders({ headers: h1, method: a })).toEqual(h1.common);
|
||||||
expect(flattenHeaders({ headers: h2, method: a })).toEqual(h2[a]);
|
expect(flattenHeaders({ headers: h2, method: a })).toEqual(h2[a]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test.each(
|
test.each(
|
||||||
keys.map((k) => [
|
methods.map((k) => [
|
||||||
k,
|
k,
|
||||||
{
|
{
|
||||||
common: {
|
common: {
|
||||||
|
@ -83,7 +74,7 @@ describe('src/core/flattenHeaders.ts', () => {
|
||||||
v4: `${k}2`,
|
v4: `${k}2`,
|
||||||
};
|
};
|
||||||
|
|
||||||
keys.forEach((a) => {
|
methods.forEach((a) => {
|
||||||
expect(flattenHeaders({ headers: h, method: a })).toEqual(
|
expect(flattenHeaders({ headers: h, method: a })).toEqual(
|
||||||
a !== k ? h1 : h2,
|
a !== k ? h1 : h2,
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,20 +1,10 @@
|
||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
import { generateType } from '@/core/generateType';
|
import { generateType } from '@/core/generateType';
|
||||||
import {
|
import { testEachMethods } from 'scripts/test.utils';
|
||||||
requestMethodNames,
|
|
||||||
requestMethodWithDataNames,
|
|
||||||
requestMethodWithParamsNames,
|
|
||||||
} from '@/core/AxiosDomain';
|
|
||||||
|
|
||||||
describe('src/core/generateType.ts', () => {
|
describe('src/core/generateType.ts', () => {
|
||||||
test('应该是一个 reuqest', () => {
|
testEachMethods('%s 应该是一个 reuqest', (k) => {
|
||||||
for (const a of [
|
expect(generateType({ method: k })).toBe('request');
|
||||||
...requestMethodNames,
|
|
||||||
...requestMethodWithParamsNames,
|
|
||||||
...requestMethodWithDataNames,
|
|
||||||
]) {
|
|
||||||
expect(generateType({ method: a })).toBe('request');
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('应该是一个 upload', () => {
|
test('应该是一个 upload', () => {
|
||||||
|
|
|
@ -1,17 +1,13 @@
|
||||||
import { describe, test, expect, vi } from 'vitest';
|
import { describe, test, expect, vi } from 'vitest';
|
||||||
import {
|
import {
|
||||||
asyncTimeout,
|
asyncTimeout,
|
||||||
|
eachMethods,
|
||||||
mockAdapter,
|
mockAdapter,
|
||||||
mockAdapterError,
|
mockAdapterError,
|
||||||
mockAdapterFail,
|
mockAdapterFail,
|
||||||
testEachMethods,
|
testEachMethods,
|
||||||
} from 'scripts/test.utils';
|
} from 'scripts/test.utils';
|
||||||
import { request } from '@/core/request';
|
import { request } from '@/core/request';
|
||||||
import {
|
|
||||||
requestMethodNames,
|
|
||||||
requestMethodWithDataNames,
|
|
||||||
requestMethodWithParamsNames,
|
|
||||||
} from '@/core/AxiosDomain';
|
|
||||||
import axios from '@/axios';
|
import axios from '@/axios';
|
||||||
|
|
||||||
describe('src/core/request.ts', () => {
|
describe('src/core/request.ts', () => {
|
||||||
|
@ -200,11 +196,7 @@ describe('src/core/request.ts', () => {
|
||||||
download: true,
|
download: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
[
|
eachMethods((a) => {
|
||||||
...requestMethodNames,
|
|
||||||
...requestMethodWithParamsNames,
|
|
||||||
...requestMethodWithDataNames,
|
|
||||||
].forEach((a) => {
|
|
||||||
request({
|
request({
|
||||||
adapter: ({ type }) => {
|
adapter: ({ type }) => {
|
||||||
expect(type).toBe('request');
|
expect(type).toBe('request');
|
||||||
|
|
|
@ -7,7 +7,6 @@ import {
|
||||||
isNull,
|
isNull,
|
||||||
isUndefined,
|
isUndefined,
|
||||||
isString,
|
isString,
|
||||||
isPromise,
|
|
||||||
} from '@/helpers/isTypes';
|
} from '@/helpers/isTypes';
|
||||||
|
|
||||||
describe('src/helpers/isTypes.ts', () => {
|
describe('src/helpers/isTypes.ts', () => {
|
||||||
|
@ -54,23 +53,4 @@ describe('src/helpers/isTypes.ts', () => {
|
||||||
expect(isString('')).toBeTruthy();
|
expect(isString('')).toBeTruthy();
|
||||||
expect(isString(``)).toBeTruthy();
|
expect(isString(``)).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('应该能判断是 Promise', () => {
|
|
||||||
expect(isPromise({})).toBeFalsy();
|
|
||||||
expect(isPromise(Promise.resolve())).toBeTruthy();
|
|
||||||
expect(
|
|
||||||
isPromise(
|
|
||||||
new Promise(function () {
|
|
||||||
return;
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
).toBeTruthy();
|
|
||||||
expect(
|
|
||||||
isPromise({
|
|
||||||
then() {
|
|
||||||
return;
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
).toBeTruthy();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue