chore: 一些小的调整
parent
e643f36fe0
commit
f23d27024f
|
@ -1,5 +1,6 @@
|
||||||
import { isFunction, isPlainObject } from '../helpers/isTypes';
|
import { isFunction, isPlainObject } from '../helpers/isTypes';
|
||||||
import { assert } from '../helpers/error';
|
import { assert } from '../helpers/error';
|
||||||
|
import { origIgnore } from '../helpers/ignore';
|
||||||
import {
|
import {
|
||||||
AxiosProgressEvent,
|
AxiosProgressEvent,
|
||||||
AxiosRequestFormData,
|
AxiosRequestFormData,
|
||||||
|
@ -291,7 +292,8 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
|
||||||
) {
|
) {
|
||||||
response.status = response.status ?? response.statusCode;
|
response.status = response.status ?? response.statusCode;
|
||||||
response.headers = response.headers ?? response.header;
|
response.headers = response.headers ?? response.header;
|
||||||
clean(response, ['statusCode', 'errMsg', 'errno', 'header']);
|
|
||||||
|
origIgnore(response, ['statusCode', 'errMsg', 'errno', 'header']);
|
||||||
}
|
}
|
||||||
|
|
||||||
function processRequest(
|
function processRequest(
|
||||||
|
@ -307,13 +309,14 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
|
||||||
): AxiosAdapterPlatformTask {
|
): AxiosAdapterPlatformTask {
|
||||||
const options = baseOptions as AxiosAdapterUploadOptions;
|
const options = baseOptions as AxiosAdapterUploadOptions;
|
||||||
const { name, filePath, fileType, ...formData } = options.data as AnyObject;
|
const { name, filePath, fileType, ...formData } = options.data as AnyObject;
|
||||||
|
|
||||||
options.name = name;
|
options.name = name;
|
||||||
options.fileName = name;
|
options.fileName = name;
|
||||||
options.filePath = filePath;
|
options.filePath = filePath;
|
||||||
options.fileType = fileType;
|
options.fileType = fileType;
|
||||||
options.formData = formData;
|
options.formData = formData;
|
||||||
|
|
||||||
|
origIgnore(options, ['params', 'data']);
|
||||||
|
|
||||||
return upload(options);
|
return upload(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -323,7 +326,6 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
|
||||||
): AxiosAdapterPlatformTask {
|
): AxiosAdapterPlatformTask {
|
||||||
const options = baseOptions as AxiosAdapterDownloadOptions;
|
const options = baseOptions as AxiosAdapterDownloadOptions;
|
||||||
const { params, success } = options;
|
const { params, success } = options;
|
||||||
|
|
||||||
options.filePath = params?.filePath;
|
options.filePath = params?.filePath;
|
||||||
options.success = (response) => {
|
options.success = (response) => {
|
||||||
response.data = {
|
response.data = {
|
||||||
|
@ -333,18 +335,16 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
|
||||||
// response.apFilePath 为支付宝小程序基础库小于 2.7.23 的特有属性。
|
// response.apFilePath 为支付宝小程序基础库小于 2.7.23 的特有属性。
|
||||||
response.apFilePath,
|
response.apFilePath,
|
||||||
};
|
};
|
||||||
clean(response, ['tempFilePath', 'apFilePath', 'filePath']);
|
|
||||||
|
origIgnore(response, ['tempFilePath', 'apFilePath', 'filePath']);
|
||||||
|
|
||||||
success(response);
|
success(response);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
origIgnore(options, ['params']);
|
||||||
|
|
||||||
return download(options);
|
return download(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
function clean(obj: AnyObject, keys: string[]) {
|
|
||||||
for (const key of keys) {
|
|
||||||
delete obj[key];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return adapter;
|
return adapter;
|
||||||
}
|
}
|
||||||
|
|
|
@ -353,7 +353,11 @@ export interface AxiosConstructor {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class Axios {
|
export default class Axios {
|
||||||
|
/**
|
||||||
|
* 父级实例
|
||||||
|
*/
|
||||||
#parent?: Axios;
|
#parent?: Axios;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认请求配置
|
* 默认请求配置
|
||||||
*/
|
*/
|
||||||
|
@ -430,8 +434,13 @@ export default class Axios {
|
||||||
*/
|
*/
|
||||||
use: (middleware: MiddlewareCallback) => MiddlewareManager;
|
use: (middleware: MiddlewareCallback) => MiddlewareManager;
|
||||||
|
|
||||||
constructor(defaults: AxiosRequestConfig, parent?: Axios) {
|
/**
|
||||||
this.defaults = defaults;
|
*
|
||||||
|
* @param config 默认配置
|
||||||
|
* @param parent 父级实例
|
||||||
|
*/
|
||||||
|
constructor(config: AxiosRequestConfig, parent?: Axios) {
|
||||||
|
this.defaults = config;
|
||||||
this.#parent = parent;
|
this.#parent = parent;
|
||||||
this.use = this.#middleware.use;
|
this.use = this.#middleware.use;
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,19 +53,19 @@ export interface AxiosInstance extends AxiosRequest, Axios {
|
||||||
fork(config: AxiosRequestConfig): AxiosInstance;
|
fork(config: AxiosRequestConfig): AxiosInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createInstance(config: AxiosRequestConfig, parent?: Axios) {
|
export function createInstance(defaults: AxiosRequestConfig, parent?: Axios) {
|
||||||
const context = new Axios(config, parent);
|
const context = new Axios(defaults, parent);
|
||||||
const instance = context.request as AxiosInstance;
|
const instance = context.request as AxiosInstance;
|
||||||
|
|
||||||
instance.getUri = function getUri(config) {
|
instance.getUri = function getUri(config) {
|
||||||
return transformURL(mergeConfig(instance.defaults, config));
|
return transformURL(mergeConfig(defaults, config));
|
||||||
};
|
};
|
||||||
instance.create = function create(config) {
|
instance.create = function create(config) {
|
||||||
return createInstance(mergeConfig(instance.defaults, config));
|
return createInstance(mergeConfig(defaults, config));
|
||||||
};
|
};
|
||||||
instance.extend = function extend(config) {
|
instance.extend = function extend(config) {
|
||||||
config.baseURL = combineURL(instance.defaults.baseURL, config.baseURL);
|
config.baseURL = combineURL(defaults.baseURL, config.baseURL);
|
||||||
return createInstance(mergeConfig(instance.defaults, config), context);
|
return createInstance(mergeConfig(defaults, config), context);
|
||||||
};
|
};
|
||||||
instance.fork = instance.extend;
|
instance.fork = instance.extend;
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,11 @@ const deepMergeConfigMap: Record<string, boolean> = {
|
||||||
params: true,
|
params: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 合并配置
|
||||||
|
*
|
||||||
|
* 按照设定的优先级进行合并
|
||||||
|
*/
|
||||||
export function mergeConfig(
|
export function mergeConfig(
|
||||||
config1: AxiosRequestConfig = {},
|
config1: AxiosRequestConfig = {},
|
||||||
config2: AxiosRequestConfig = {},
|
config2: AxiosRequestConfig = {},
|
||||||
|
|
|
@ -1,8 +1,26 @@
|
||||||
|
/**
|
||||||
|
* 忽略键值对
|
||||||
|
*
|
||||||
|
* @param obj 源对象
|
||||||
|
* @param keys 忽略的键
|
||||||
|
*/
|
||||||
export function ignore<T extends AnyObject, K extends keyof T>(
|
export function ignore<T extends AnyObject, K extends keyof T>(
|
||||||
obj: T,
|
obj: T,
|
||||||
...keys: K[]
|
...keys: K[]
|
||||||
): Omit<T, K> {
|
): Omit<T, K> {
|
||||||
const result = { ...obj };
|
const res = { ...obj };
|
||||||
for (const key of keys) delete result[key];
|
origIgnore(res, keys);
|
||||||
return result;
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从源对象删除键值对
|
||||||
|
*
|
||||||
|
* @param obj 源对象
|
||||||
|
* @param keys 忽略的键
|
||||||
|
*/
|
||||||
|
export function origIgnore(obj: AnyObject, keys: PropertyKey[]) {
|
||||||
|
for (const key of keys) {
|
||||||
|
delete obj[key as string | number];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,6 +56,7 @@ describe('src/adapter/createAdapter.ts', () => {
|
||||||
data: {
|
data: {
|
||||||
name: 'file',
|
name: 'file',
|
||||||
filePath: '/path/file',
|
filePath: '/path/file',
|
||||||
|
fileType: 'image',
|
||||||
user: 'test',
|
user: 'test',
|
||||||
id: 1,
|
id: 1,
|
||||||
},
|
},
|
||||||
|
@ -78,7 +79,8 @@ describe('src/adapter/createAdapter.ts', () => {
|
||||||
|
|
||||||
expect(p.request).not.toBeCalled();
|
expect(p.request).not.toBeCalled();
|
||||||
a(r);
|
a(r);
|
||||||
expect(p.request.mock.calls[0][0]).toMatchInlineSnapshot(`
|
const rOpts = p.request.mock.calls[0][0];
|
||||||
|
expect(rOpts).toMatchInlineSnapshot(`
|
||||||
{
|
{
|
||||||
"fail": [Function],
|
"fail": [Function],
|
||||||
"header": {
|
"header": {
|
||||||
|
@ -93,21 +95,39 @@ describe('src/adapter/createAdapter.ts', () => {
|
||||||
"url": "test",
|
"url": "test",
|
||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
|
expect(rOpts.header).toEqual(r.headers);
|
||||||
|
|
||||||
|
expect(p.download).not.toBeCalled();
|
||||||
|
a(d);
|
||||||
|
const dOpts = p.download.mock.calls[0][0];
|
||||||
|
expect(dOpts).toMatchInlineSnapshot(`
|
||||||
|
{
|
||||||
|
"fail": [Function],
|
||||||
|
"filePath": "/path/file",
|
||||||
|
"header": {
|
||||||
|
"Accept": "application/json, text/plain, */*",
|
||||||
|
},
|
||||||
|
"headers": {
|
||||||
|
"Accept": "application/json, text/plain, */*",
|
||||||
|
},
|
||||||
|
"method": "GET",
|
||||||
|
"success": [Function],
|
||||||
|
"type": "download",
|
||||||
|
"url": "test",
|
||||||
|
}
|
||||||
|
`);
|
||||||
|
expect(dOpts.header).toEqual(d.headers);
|
||||||
|
expect(dOpts.filePath).toEqual(d.params.filePath);
|
||||||
|
|
||||||
expect(p.upload).not.toBeCalled();
|
expect(p.upload).not.toBeCalled();
|
||||||
a(u);
|
a(u);
|
||||||
expect(p.upload.mock.calls[0][0]).toMatchInlineSnapshot(`
|
const uOpts = p.upload.mock.calls[0][0];
|
||||||
|
expect(uOpts).toMatchInlineSnapshot(`
|
||||||
{
|
{
|
||||||
"data": {
|
|
||||||
"filePath": "/path/file",
|
|
||||||
"id": 1,
|
|
||||||
"name": "file",
|
|
||||||
"user": "test",
|
|
||||||
},
|
|
||||||
"fail": [Function],
|
"fail": [Function],
|
||||||
"fileName": "file",
|
"fileName": "file",
|
||||||
"filePath": "/path/file",
|
"filePath": "/path/file",
|
||||||
"fileType": undefined,
|
"fileType": "image",
|
||||||
"formData": {
|
"formData": {
|
||||||
"id": 1,
|
"id": 1,
|
||||||
"user": "test",
|
"user": "test",
|
||||||
|
@ -125,28 +145,15 @@ describe('src/adapter/createAdapter.ts', () => {
|
||||||
"url": "test",
|
"url": "test",
|
||||||
}
|
}
|
||||||
`);
|
`);
|
||||||
|
expect(uOpts.header).toEqual(u.headers);
|
||||||
expect(p.download).not.toBeCalled();
|
expect(uOpts.name).toEqual(u.data.name);
|
||||||
a(d);
|
expect(uOpts.fileName).toEqual(u.data.name);
|
||||||
expect(p.download.mock.calls[0][0]).toMatchInlineSnapshot(`
|
expect(uOpts.filePath).toEqual(u.data.filePath);
|
||||||
{
|
expect(uOpts.fileType).toEqual(u.data.fileType);
|
||||||
"fail": [Function],
|
expect(uOpts.formData).toEqual({
|
||||||
"filePath": "/path/file",
|
id: u.data.id,
|
||||||
"header": {
|
user: u.data.user,
|
||||||
"Accept": "application/json, text/plain, */*",
|
});
|
||||||
},
|
|
||||||
"headers": {
|
|
||||||
"Accept": "application/json, text/plain, */*",
|
|
||||||
},
|
|
||||||
"method": "GET",
|
|
||||||
"params": {
|
|
||||||
"filePath": "/path/file",
|
|
||||||
},
|
|
||||||
"success": [Function],
|
|
||||||
"type": "download",
|
|
||||||
"url": "test",
|
|
||||||
}
|
|
||||||
`);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('应该支持转换下载数据', () => {
|
test('应该支持转换下载数据', () => {
|
||||||
|
|
|
@ -1,11 +1,22 @@
|
||||||
import { describe, test, expect } from 'vitest';
|
import { describe, test, expect } from 'vitest';
|
||||||
import { ignore } from '@/helpers/ignore';
|
import { ignore, origIgnore } from '@/helpers/ignore';
|
||||||
|
|
||||||
describe('src/helpers/ignore.ts', () => {
|
describe('src/helpers/ignore.ts', () => {
|
||||||
test('不应该改变传递的对象', () => {
|
test('不应该改变传递的对象', () => {
|
||||||
expect(ignore({ v1: 1 })).toEqual({ v1: 1 });
|
expect(ignore({ v1: 1 })).toEqual({ v1: 1 });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('应该不改变源对象', () => {
|
||||||
|
const o = {
|
||||||
|
v1: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(ignore(o, 'v1')).toEqual({});
|
||||||
|
expect(o).toEqual({
|
||||||
|
v1: 1,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('应该忽略指定键值', () => {
|
test('应该忽略指定键值', () => {
|
||||||
const o = {
|
const o = {
|
||||||
v1: 1,
|
v1: 1,
|
||||||
|
@ -36,4 +47,29 @@ describe('src/helpers/ignore.ts', () => {
|
||||||
});
|
});
|
||||||
expect(ignore(o, 'v1', 'v2', 'v3')).toEqual({});
|
expect(ignore(o, 'v1', 'v2', 'v3')).toEqual({});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('应该从源对象删除', () => {
|
||||||
|
const o = {
|
||||||
|
v1: 1,
|
||||||
|
v2: {},
|
||||||
|
v3: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
origIgnore(o, ['v1']);
|
||||||
|
|
||||||
|
expect(o).toEqual({
|
||||||
|
v2: {},
|
||||||
|
v3: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
origIgnore(o, ['v2']);
|
||||||
|
|
||||||
|
expect(o).toEqual({
|
||||||
|
v3: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
origIgnore(o, ['v3']);
|
||||||
|
|
||||||
|
expect(o).toEqual({});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue