chore: 一些小的调整

pull/49/head
zjx0905 2023-04-27 21:25:47 +08:00
parent e643f36fe0
commit f23d27024f
7 changed files with 128 additions and 53 deletions

View File

@ -1,5 +1,6 @@
import { isFunction, isPlainObject } from '../helpers/isTypes';
import { assert } from '../helpers/error';
import { origIgnore } from '../helpers/ignore';
import {
AxiosProgressEvent,
AxiosRequestFormData,
@ -291,7 +292,8 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
) {
response.status = response.status ?? response.statusCode;
response.headers = response.headers ?? response.header;
clean(response, ['statusCode', 'errMsg', 'errno', 'header']);
origIgnore(response, ['statusCode', 'errMsg', 'errno', 'header']);
}
function processRequest(
@ -307,13 +309,14 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
): AxiosAdapterPlatformTask {
const options = baseOptions as AxiosAdapterUploadOptions;
const { name, filePath, fileType, ...formData } = options.data as AnyObject;
options.name = name;
options.fileName = name;
options.filePath = filePath;
options.fileType = fileType;
options.formData = formData;
origIgnore(options, ['params', 'data']);
return upload(options);
}
@ -323,7 +326,6 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
): AxiosAdapterPlatformTask {
const options = baseOptions as AxiosAdapterDownloadOptions;
const { params, success } = options;
options.filePath = params?.filePath;
options.success = (response) => {
response.data = {
@ -333,18 +335,16 @@ export function createAdapter(platform: AxiosAdapterPlatform) {
// response.apFilePath 为支付宝小程序基础库小于 2.7.23 的特有属性。
response.apFilePath,
};
clean(response, ['tempFilePath', 'apFilePath', 'filePath']);
origIgnore(response, ['tempFilePath', 'apFilePath', 'filePath']);
success(response);
};
origIgnore(options, ['params']);
return download(options);
}
function clean(obj: AnyObject, keys: string[]) {
for (const key of keys) {
delete obj[key];
}
}
return adapter;
}

View File

@ -353,7 +353,11 @@ export interface AxiosConstructor {
}
export default class Axios {
/**
*
*/
#parent?: Axios;
/**
*
*/
@ -430,8 +434,13 @@ export default class Axios {
*/
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.use = this.#middleware.use;
}

View File

@ -53,19 +53,19 @@ export interface AxiosInstance extends AxiosRequest, Axios {
fork(config: AxiosRequestConfig): AxiosInstance;
}
export function createInstance(config: AxiosRequestConfig, parent?: Axios) {
const context = new Axios(config, parent);
export function createInstance(defaults: AxiosRequestConfig, parent?: Axios) {
const context = new Axios(defaults, parent);
const instance = context.request as AxiosInstance;
instance.getUri = function getUri(config) {
return transformURL(mergeConfig(instance.defaults, config));
return transformURL(mergeConfig(defaults, config));
};
instance.create = function create(config) {
return createInstance(mergeConfig(instance.defaults, config));
return createInstance(mergeConfig(defaults, config));
};
instance.extend = function extend(config) {
config.baseURL = combineURL(instance.defaults.baseURL, config.baseURL);
return createInstance(mergeConfig(instance.defaults, config), context);
config.baseURL = combineURL(defaults.baseURL, config.baseURL);
return createInstance(mergeConfig(defaults, config), context);
};
instance.fork = instance.extend;

View File

@ -13,6 +13,11 @@ const deepMergeConfigMap: Record<string, boolean> = {
params: true,
};
/**
*
*
*
*/
export function mergeConfig(
config1: AxiosRequestConfig = {},
config2: AxiosRequestConfig = {},

View File

@ -1,8 +1,26 @@
/**
*
*
* @param obj
* @param keys
*/
export function ignore<T extends AnyObject, K extends keyof T>(
obj: T,
...keys: K[]
): Omit<T, K> {
const result = { ...obj };
for (const key of keys) delete result[key];
return result;
const res = { ...obj };
origIgnore(res, keys);
return res;
}
/**
*
*
* @param obj
* @param keys
*/
export function origIgnore(obj: AnyObject, keys: PropertyKey[]) {
for (const key of keys) {
delete obj[key as string | number];
}
}

View File

@ -56,6 +56,7 @@ describe('src/adapter/createAdapter.ts', () => {
data: {
name: 'file',
filePath: '/path/file',
fileType: 'image',
user: 'test',
id: 1,
},
@ -78,7 +79,8 @@ describe('src/adapter/createAdapter.ts', () => {
expect(p.request).not.toBeCalled();
a(r);
expect(p.request.mock.calls[0][0]).toMatchInlineSnapshot(`
const rOpts = p.request.mock.calls[0][0];
expect(rOpts).toMatchInlineSnapshot(`
{
"fail": [Function],
"header": {
@ -93,21 +95,39 @@ describe('src/adapter/createAdapter.ts', () => {
"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();
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],
"fileName": "file",
"filePath": "/path/file",
"fileType": undefined,
"fileType": "image",
"formData": {
"id": 1,
"user": "test",
@ -125,28 +145,15 @@ describe('src/adapter/createAdapter.ts', () => {
"url": "test",
}
`);
expect(p.download).not.toBeCalled();
a(d);
expect(p.download.mock.calls[0][0]).toMatchInlineSnapshot(`
{
"fail": [Function],
"filePath": "/path/file",
"header": {
"Accept": "application/json, text/plain, */*",
},
"headers": {
"Accept": "application/json, text/plain, */*",
},
"method": "GET",
"params": {
"filePath": "/path/file",
},
"success": [Function],
"type": "download",
"url": "test",
}
`);
expect(uOpts.header).toEqual(u.headers);
expect(uOpts.name).toEqual(u.data.name);
expect(uOpts.fileName).toEqual(u.data.name);
expect(uOpts.filePath).toEqual(u.data.filePath);
expect(uOpts.fileType).toEqual(u.data.fileType);
expect(uOpts.formData).toEqual({
id: u.data.id,
user: u.data.user,
});
});
test('应该支持转换下载数据', () => {

View File

@ -1,11 +1,22 @@
import { describe, test, expect } from 'vitest';
import { ignore } from '@/helpers/ignore';
import { ignore, origIgnore } from '@/helpers/ignore';
describe('src/helpers/ignore.ts', () => {
test('不应该改变传递的对象', () => {
expect(ignore({ v1: 1 })).toEqual({ v1: 1 });
});
test('应该不改变源对象', () => {
const o = {
v1: 1,
};
expect(ignore(o, 'v1')).toEqual({});
expect(o).toEqual({
v1: 1,
});
});
test('应该忽略指定键值', () => {
const o = {
v1: 1,
@ -36,4 +47,29 @@ describe('src/helpers/ignore.ts', () => {
});
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({});
});
});