refactor: 拆分模块
parent
5aad9a83ab
commit
968bbb775e
|
@ -1,10 +1,10 @@
|
|||
import { test } from 'vitest';
|
||||
import {
|
||||
requestMethodNames,
|
||||
requestMethodWithParamsNames,
|
||||
requestMethodWithDataNames,
|
||||
} from '@/core/AxiosDomain';
|
||||
import { AxiosAdapterRequestConfig } from '@/adapter';
|
||||
PLAIN_METHODS,
|
||||
WITH_PARAMS_METHODS,
|
||||
WITH_DATA_METHODS,
|
||||
} from '@/constants/methods';
|
||||
import { AxiosAdapterRequestConfig } from '@/adpater/createAdapter';
|
||||
|
||||
export function asyncNext() {
|
||||
return Promise.resolve().then;
|
||||
|
@ -114,9 +114,9 @@ export function mockAdapterFail(options: MockAdapterOptions = {}) {
|
|||
}
|
||||
|
||||
export const methods = [
|
||||
...requestMethodNames,
|
||||
...requestMethodWithParamsNames,
|
||||
...requestMethodWithDataNames,
|
||||
...PLAIN_METHODS,
|
||||
...WITH_PARAMS_METHODS,
|
||||
...WITH_DATA_METHODS,
|
||||
];
|
||||
|
||||
export const testEachMethods = test.each(methods);
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
import { isFunction, isPlainObject } from './helpers/isTypes';
|
||||
import { assert } from './helpers/error';
|
||||
import { isFunction, isPlainObject } from '../helpers/isTypes';
|
||||
import { assert } from '../helpers/error';
|
||||
import {
|
||||
AxiosProgressEvent,
|
||||
AxiosRequestFormData,
|
||||
AxiosRequestHeaders,
|
||||
} from './core/Axios';
|
||||
} from '../core/Axios';
|
||||
|
||||
/**
|
||||
* 适配器请求类型
|
||||
|
@ -240,59 +240,6 @@ export interface AxiosAdapter {
|
|||
(config: AxiosAdapterRequestConfig): AxiosAdapterPlatformTask;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取支持的平台适配器
|
||||
*/
|
||||
export function getDefaultAdapter() {
|
||||
const platform = revisePlatformApiNames(getPlatform());
|
||||
|
||||
function getPlatform() {
|
||||
const undef = 'undefined';
|
||||
|
||||
if (typeof uni !== undef) {
|
||||
return {
|
||||
request: uni.request,
|
||||
downloadFile: uni.downloadFile,
|
||||
uploadFile: uni.uploadFile,
|
||||
};
|
||||
} else if (typeof wx !== undef) {
|
||||
return wx;
|
||||
} else if (typeof my !== undef) {
|
||||
return my;
|
||||
} else if (typeof swan !== undef) {
|
||||
return swan;
|
||||
} else if (typeof tt !== undef) {
|
||||
return tt;
|
||||
} else if (typeof qq !== undef) {
|
||||
return qq;
|
||||
} else if (typeof qh !== undef) {
|
||||
return qh;
|
||||
} else if (typeof ks !== undef) {
|
||||
return ks;
|
||||
} else if (typeof dd !== undef) {
|
||||
return dd;
|
||||
} else if (typeof jd !== undef) {
|
||||
return jd;
|
||||
}
|
||||
}
|
||||
|
||||
function revisePlatformApiNames(platform?: AnyObject) {
|
||||
return (
|
||||
platform && {
|
||||
request: platform.request ?? platform.httpRequest,
|
||||
upload: platform.upload ?? platform.uploadFile,
|
||||
download: platform.download ?? platform.downloadFile,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
if (!isPlatform(platform)) {
|
||||
return;
|
||||
}
|
||||
|
||||
return createAdapter(platform);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建适配器
|
||||
*
|
||||
|
@ -400,12 +347,3 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
|
|||
|
||||
return adapter;
|
||||
}
|
||||
|
||||
export function isPlatform(value: any): value is AxiosAdapterPlatform {
|
||||
return (
|
||||
isPlainObject(value) &&
|
||||
isFunction(value.request) &&
|
||||
isFunction(value.upload) &&
|
||||
isFunction(value.download)
|
||||
);
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
import { isFunction, isPlainObject } from '../helpers/isTypes';
|
||||
import { AxiosAdapterPlatform, createAdapter } from './createAdapter';
|
||||
|
||||
/**
|
||||
* 获取支持的平台适配器
|
||||
*/
|
||||
export function getDefaultAdapter() {
|
||||
const platform = revisePlatformApiNames(getPlatform());
|
||||
if (!isPlatform(platform)) {
|
||||
return;
|
||||
}
|
||||
|
||||
function getPlatform() {
|
||||
const undef = 'undefined';
|
||||
|
||||
if (typeof uni !== undef) {
|
||||
return {
|
||||
request: uni.request,
|
||||
downloadFile: uni.downloadFile,
|
||||
uploadFile: uni.uploadFile,
|
||||
};
|
||||
} else if (typeof wx !== undef) {
|
||||
return wx;
|
||||
} else if (typeof my !== undef) {
|
||||
return my;
|
||||
} else if (typeof swan !== undef) {
|
||||
return swan;
|
||||
} else if (typeof tt !== undef) {
|
||||
return tt;
|
||||
} else if (typeof qq !== undef) {
|
||||
return qq;
|
||||
} else if (typeof qh !== undef) {
|
||||
return qh;
|
||||
} else if (typeof ks !== undef) {
|
||||
return ks;
|
||||
} else if (typeof dd !== undef) {
|
||||
return dd;
|
||||
} else if (typeof jd !== undef) {
|
||||
return jd;
|
||||
}
|
||||
}
|
||||
|
||||
function revisePlatformApiNames(platform?: AnyObject) {
|
||||
return (
|
||||
platform && {
|
||||
request: platform.request ?? platform.httpRequest,
|
||||
upload: platform.upload ?? platform.uploadFile,
|
||||
download: platform.download ?? platform.downloadFile,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
function isPlatform(value: any): value is AxiosAdapterPlatform {
|
||||
return (
|
||||
isPlainObject(value) &&
|
||||
isFunction(value.request) &&
|
||||
isFunction(value.upload) &&
|
||||
isFunction(value.download)
|
||||
);
|
||||
}
|
||||
|
||||
return createAdapter(platform);
|
||||
}
|
|
@ -11,7 +11,7 @@ import Axios, {
|
|||
AxiosRequestConfig,
|
||||
AxiosRequestHeaders,
|
||||
} from './core/Axios';
|
||||
import { createAdapter } from './adapter';
|
||||
import { createAdapter } from './adpater/createAdapter';
|
||||
import defaults from './defaults';
|
||||
import { version } from './version';
|
||||
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
/**
|
||||
* 普通的请求方法名称
|
||||
*/
|
||||
export const PLAIN_METHODS = ['options', 'trace', 'connect'] as const;
|
||||
|
||||
/**
|
||||
* 带参数的请求方法名称
|
||||
*/
|
||||
export const WITH_PARAMS_METHODS = ['head', 'get', 'delete'] as const;
|
||||
|
||||
/**
|
||||
* 带数据的请求方法名称
|
||||
*/
|
||||
export const WITH_DATA_METHODS = ['post', 'put', 'patch'] as const;
|
||||
|
||||
/**
|
||||
* 可以携带 data 的请求方法
|
||||
*/
|
||||
export const WITH_DATA_RE = new RegExp(`^${WITH_DATA_METHODS.join('|')}`, 'i');
|
|
@ -11,7 +11,7 @@ import {
|
|||
AxiosAdapterPlatformTask,
|
||||
AxiosAdapterRequestConfig,
|
||||
AxiosAdapterResponseData,
|
||||
} from '../adapter';
|
||||
} from '../adpater/createAdapter';
|
||||
import InterceptorManager, { Interceptor } from './InterceptorManager';
|
||||
import { mergeConfig } from './mergeConfig';
|
||||
import AxiosDomain from './AxiosDomain';
|
||||
|
|
|
@ -1,3 +1,8 @@
|
|||
import {
|
||||
PLAIN_METHODS,
|
||||
WITH_DATA_METHODS,
|
||||
WITH_PARAMS_METHODS,
|
||||
} from '../constants/methods';
|
||||
import { isString, isUndefined } from '../helpers/isTypes';
|
||||
import { deepMerge } from '../helpers/deepMerge';
|
||||
import { mergeConfig } from './mergeConfig';
|
||||
|
@ -51,21 +56,6 @@ export type AxiosDomainRequestMethodWithData = <
|
|||
config?: AxiosRequestConfig,
|
||||
) => Promise<AxiosResponse<TData>>;
|
||||
|
||||
/**
|
||||
* 普通的请求方法名称
|
||||
*/
|
||||
export const requestMethodNames = ['options', 'trace', 'connect'] as const;
|
||||
|
||||
/**
|
||||
* 带参数的请求方法名称
|
||||
*/
|
||||
export const requestMethodWithParamsNames = ['head', 'get', 'delete'] as const;
|
||||
|
||||
/**
|
||||
* 带数据的请求方法名称
|
||||
*/
|
||||
export const requestMethodWithDataNames = ['post', 'put', 'patch'] as const;
|
||||
|
||||
export default class AxiosDomain {
|
||||
/**
|
||||
* 默认请求配置
|
||||
|
@ -147,7 +137,7 @@ export default class AxiosDomain {
|
|||
}
|
||||
}
|
||||
|
||||
for (const method of requestMethodNames) {
|
||||
for (const method of PLAIN_METHODS) {
|
||||
AxiosDomain.prototype[method] = function processRequestMethod(
|
||||
url,
|
||||
config = {},
|
||||
|
@ -157,7 +147,7 @@ for (const method of requestMethodNames) {
|
|||
};
|
||||
}
|
||||
|
||||
for (const method of requestMethodWithParamsNames) {
|
||||
for (const method of WITH_PARAMS_METHODS) {
|
||||
AxiosDomain.prototype[method] = function processRequestMethodWithParams(
|
||||
url,
|
||||
params = {},
|
||||
|
@ -169,7 +159,7 @@ for (const method of requestMethodWithParamsNames) {
|
|||
};
|
||||
}
|
||||
|
||||
for (const method of requestMethodWithDataNames) {
|
||||
for (const method of WITH_DATA_METHODS) {
|
||||
AxiosDomain.prototype[method] = function processRequestMethodWithData(
|
||||
url,
|
||||
data,
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { getDefaultAdapter } from './adapter';
|
||||
import { getDefaultAdapter } from './adpater/getDefaultAdapter';
|
||||
import { AxiosInstanceDefaults } from './axios';
|
||||
|
||||
const defaults: AxiosInstanceDefaults = {
|
||||
|
|
|
@ -29,7 +29,7 @@ export type {
|
|||
AxiosAdapterUploadOptions,
|
||||
AxiosAdapterPlatform,
|
||||
AxiosAdapterPlatformTask,
|
||||
} from './adapter';
|
||||
} from './adpater/createAdapter';
|
||||
export type {
|
||||
AxiosInstanceDefaults,
|
||||
AxiosInstance,
|
||||
|
@ -39,7 +39,7 @@ export type {
|
|||
export { CancelToken, isCancel } from './request/cancel';
|
||||
export { isAxiosError } from './request/createError';
|
||||
export { default as Axios } from './core/Axios';
|
||||
export { createAdapter } from './adapter';
|
||||
export { createAdapter } from './adpater/createAdapter';
|
||||
export { version } from './version';
|
||||
|
||||
export default axios;
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
import { cleanStack } from '../helpers/error';
|
||||
import { AxiosAdapterPlatformTask } from '../adpater/createAdapter';
|
||||
import {
|
||||
AxiosRequestConfig,
|
||||
AxiosResponse,
|
||||
AxiosResponseError,
|
||||
} from '../core/Axios';
|
||||
import { AxiosAdapterPlatformTask } from '../adapter';
|
||||
|
||||
export type AxiosErrorResponse = AxiosResponse | AxiosResponseError;
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { WITH_DATA_RE } from '../constants/methods';
|
||||
import { isFunction, isString } from '../helpers/isTypes';
|
||||
import { assert } from '../helpers/error';
|
||||
import { requestMethodWithDataNames } from '../core/AxiosDomain';
|
||||
import {
|
||||
AxiosRequestConfig,
|
||||
AxiosRequestMethod,
|
||||
|
@ -13,14 +13,6 @@ import { request } from './request';
|
|||
import { transformURL } from './transformURL';
|
||||
import { AxiosErrorResponse } from './createError';
|
||||
|
||||
/**
|
||||
* 可以携带 data 的请求方法
|
||||
*/
|
||||
const requestMethodWithDataRE = new RegExp(
|
||||
`^${requestMethodWithDataNames.join('|')}`,
|
||||
'i',
|
||||
);
|
||||
|
||||
/**
|
||||
* 发送请求
|
||||
*
|
||||
|
@ -41,7 +33,7 @@ export function dispatchRequest(config: AxiosRequestConfig) {
|
|||
|
||||
// 可以携带 data 的请求方法,转换 data
|
||||
// 否则,删除 data
|
||||
if (requestMethodWithDataRE.test(config.method!)) {
|
||||
if (WITH_DATA_RE.test(config.method!)) {
|
||||
dataTransformer(config, config.transformRequest);
|
||||
} else {
|
||||
delete config.data;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { AxiosRequestConfig } from '../core/Axios';
|
||||
import { AxiosAdapterRequestType } from '../adapter';
|
||||
import { AxiosAdapterRequestType } from '../adpater/createAdapter';
|
||||
|
||||
export function generateType(config: AxiosRequestConfig) {
|
||||
let requestType: AxiosAdapterRequestType = 'request';
|
||||
|
|
|
@ -10,8 +10,7 @@ import {
|
|||
AxiosAdapterResponse,
|
||||
AxiosAdapterResponseError,
|
||||
AxiosAdapterPlatformTask,
|
||||
} from '../adapter';
|
||||
|
||||
} from '../adpater/createAdapter';
|
||||
import { isCancelToken } from './cancel';
|
||||
import { AxiosErrorResponse, createError } from './createError';
|
||||
import { generateType } from './generateType';
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
import { describe, test, expect, vi } from 'vitest';
|
||||
import { noop } from 'scripts/test.utils';
|
||||
import { AxiosAdapterPlatform, createAdapter } from '@/adapter';
|
||||
import { AxiosAdapterPlatform, createAdapter } from '@/adpater/createAdapter';
|
||||
|
||||
describe('src/adapter.ts', () => {
|
||||
describe('src/adapter/createAdapter.ts', () => {
|
||||
test('应该抛出异常', () => {
|
||||
expect(() =>
|
||||
createAdapter(undefined as unknown as AxiosAdapterPlatform),
|
|
@ -1,5 +1,5 @@
|
|||
import { describe, test, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
import { getDefaultAdapter } from '@/adapter';
|
||||
import { getDefaultAdapter } from '@/adpater/getDefaultAdapter';
|
||||
|
||||
const platforms = [
|
||||
'uni',
|
||||
|
@ -15,7 +15,7 @@ const platforms = [
|
|||
'unknown',
|
||||
];
|
||||
|
||||
describe.each(platforms)('src/adapter.ts', (p) => {
|
||||
describe.each(platforms)('src/adapter/getDefaultAdapter.ts', (p) => {
|
||||
beforeEach(() => {
|
||||
vi.stubGlobal(p, {
|
||||
request: vi.fn(),
|
|
@ -3,8 +3,8 @@ import { eachMethods } from 'scripts/test.utils';
|
|||
import { CancelToken, isCancel } from '@/request/cancel';
|
||||
import { isAxiosError } from '@/request/createError';
|
||||
import Axios from '@/core/Axios';
|
||||
import { createAdapter } from '@/adpater/createAdapter';
|
||||
import defaults from '@/defaults';
|
||||
import { createAdapter } from '@/adapter';
|
||||
import axios from '@/axios';
|
||||
|
||||
describe('src/axios.ts', () => {
|
||||
|
|
|
@ -1,9 +1,17 @@
|
|||
import { describe, test, expect } from 'vitest';
|
||||
import axios from '@/axios';
|
||||
import { describe, test, expect, vi, afterEach } from 'vitest';
|
||||
import { mockAdapter, testEachMethods } from 'scripts/test.utils';
|
||||
import {
|
||||
PLAIN_METHODS,
|
||||
WITH_DATA_METHODS,
|
||||
WITH_PARAMS_METHODS,
|
||||
} from '@/constants/methods';
|
||||
import AxiosDomain from '@/core/AxiosDomain';
|
||||
import defaults from '@/defaults';
|
||||
import { testEachMethods } from 'scripts/test.utils';
|
||||
import axios from '@/axios';
|
||||
|
||||
describe('src/axios.ts', () => {
|
||||
const data = { result: null };
|
||||
|
||||
test('应该有这些实例属性及方法', () => {
|
||||
expect(axios.defaults).toBe(defaults);
|
||||
expect(axios.interceptors).toBeTypeOf('object');
|
||||
|
@ -15,4 +23,148 @@ describe('src/axios.ts', () => {
|
|||
testEachMethods('%s 应该是一个函数', (k) => {
|
||||
expect(axios[k]).toBeTypeOf('function');
|
||||
});
|
||||
|
||||
test('应该可以发送普通别名请求', () => {
|
||||
const c = {
|
||||
baseURL: 'http://api.com',
|
||||
adapter: mockAdapter({
|
||||
before: (config) => {
|
||||
expect(config.url).toBe('http://api.com/test');
|
||||
},
|
||||
data,
|
||||
}),
|
||||
};
|
||||
|
||||
PLAIN_METHODS.forEach((a) => {
|
||||
axios[a]('test', c).then((res) => {
|
||||
expect(res.data).toEqual(data);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('应该可以发送带参数的别名请求', () => {
|
||||
const p = { id: 1 };
|
||||
const c1 = {
|
||||
baseURL: 'http://api.com',
|
||||
adapter: mockAdapter({
|
||||
before: (config) => {
|
||||
expect(config.url).toBe('http://api.com/test?id=1');
|
||||
expect(config.params).toEqual(p);
|
||||
},
|
||||
data,
|
||||
}),
|
||||
};
|
||||
const c2 = {
|
||||
baseURL: 'http://api.com',
|
||||
adapter: mockAdapter({
|
||||
before: (config) => {
|
||||
expect(config.url).toBe('http://api.com/test/1');
|
||||
expect(config.params).toEqual({});
|
||||
},
|
||||
data,
|
||||
}),
|
||||
};
|
||||
|
||||
WITH_PARAMS_METHODS.forEach((a) => {
|
||||
axios[a]('test', p, c1).then((res) => {
|
||||
expect(res.data).toEqual(data);
|
||||
});
|
||||
axios[a]('test/:id', { ...p }, c2).then((res) => {
|
||||
expect(res.data).toEqual(data);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('应该可以发送带数据的别名请求', () => {
|
||||
const d = { id: 1 };
|
||||
const c1 = {
|
||||
baseURL: 'http://api.com',
|
||||
adapter: mockAdapter({
|
||||
before: (config) => {
|
||||
expect(config.url).toBe('http://api.com/test');
|
||||
expect(config.data).toEqual(d);
|
||||
},
|
||||
data,
|
||||
}),
|
||||
};
|
||||
const c2 = {
|
||||
baseURL: 'http://api.com',
|
||||
adapter: mockAdapter({
|
||||
before: (config) => {
|
||||
expect(config.url).toBe('http://api.com/test/1');
|
||||
expect(config.data).toEqual(d);
|
||||
},
|
||||
data,
|
||||
}),
|
||||
};
|
||||
|
||||
WITH_DATA_METHODS.forEach((a) => {
|
||||
axios[a]('test', d, c1).then((res) => {
|
||||
expect(res.data).toEqual(data);
|
||||
});
|
||||
axios[a]('test/:id', d, c2).then((res) => {
|
||||
expect(res.data).toEqual(data);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('应该支持添加/移除请求拦截器', async () => {
|
||||
const cb = vi.fn((v) => v);
|
||||
|
||||
const id = axios.interceptors.request.use(cb);
|
||||
await axios.request('/test', {
|
||||
adapter: mockAdapter(),
|
||||
});
|
||||
|
||||
expect(cb.mock.calls.length).toBe(1);
|
||||
|
||||
await axios.request('/test', {
|
||||
adapter: mockAdapter(),
|
||||
});
|
||||
|
||||
expect(cb.mock.calls.length).toBe(2);
|
||||
|
||||
axios.interceptors.request.eject(id);
|
||||
await axios.request('/test', {
|
||||
adapter: mockAdapter(),
|
||||
});
|
||||
|
||||
expect(cb.mock.calls.length).toBe(2);
|
||||
});
|
||||
|
||||
test('应该支持添加/移除响应拦截器', async () => {
|
||||
const cb = vi.fn((v) => v);
|
||||
|
||||
const id = axios.interceptors.response.use(cb);
|
||||
await axios.request('/test', {
|
||||
adapter: mockAdapter(),
|
||||
});
|
||||
|
||||
expect(cb.mock.calls.length).toBe(1);
|
||||
|
||||
await axios.request('/test', {
|
||||
adapter: mockAdapter(),
|
||||
});
|
||||
|
||||
expect(cb.mock.calls.length).toBe(2);
|
||||
|
||||
axios.interceptors.response.eject(id);
|
||||
await axios.request('/test', {
|
||||
adapter: mockAdapter(),
|
||||
});
|
||||
|
||||
expect(cb.mock.calls.length).toBe(2);
|
||||
});
|
||||
|
||||
test('应该可以获取 URI', () => {
|
||||
expect(axios.getUri({ url: 'test' })).toBe('test');
|
||||
expect(axios.getUri({ url: 'test', params: { id: 1 } })).toBe('test?id=1');
|
||||
expect(axios.getUri({ url: 'test', paramsSerializer: () => 'id=1' })).toBe(
|
||||
'test?id=1',
|
||||
);
|
||||
});
|
||||
|
||||
test('派生的领域应该为 AxiosDomain 的实例', () => {
|
||||
expect(axios.fork() instanceof AxiosDomain).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,8 +1,14 @@
|
|||
import { describe, test, expect } from 'vitest';
|
||||
import { mockAdapter, mockAdapterError } from 'scripts/test.utils';
|
||||
import {
|
||||
mockAdapter,
|
||||
mockAdapterError,
|
||||
testEachMethods,
|
||||
} from 'scripts/test.utils';
|
||||
import axios from '@/axios';
|
||||
|
||||
describe('src/axios.ts', () => {
|
||||
const data = { 'src/axios.ts': true };
|
||||
|
||||
test('应该处理成功和失败', () => {
|
||||
axios({
|
||||
adapter: mockAdapter({
|
||||
|
@ -39,4 +45,23 @@ describe('src/axios.ts', () => {
|
|||
expect(err.response.data).toEqual({ v1: 1 });
|
||||
});
|
||||
});
|
||||
|
||||
testEachMethods('应该可以发送 %d 请求', (k) => {
|
||||
const c = {
|
||||
baseURL: 'http://api.com',
|
||||
adapter: mockAdapter({
|
||||
before: (config) => {
|
||||
expect(config.url).toBe('http://api.com/test');
|
||||
},
|
||||
data,
|
||||
}),
|
||||
};
|
||||
|
||||
axios('test', { ...c, method: k }).then((res) => {
|
||||
expect(res.data).toEqual(data);
|
||||
});
|
||||
axios({ ...c, url: 'test', method: k }).then((res) => {
|
||||
expect(res.data).toEqual(data);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
import { describe, test, expect } from 'vitest';
|
||||
import {
|
||||
PLAIN_METHODS,
|
||||
WITH_DATA_METHODS,
|
||||
WITH_PARAMS_METHODS,
|
||||
} from '@/constants/methods';
|
||||
|
||||
describe('src/constants/methods.ts', () => {
|
||||
test('PLAIN_METHODS 应该有这些值', () => {
|
||||
expect(PLAIN_METHODS).toEqual(['options', 'trace', 'connect']);
|
||||
});
|
||||
|
||||
test('WITH_PARAMS_METHODS 应该有这些值', () => {
|
||||
expect(WITH_PARAMS_METHODS).toEqual(['head', 'get', 'delete']);
|
||||
});
|
||||
|
||||
test('WITH_DATA_METHODS 应该有这些值', () => {
|
||||
expect(WITH_DATA_METHODS).toEqual(['post', 'put', 'patch']);
|
||||
});
|
||||
});
|
|
@ -5,11 +5,12 @@ import {
|
|||
mockAdapterFail,
|
||||
testEachMethods,
|
||||
} from 'scripts/test.utils';
|
||||
import AxiosDomain, {
|
||||
requestMethodNames,
|
||||
requestMethodWithParamsNames,
|
||||
requestMethodWithDataNames,
|
||||
} from '@/core/AxiosDomain';
|
||||
import {
|
||||
PLAIN_METHODS,
|
||||
WITH_DATA_METHODS,
|
||||
WITH_PARAMS_METHODS,
|
||||
} from '@/constants/methods';
|
||||
import AxiosDomain from '@/core/AxiosDomain';
|
||||
import Axios from '@/core/Axios';
|
||||
import axios from '@/axios';
|
||||
|
||||
|
@ -51,7 +52,7 @@ describe('src/core/Axios.ts', () => {
|
|||
}),
|
||||
};
|
||||
|
||||
requestMethodNames.forEach((a) => {
|
||||
PLAIN_METHODS.forEach((a) => {
|
||||
axiosObj[a]('test', c).then((res) => {
|
||||
expect(res.data).toEqual(data);
|
||||
});
|
||||
|
@ -79,7 +80,7 @@ describe('src/core/Axios.ts', () => {
|
|||
}),
|
||||
};
|
||||
|
||||
requestMethodWithParamsNames.forEach((a) => {
|
||||
WITH_PARAMS_METHODS.forEach((a) => {
|
||||
axiosObj[a]('test', p, c1).then((res) => {
|
||||
expect(res.data).toEqual(data);
|
||||
});
|
||||
|
@ -110,7 +111,7 @@ describe('src/core/Axios.ts', () => {
|
|||
}),
|
||||
};
|
||||
|
||||
requestMethodWithDataNames.forEach((a) => {
|
||||
WITH_DATA_METHODS.forEach((a) => {
|
||||
axiosObj[a]('test', d, c1).then((res) => {
|
||||
expect(res.data).toEqual(data);
|
||||
});
|
||||
|
|
|
@ -1,20 +1,15 @@
|
|||
import { describe, test, expect, vi } from 'vitest';
|
||||
import { ignore } from '@/helpers/ignore';
|
||||
import AxiosDomain, {
|
||||
requestMethodNames,
|
||||
requestMethodWithParamsNames,
|
||||
requestMethodWithDataNames,
|
||||
} from '@/core/AxiosDomain';
|
||||
import {
|
||||
PLAIN_METHODS,
|
||||
WITH_DATA_METHODS,
|
||||
WITH_PARAMS_METHODS,
|
||||
} from '@/constants/methods';
|
||||
import AxiosDomain from '@/core/AxiosDomain';
|
||||
import { AxiosResponse } from '@/core/Axios';
|
||||
import { eachMethods } from 'scripts/test.utils';
|
||||
|
||||
describe('src/core/AxiosDomain.ts', () => {
|
||||
test('应该有这些常量', () => {
|
||||
expect(requestMethodNames).toEqual(['options', 'trace', 'connect']);
|
||||
expect(requestMethodWithParamsNames).toEqual(['head', 'get', 'delete']);
|
||||
expect(requestMethodWithDataNames).toEqual(['post', 'put', 'patch']);
|
||||
});
|
||||
|
||||
test('应该有这些实例属性', () => {
|
||||
const c = {
|
||||
baseURL: 'http://api.com',
|
||||
|
@ -59,14 +54,14 @@ describe('src/core/AxiosDomain.ts', () => {
|
|||
|
||||
a.request('test');
|
||||
|
||||
requestMethodNames.forEach((k) => a[k]('test'));
|
||||
requestMethodWithParamsNames.forEach((k) => a[k]('test'));
|
||||
requestMethodWithDataNames.forEach((k) => a[k]('test'));
|
||||
PLAIN_METHODS.forEach((k) => a[k]('test'));
|
||||
WITH_PARAMS_METHODS.forEach((k) => a[k]('test'));
|
||||
WITH_DATA_METHODS.forEach((k) => a[k]('test'));
|
||||
|
||||
const l =
|
||||
requestMethodNames.length +
|
||||
requestMethodWithParamsNames.length +
|
||||
requestMethodWithDataNames.length +
|
||||
PLAIN_METHODS.length +
|
||||
WITH_PARAMS_METHODS.length +
|
||||
WITH_DATA_METHODS.length +
|
||||
1;
|
||||
expect(cb.mock.calls.length).toBe(l);
|
||||
cb.mock.calls.forEach(([config]) => expect(config.url).toBe('test'));
|
||||
|
@ -99,18 +94,14 @@ describe('src/core/AxiosDomain.ts', () => {
|
|||
|
||||
a.request(u, c);
|
||||
|
||||
requestMethodNames.forEach((k) => a[k](u, c));
|
||||
requestMethodWithParamsNames.forEach((k) =>
|
||||
a[k](u, c.params, ignore(c, 'params')),
|
||||
);
|
||||
requestMethodWithDataNames.forEach((k) =>
|
||||
a[k](u, c.data, ignore(c, 'data')),
|
||||
);
|
||||
PLAIN_METHODS.forEach((k) => a[k](u, c));
|
||||
WITH_PARAMS_METHODS.forEach((k) => a[k](u, c.params, ignore(c, 'params')));
|
||||
WITH_DATA_METHODS.forEach((k) => a[k](u, c.data, ignore(c, 'data')));
|
||||
|
||||
const l =
|
||||
requestMethodNames.length +
|
||||
requestMethodWithParamsNames.length +
|
||||
requestMethodWithDataNames.length +
|
||||
PLAIN_METHODS.length +
|
||||
WITH_PARAMS_METHODS.length +
|
||||
WITH_DATA_METHODS.length +
|
||||
1;
|
||||
expect(cb.mock.calls.length).toBe(l);
|
||||
});
|
||||
|
@ -142,18 +133,18 @@ describe('src/core/AxiosDomain.ts', () => {
|
|||
|
||||
a.request(c);
|
||||
|
||||
requestMethodNames.forEach((k) => a[k](c.url, ignore(c, 'url')));
|
||||
requestMethodWithParamsNames.forEach((k) =>
|
||||
PLAIN_METHODS.forEach((k) => a[k](c.url, ignore(c, 'url')));
|
||||
WITH_PARAMS_METHODS.forEach((k) =>
|
||||
a[k](c.url, c.params, ignore(c, 'url', 'params')),
|
||||
);
|
||||
requestMethodWithDataNames.forEach((k) =>
|
||||
WITH_DATA_METHODS.forEach((k) =>
|
||||
a[k](c.url, c.data, ignore(c, 'url', 'data')),
|
||||
);
|
||||
|
||||
const l =
|
||||
requestMethodNames.length +
|
||||
requestMethodWithParamsNames.length +
|
||||
requestMethodWithDataNames.length +
|
||||
PLAIN_METHODS.length +
|
||||
WITH_PARAMS_METHODS.length +
|
||||
WITH_DATA_METHODS.length +
|
||||
1;
|
||||
expect(cb.mock.calls.length).toBe(l);
|
||||
});
|
||||
|
@ -190,7 +181,7 @@ describe('src/core/AxiosDomain.ts', () => {
|
|||
return {} as AxiosResponse;
|
||||
});
|
||||
|
||||
requestMethodWithParamsNames.forEach((k) => a[k]('test', p, c));
|
||||
WITH_PARAMS_METHODS.forEach((k) => a[k]('test', p, c));
|
||||
});
|
||||
|
||||
test('应该只取传入的 data', () => {
|
||||
|
@ -212,7 +203,7 @@ describe('src/core/AxiosDomain.ts', () => {
|
|||
return {} as AxiosResponse;
|
||||
});
|
||||
|
||||
requestMethodWithDataNames.forEach((k) => a[k]('test', d, c));
|
||||
WITH_DATA_METHODS.forEach((k) => a[k]('test', d, c));
|
||||
});
|
||||
|
||||
test('应该支持多种类型 data', () => {
|
||||
|
@ -240,7 +231,7 @@ describe('src/core/AxiosDomain.ts', () => {
|
|||
return {} as AxiosResponse;
|
||||
});
|
||||
|
||||
requestMethodWithDataNames.forEach((k) => {
|
||||
WITH_DATA_METHODS.forEach((k) => {
|
||||
testStr[k]('test', str);
|
||||
testObj[k]('test', obj);
|
||||
testBuff[k]('test', buff);
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
import { describe, test, expect, vi } from 'vitest';
|
||||
import { asyncNext, mockAdapter, testEachMethods } from 'scripts/test.utils';
|
||||
import { dispatchRequest } from '@/request/dispatchRequest';
|
||||
import {
|
||||
requestMethodNames,
|
||||
requestMethodWithDataNames,
|
||||
requestMethodWithParamsNames,
|
||||
} from '@/core/AxiosDomain';
|
||||
PLAIN_METHODS,
|
||||
WITH_DATA_METHODS,
|
||||
WITH_PARAMS_METHODS,
|
||||
} from '@/constants/methods';
|
||||
import { dispatchRequest } from '@/request/dispatchRequest';
|
||||
|
||||
import axios from '@/axios';
|
||||
import _defaults from '@/defaults';
|
||||
|
@ -139,7 +139,7 @@ describe('src/request/dispatchRequest.ts', () => {
|
|||
});
|
||||
});
|
||||
|
||||
test.each(requestMethodWithDataNames)('%s 方法应该支持转换请求数据', (k) => {
|
||||
test.each(WITH_DATA_METHODS)('%s 方法应该支持转换请求数据', (k) => {
|
||||
const c = {
|
||||
...defaults,
|
||||
url: 'test',
|
||||
|
@ -161,7 +161,7 @@ describe('src/request/dispatchRequest.ts', () => {
|
|||
transformRequest: () => ({ id: 1 }),
|
||||
};
|
||||
|
||||
[...requestMethodNames, ...requestMethodWithParamsNames].forEach((k) => {
|
||||
[...PLAIN_METHODS, ...WITH_PARAMS_METHODS].forEach((k) => {
|
||||
const s = { ...c, method: k };
|
||||
dispatchRequest(s);
|
||||
expect(s.data).toBeUndefined();
|
||||
|
|
Loading…
Reference in New Issue