refactor: 拆分模块
parent
5aad9a83ab
commit
968bbb775e
|
@ -1,10 +1,10 @@
|
||||||
import { test } from 'vitest';
|
import { test } from 'vitest';
|
||||||
import {
|
import {
|
||||||
requestMethodNames,
|
PLAIN_METHODS,
|
||||||
requestMethodWithParamsNames,
|
WITH_PARAMS_METHODS,
|
||||||
requestMethodWithDataNames,
|
WITH_DATA_METHODS,
|
||||||
} from '@/core/AxiosDomain';
|
} from '@/constants/methods';
|
||||||
import { AxiosAdapterRequestConfig } from '@/adapter';
|
import { AxiosAdapterRequestConfig } from '@/adpater/createAdapter';
|
||||||
|
|
||||||
export function asyncNext() {
|
export function asyncNext() {
|
||||||
return Promise.resolve().then;
|
return Promise.resolve().then;
|
||||||
|
@ -114,9 +114,9 @@ export function mockAdapterFail(options: MockAdapterOptions = {}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const methods = [
|
export const methods = [
|
||||||
...requestMethodNames,
|
...PLAIN_METHODS,
|
||||||
...requestMethodWithParamsNames,
|
...WITH_PARAMS_METHODS,
|
||||||
...requestMethodWithDataNames,
|
...WITH_DATA_METHODS,
|
||||||
];
|
];
|
||||||
|
|
||||||
export const testEachMethods = test.each(methods);
|
export const testEachMethods = test.each(methods);
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
import { isFunction, isPlainObject } from './helpers/isTypes';
|
import { isFunction, isPlainObject } from '../helpers/isTypes';
|
||||||
import { assert } from './helpers/error';
|
import { assert } from '../helpers/error';
|
||||||
import {
|
import {
|
||||||
AxiosProgressEvent,
|
AxiosProgressEvent,
|
||||||
AxiosRequestFormData,
|
AxiosRequestFormData,
|
||||||
AxiosRequestHeaders,
|
AxiosRequestHeaders,
|
||||||
} from './core/Axios';
|
} from '../core/Axios';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 适配器请求类型
|
* 适配器请求类型
|
||||||
|
@ -240,59 +240,6 @@ export interface AxiosAdapter {
|
||||||
(config: AxiosAdapterRequestConfig): AxiosAdapterPlatformTask;
|
(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;
|
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,
|
AxiosRequestConfig,
|
||||||
AxiosRequestHeaders,
|
AxiosRequestHeaders,
|
||||||
} from './core/Axios';
|
} from './core/Axios';
|
||||||
import { createAdapter } from './adapter';
|
import { createAdapter } from './adpater/createAdapter';
|
||||||
import defaults from './defaults';
|
import defaults from './defaults';
|
||||||
import { version } from './version';
|
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,
|
AxiosAdapterPlatformTask,
|
||||||
AxiosAdapterRequestConfig,
|
AxiosAdapterRequestConfig,
|
||||||
AxiosAdapterResponseData,
|
AxiosAdapterResponseData,
|
||||||
} from '../adapter';
|
} from '../adpater/createAdapter';
|
||||||
import InterceptorManager, { Interceptor } from './InterceptorManager';
|
import InterceptorManager, { Interceptor } from './InterceptorManager';
|
||||||
import { mergeConfig } from './mergeConfig';
|
import { mergeConfig } from './mergeConfig';
|
||||||
import AxiosDomain from './AxiosDomain';
|
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 { isString, isUndefined } from '../helpers/isTypes';
|
||||||
import { deepMerge } from '../helpers/deepMerge';
|
import { deepMerge } from '../helpers/deepMerge';
|
||||||
import { mergeConfig } from './mergeConfig';
|
import { mergeConfig } from './mergeConfig';
|
||||||
|
@ -51,21 +56,6 @@ export type AxiosDomainRequestMethodWithData = <
|
||||||
config?: AxiosRequestConfig,
|
config?: AxiosRequestConfig,
|
||||||
) => Promise<AxiosResponse<TData>>;
|
) => 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 {
|
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(
|
AxiosDomain.prototype[method] = function processRequestMethod(
|
||||||
url,
|
url,
|
||||||
config = {},
|
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(
|
AxiosDomain.prototype[method] = function processRequestMethodWithParams(
|
||||||
url,
|
url,
|
||||||
params = {},
|
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(
|
AxiosDomain.prototype[method] = function processRequestMethodWithData(
|
||||||
url,
|
url,
|
||||||
data,
|
data,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { getDefaultAdapter } from './adapter';
|
import { getDefaultAdapter } from './adpater/getDefaultAdapter';
|
||||||
import { AxiosInstanceDefaults } from './axios';
|
import { AxiosInstanceDefaults } from './axios';
|
||||||
|
|
||||||
const defaults: AxiosInstanceDefaults = {
|
const defaults: AxiosInstanceDefaults = {
|
||||||
|
|
|
@ -29,7 +29,7 @@ export type {
|
||||||
AxiosAdapterUploadOptions,
|
AxiosAdapterUploadOptions,
|
||||||
AxiosAdapterPlatform,
|
AxiosAdapterPlatform,
|
||||||
AxiosAdapterPlatformTask,
|
AxiosAdapterPlatformTask,
|
||||||
} from './adapter';
|
} from './adpater/createAdapter';
|
||||||
export type {
|
export type {
|
||||||
AxiosInstanceDefaults,
|
AxiosInstanceDefaults,
|
||||||
AxiosInstance,
|
AxiosInstance,
|
||||||
|
@ -39,7 +39,7 @@ export type {
|
||||||
export { CancelToken, isCancel } from './request/cancel';
|
export { CancelToken, isCancel } from './request/cancel';
|
||||||
export { isAxiosError } from './request/createError';
|
export { isAxiosError } from './request/createError';
|
||||||
export { default as Axios } from './core/Axios';
|
export { default as Axios } from './core/Axios';
|
||||||
export { createAdapter } from './adapter';
|
export { createAdapter } from './adpater/createAdapter';
|
||||||
export { version } from './version';
|
export { version } from './version';
|
||||||
|
|
||||||
export default axios;
|
export default axios;
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
import { cleanStack } from '../helpers/error';
|
import { cleanStack } from '../helpers/error';
|
||||||
|
import { AxiosAdapterPlatformTask } from '../adpater/createAdapter';
|
||||||
import {
|
import {
|
||||||
AxiosRequestConfig,
|
AxiosRequestConfig,
|
||||||
AxiosResponse,
|
AxiosResponse,
|
||||||
AxiosResponseError,
|
AxiosResponseError,
|
||||||
} from '../core/Axios';
|
} from '../core/Axios';
|
||||||
import { AxiosAdapterPlatformTask } from '../adapter';
|
|
||||||
|
|
||||||
export type AxiosErrorResponse = AxiosResponse | AxiosResponseError;
|
export type AxiosErrorResponse = AxiosResponse | AxiosResponseError;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
|
import { WITH_DATA_RE } from '../constants/methods';
|
||||||
import { isFunction, isString } from '../helpers/isTypes';
|
import { isFunction, isString } from '../helpers/isTypes';
|
||||||
import { assert } from '../helpers/error';
|
import { assert } from '../helpers/error';
|
||||||
import { requestMethodWithDataNames } from '../core/AxiosDomain';
|
|
||||||
import {
|
import {
|
||||||
AxiosRequestConfig,
|
AxiosRequestConfig,
|
||||||
AxiosRequestMethod,
|
AxiosRequestMethod,
|
||||||
|
@ -13,14 +13,6 @@ import { request } from './request';
|
||||||
import { transformURL } from './transformURL';
|
import { transformURL } from './transformURL';
|
||||||
import { AxiosErrorResponse } from './createError';
|
import { AxiosErrorResponse } from './createError';
|
||||||
|
|
||||||
/**
|
|
||||||
* 可以携带 data 的请求方法
|
|
||||||
*/
|
|
||||||
const requestMethodWithDataRE = new RegExp(
|
|
||||||
`^${requestMethodWithDataNames.join('|')}`,
|
|
||||||
'i',
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 发送请求
|
* 发送请求
|
||||||
*
|
*
|
||||||
|
@ -41,7 +33,7 @@ export function dispatchRequest(config: AxiosRequestConfig) {
|
||||||
|
|
||||||
// 可以携带 data 的请求方法,转换 data
|
// 可以携带 data 的请求方法,转换 data
|
||||||
// 否则,删除 data
|
// 否则,删除 data
|
||||||
if (requestMethodWithDataRE.test(config.method!)) {
|
if (WITH_DATA_RE.test(config.method!)) {
|
||||||
dataTransformer(config, config.transformRequest);
|
dataTransformer(config, config.transformRequest);
|
||||||
} else {
|
} else {
|
||||||
delete config.data;
|
delete config.data;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { AxiosRequestConfig } from '../core/Axios';
|
import { AxiosRequestConfig } from '../core/Axios';
|
||||||
import { AxiosAdapterRequestType } from '../adapter';
|
import { AxiosAdapterRequestType } from '../adpater/createAdapter';
|
||||||
|
|
||||||
export function generateType(config: AxiosRequestConfig) {
|
export function generateType(config: AxiosRequestConfig) {
|
||||||
let requestType: AxiosAdapterRequestType = 'request';
|
let requestType: AxiosAdapterRequestType = 'request';
|
||||||
|
|
|
@ -10,8 +10,7 @@ import {
|
||||||
AxiosAdapterResponse,
|
AxiosAdapterResponse,
|
||||||
AxiosAdapterResponseError,
|
AxiosAdapterResponseError,
|
||||||
AxiosAdapterPlatformTask,
|
AxiosAdapterPlatformTask,
|
||||||
} from '../adapter';
|
} from '../adpater/createAdapter';
|
||||||
|
|
||||||
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';
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import { describe, test, expect, vi } from 'vitest';
|
import { describe, test, expect, vi } from 'vitest';
|
||||||
import { noop } from 'scripts/test.utils';
|
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('应该抛出异常', () => {
|
test('应该抛出异常', () => {
|
||||||
expect(() =>
|
expect(() =>
|
||||||
createAdapter(undefined as unknown as AxiosAdapterPlatform),
|
createAdapter(undefined as unknown as AxiosAdapterPlatform),
|
|
@ -1,5 +1,5 @@
|
||||||
import { describe, test, expect, beforeEach, afterEach, vi } from 'vitest';
|
import { describe, test, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||||
import { getDefaultAdapter } from '@/adapter';
|
import { getDefaultAdapter } from '@/adpater/getDefaultAdapter';
|
||||||
|
|
||||||
const platforms = [
|
const platforms = [
|
||||||
'uni',
|
'uni',
|
||||||
|
@ -15,7 +15,7 @@ const platforms = [
|
||||||
'unknown',
|
'unknown',
|
||||||
];
|
];
|
||||||
|
|
||||||
describe.each(platforms)('src/adapter.ts', (p) => {
|
describe.each(platforms)('src/adapter/getDefaultAdapter.ts', (p) => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.stubGlobal(p, {
|
vi.stubGlobal(p, {
|
||||||
request: vi.fn(),
|
request: vi.fn(),
|
|
@ -3,8 +3,8 @@ import { eachMethods } from 'scripts/test.utils';
|
||||||
import { CancelToken, isCancel } from '@/request/cancel';
|
import { CancelToken, isCancel } from '@/request/cancel';
|
||||||
import { isAxiosError } from '@/request/createError';
|
import { isAxiosError } from '@/request/createError';
|
||||||
import Axios from '@/core/Axios';
|
import Axios from '@/core/Axios';
|
||||||
|
import { createAdapter } from '@/adpater/createAdapter';
|
||||||
import defaults from '@/defaults';
|
import defaults from '@/defaults';
|
||||||
import { createAdapter } from '@/adapter';
|
|
||||||
import axios from '@/axios';
|
import axios from '@/axios';
|
||||||
|
|
||||||
describe('src/axios.ts', () => {
|
describe('src/axios.ts', () => {
|
||||||
|
|
|
@ -1,9 +1,17 @@
|
||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect, vi, afterEach } from 'vitest';
|
||||||
import axios from '@/axios';
|
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 defaults from '@/defaults';
|
||||||
import { testEachMethods } from 'scripts/test.utils';
|
import axios from '@/axios';
|
||||||
|
|
||||||
describe('src/axios.ts', () => {
|
describe('src/axios.ts', () => {
|
||||||
|
const data = { result: null };
|
||||||
|
|
||||||
test('应该有这些实例属性及方法', () => {
|
test('应该有这些实例属性及方法', () => {
|
||||||
expect(axios.defaults).toBe(defaults);
|
expect(axios.defaults).toBe(defaults);
|
||||||
expect(axios.interceptors).toBeTypeOf('object');
|
expect(axios.interceptors).toBeTypeOf('object');
|
||||||
|
@ -15,4 +23,148 @@ describe('src/axios.ts', () => {
|
||||||
testEachMethods('%s 应该是一个函数', (k) => {
|
testEachMethods('%s 应该是一个函数', (k) => {
|
||||||
expect(axios[k]).toBeTypeOf('function');
|
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 { describe, test, expect } from 'vitest';
|
||||||
import { mockAdapter, mockAdapterError } from 'scripts/test.utils';
|
import {
|
||||||
|
mockAdapter,
|
||||||
|
mockAdapterError,
|
||||||
|
testEachMethods,
|
||||||
|
} from 'scripts/test.utils';
|
||||||
import axios from '@/axios';
|
import axios from '@/axios';
|
||||||
|
|
||||||
describe('src/axios.ts', () => {
|
describe('src/axios.ts', () => {
|
||||||
|
const data = { 'src/axios.ts': true };
|
||||||
|
|
||||||
test('应该处理成功和失败', () => {
|
test('应该处理成功和失败', () => {
|
||||||
axios({
|
axios({
|
||||||
adapter: mockAdapter({
|
adapter: mockAdapter({
|
||||||
|
@ -39,4 +45,23 @@ describe('src/axios.ts', () => {
|
||||||
expect(err.response.data).toEqual({ v1: 1 });
|
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,
|
mockAdapterFail,
|
||||||
testEachMethods,
|
testEachMethods,
|
||||||
} from 'scripts/test.utils';
|
} from 'scripts/test.utils';
|
||||||
import AxiosDomain, {
|
import {
|
||||||
requestMethodNames,
|
PLAIN_METHODS,
|
||||||
requestMethodWithParamsNames,
|
WITH_DATA_METHODS,
|
||||||
requestMethodWithDataNames,
|
WITH_PARAMS_METHODS,
|
||||||
} from '@/core/AxiosDomain';
|
} from '@/constants/methods';
|
||||||
|
import AxiosDomain from '@/core/AxiosDomain';
|
||||||
import Axios from '@/core/Axios';
|
import Axios from '@/core/Axios';
|
||||||
import axios from '@/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) => {
|
axiosObj[a]('test', c).then((res) => {
|
||||||
expect(res.data).toEqual(data);
|
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) => {
|
axiosObj[a]('test', p, c1).then((res) => {
|
||||||
expect(res.data).toEqual(data);
|
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) => {
|
axiosObj[a]('test', d, c1).then((res) => {
|
||||||
expect(res.data).toEqual(data);
|
expect(res.data).toEqual(data);
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,20 +1,15 @@
|
||||||
import { describe, test, expect, vi } from 'vitest';
|
import { describe, test, expect, vi } from 'vitest';
|
||||||
import { ignore } from '@/helpers/ignore';
|
import { ignore } from '@/helpers/ignore';
|
||||||
import AxiosDomain, {
|
import {
|
||||||
requestMethodNames,
|
PLAIN_METHODS,
|
||||||
requestMethodWithParamsNames,
|
WITH_DATA_METHODS,
|
||||||
requestMethodWithDataNames,
|
WITH_PARAMS_METHODS,
|
||||||
} from '@/core/AxiosDomain';
|
} from '@/constants/methods';
|
||||||
|
import AxiosDomain from '@/core/AxiosDomain';
|
||||||
import { AxiosResponse } from '@/core/Axios';
|
import { AxiosResponse } from '@/core/Axios';
|
||||||
import { eachMethods } from 'scripts/test.utils';
|
import { eachMethods } from 'scripts/test.utils';
|
||||||
|
|
||||||
describe('src/core/AxiosDomain.ts', () => {
|
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('应该有这些实例属性', () => {
|
test('应该有这些实例属性', () => {
|
||||||
const c = {
|
const c = {
|
||||||
baseURL: 'http://api.com',
|
baseURL: 'http://api.com',
|
||||||
|
@ -59,14 +54,14 @@ describe('src/core/AxiosDomain.ts', () => {
|
||||||
|
|
||||||
a.request('test');
|
a.request('test');
|
||||||
|
|
||||||
requestMethodNames.forEach((k) => a[k]('test'));
|
PLAIN_METHODS.forEach((k) => a[k]('test'));
|
||||||
requestMethodWithParamsNames.forEach((k) => a[k]('test'));
|
WITH_PARAMS_METHODS.forEach((k) => a[k]('test'));
|
||||||
requestMethodWithDataNames.forEach((k) => a[k]('test'));
|
WITH_DATA_METHODS.forEach((k) => a[k]('test'));
|
||||||
|
|
||||||
const l =
|
const l =
|
||||||
requestMethodNames.length +
|
PLAIN_METHODS.length +
|
||||||
requestMethodWithParamsNames.length +
|
WITH_PARAMS_METHODS.length +
|
||||||
requestMethodWithDataNames.length +
|
WITH_DATA_METHODS.length +
|
||||||
1;
|
1;
|
||||||
expect(cb.mock.calls.length).toBe(l);
|
expect(cb.mock.calls.length).toBe(l);
|
||||||
cb.mock.calls.forEach(([config]) => expect(config.url).toBe('test'));
|
cb.mock.calls.forEach(([config]) => expect(config.url).toBe('test'));
|
||||||
|
@ -99,18 +94,14 @@ describe('src/core/AxiosDomain.ts', () => {
|
||||||
|
|
||||||
a.request(u, c);
|
a.request(u, c);
|
||||||
|
|
||||||
requestMethodNames.forEach((k) => a[k](u, c));
|
PLAIN_METHODS.forEach((k) => a[k](u, c));
|
||||||
requestMethodWithParamsNames.forEach((k) =>
|
WITH_PARAMS_METHODS.forEach((k) => a[k](u, c.params, ignore(c, 'params')));
|
||||||
a[k](u, c.params, ignore(c, 'params')),
|
WITH_DATA_METHODS.forEach((k) => a[k](u, c.data, ignore(c, 'data')));
|
||||||
);
|
|
||||||
requestMethodWithDataNames.forEach((k) =>
|
|
||||||
a[k](u, c.data, ignore(c, 'data')),
|
|
||||||
);
|
|
||||||
|
|
||||||
const l =
|
const l =
|
||||||
requestMethodNames.length +
|
PLAIN_METHODS.length +
|
||||||
requestMethodWithParamsNames.length +
|
WITH_PARAMS_METHODS.length +
|
||||||
requestMethodWithDataNames.length +
|
WITH_DATA_METHODS.length +
|
||||||
1;
|
1;
|
||||||
expect(cb.mock.calls.length).toBe(l);
|
expect(cb.mock.calls.length).toBe(l);
|
||||||
});
|
});
|
||||||
|
@ -142,18 +133,18 @@ describe('src/core/AxiosDomain.ts', () => {
|
||||||
|
|
||||||
a.request(c);
|
a.request(c);
|
||||||
|
|
||||||
requestMethodNames.forEach((k) => a[k](c.url, ignore(c, 'url')));
|
PLAIN_METHODS.forEach((k) => a[k](c.url, ignore(c, 'url')));
|
||||||
requestMethodWithParamsNames.forEach((k) =>
|
WITH_PARAMS_METHODS.forEach((k) =>
|
||||||
a[k](c.url, c.params, ignore(c, 'url', 'params')),
|
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')),
|
a[k](c.url, c.data, ignore(c, 'url', 'data')),
|
||||||
);
|
);
|
||||||
|
|
||||||
const l =
|
const l =
|
||||||
requestMethodNames.length +
|
PLAIN_METHODS.length +
|
||||||
requestMethodWithParamsNames.length +
|
WITH_PARAMS_METHODS.length +
|
||||||
requestMethodWithDataNames.length +
|
WITH_DATA_METHODS.length +
|
||||||
1;
|
1;
|
||||||
expect(cb.mock.calls.length).toBe(l);
|
expect(cb.mock.calls.length).toBe(l);
|
||||||
});
|
});
|
||||||
|
@ -190,7 +181,7 @@ describe('src/core/AxiosDomain.ts', () => {
|
||||||
return {} as AxiosResponse;
|
return {} as AxiosResponse;
|
||||||
});
|
});
|
||||||
|
|
||||||
requestMethodWithParamsNames.forEach((k) => a[k]('test', p, c));
|
WITH_PARAMS_METHODS.forEach((k) => a[k]('test', p, c));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('应该只取传入的 data', () => {
|
test('应该只取传入的 data', () => {
|
||||||
|
@ -212,7 +203,7 @@ describe('src/core/AxiosDomain.ts', () => {
|
||||||
return {} as AxiosResponse;
|
return {} as AxiosResponse;
|
||||||
});
|
});
|
||||||
|
|
||||||
requestMethodWithDataNames.forEach((k) => a[k]('test', d, c));
|
WITH_DATA_METHODS.forEach((k) => a[k]('test', d, c));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('应该支持多种类型 data', () => {
|
test('应该支持多种类型 data', () => {
|
||||||
|
@ -240,7 +231,7 @@ describe('src/core/AxiosDomain.ts', () => {
|
||||||
return {} as AxiosResponse;
|
return {} as AxiosResponse;
|
||||||
});
|
});
|
||||||
|
|
||||||
requestMethodWithDataNames.forEach((k) => {
|
WITH_DATA_METHODS.forEach((k) => {
|
||||||
testStr[k]('test', str);
|
testStr[k]('test', str);
|
||||||
testObj[k]('test', obj);
|
testObj[k]('test', obj);
|
||||||
testBuff[k]('test', buff);
|
testBuff[k]('test', buff);
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import { describe, test, expect, vi } from 'vitest';
|
import { describe, test, expect, vi } from 'vitest';
|
||||||
import { asyncNext, mockAdapter, testEachMethods } from 'scripts/test.utils';
|
import { asyncNext, mockAdapter, testEachMethods } from 'scripts/test.utils';
|
||||||
import { dispatchRequest } from '@/request/dispatchRequest';
|
|
||||||
import {
|
import {
|
||||||
requestMethodNames,
|
PLAIN_METHODS,
|
||||||
requestMethodWithDataNames,
|
WITH_DATA_METHODS,
|
||||||
requestMethodWithParamsNames,
|
WITH_PARAMS_METHODS,
|
||||||
} from '@/core/AxiosDomain';
|
} from '@/constants/methods';
|
||||||
|
import { dispatchRequest } from '@/request/dispatchRequest';
|
||||||
|
|
||||||
import axios from '@/axios';
|
import axios from '@/axios';
|
||||||
import _defaults from '@/defaults';
|
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 = {
|
const c = {
|
||||||
...defaults,
|
...defaults,
|
||||||
url: 'test',
|
url: 'test',
|
||||||
|
@ -161,7 +161,7 @@ describe('src/request/dispatchRequest.ts', () => {
|
||||||
transformRequest: () => ({ id: 1 }),
|
transformRequest: () => ({ id: 1 }),
|
||||||
};
|
};
|
||||||
|
|
||||||
[...requestMethodNames, ...requestMethodWithParamsNames].forEach((k) => {
|
[...PLAIN_METHODS, ...WITH_PARAMS_METHODS].forEach((k) => {
|
||||||
const s = { ...c, method: k };
|
const s = { ...c, method: k };
|
||||||
dispatchRequest(s);
|
dispatchRequest(s);
|
||||||
expect(s.data).toBeUndefined();
|
expect(s.data).toBeUndefined();
|
||||||
|
|
Loading…
Reference in New Issue