feat: 支持深度合并 params & data

pull/41/head
zjx0905 2023-04-07 12:39:23 +08:00
parent 1c09ffdd91
commit 22f65cf69c
3 changed files with 86 additions and 27 deletions

View File

@ -207,7 +207,9 @@ export default class Axios extends AxiosDomain {
};
constructor(defaults: AxiosRequestConfig = {}) {
super(defaults, (...args) => this.#processRequest(...args));
super(defaults, (...args) => {
return this.#processRequest(...args);
});
}
getUri(config: AxiosRequestConfig): string {
@ -222,15 +224,16 @@ export default class Axios extends AxiosDomain {
*
*/
fork(defaults: AxiosRequestConfig) {
const config = mergeConfig(this.defaults, defaults);
const { baseURL = '' } = defaults;
if (!isAbsoluteURL(baseURL)) {
config.baseURL = combineURL(this.defaults.baseURL ?? '', baseURL);
defaults.baseURL = combineURL(this.defaults.baseURL ?? '', baseURL);
}
return new AxiosDomain(config, this.#processRequest);
return new AxiosDomain(mergeConfig(this.defaults, defaults), (...args) => {
return this.#processRequest(...args);
});
}
#processRequest = <TData = unknown>(config: AxiosRequestConfig) => {
#processRequest<TData = unknown>(config: AxiosRequestConfig) {
const { request, response } = this.interceptors;
let promiseRequest = Promise.resolve(config);
@ -247,5 +250,5 @@ export default class Axios extends AxiosDomain {
>;
});
return promiseResponse as Promise<AxiosResponse<TData>>;
};
}
}

View File

@ -1,5 +1,6 @@
import { AxiosRequestConfig, AxiosRequestData, AxiosResponse } from './Axios';
import { deepMerge } from '../helpers/deepMerge';
import { mergeConfig } from './mergeConfig';
import { AxiosRequestConfig, AxiosRequestData, AxiosResponse } from './Axios';
export interface AxiosDomainRequest {
<TData = unknown>(
@ -49,7 +50,7 @@ export interface AxiosDomainAsRequestWithData {
/**
*
*/
data?: AnyObject,
data?: AxiosRequestData,
/**
*
*/
@ -128,8 +129,8 @@ export default class AxiosDomain {
processRequest: AxiosDomainRequest,
) {
this.defaults = defaults;
this.request = <TData = unknown>(config: AxiosRequestConfig) => {
return processRequest<TData>(mergeConfig(this.defaults, config));
this.request = (config) => {
return processRequest(mergeConfig(this.defaults, config));
};
this.#createAsRequests();
@ -139,10 +140,7 @@ export default class AxiosDomain {
#createAsRequests() {
for (const alias of AxiosDomain.as) {
this[alias] = function processAsRequest(
url: string,
config: AxiosRequestConfig = {},
) {
this[alias] = function processAsRequest(url, config = {}) {
config.url = url;
config.method = alias;
@ -153,14 +151,10 @@ export default class AxiosDomain {
#createAspRequests() {
for (const alias of AxiosDomain.asp) {
this[alias] = function processAspRequest(
url: string,
params: AnyObject = {},
config: AxiosRequestConfig = {},
) {
this[alias] = function processAspRequest(url, params = {}, config = {}) {
config.url = url;
config.method = alias;
config.params = Object.assign(params, config.params);
config.params = deepMerge(params, config.params ?? {});
return this.request(config);
};
@ -169,14 +163,10 @@ export default class AxiosDomain {
#createAsdRequests() {
for (const alias of AxiosDomain.asd) {
this[alias] = function processAsdRequest(
url: string,
data: AxiosRequestData = {},
config: AxiosRequestConfig = {},
) {
this[alias] = function processAsdRequest(url, data = {}, config = {}) {
config.url = url;
config.method = alias;
config.data = Object.assign(data, config.data);
config.data = deepMerge(data, config.data ?? {});
return this.request(config);
};

View File

@ -2,7 +2,7 @@ import { describe, test, expect, vi } from 'vitest';
import AxiosDomain, { AxiosDomainRequest } from 'src/core/AxiosDomain';
import { ignore } from 'src/helpers/ignore';
describe('src/core/Axios.ts', () => {
describe('src/core/AxiosDomain.ts', () => {
test('应该有这些静态属性', () => {
expect(AxiosDomain.as).toEqual(['options', 'trace', 'connect']);
expect(AxiosDomain.asp).toEqual(['head', 'get', 'delete']);
@ -87,4 +87,70 @@ describe('src/core/Axios.ts', () => {
1;
expect(cb.mock.calls.length).toBe(l);
});
test('应该支持深度合并 params', () => {
const d = {
baseURL: 'http://api.com',
};
const p = {
v1: 1,
v2: {
v1: 1,
},
};
const c = {
params: {
v2: {
v2: 2,
},
v3: 3,
},
};
const a = new AxiosDomain(d, ((config) => {
expect(config.params).toEqual({
v1: 1,
v2: {
v1: 1,
v2: 2,
},
v3: 3,
});
}) as AxiosDomainRequest);
AxiosDomain.asp.forEach((k) => a[k]('test', p, c));
});
test('应该支持深度合并 data', () => {
const ds = {
baseURL: 'http://api.com',
};
const d = {
v1: 1,
v2: {
v1: 1,
},
};
const c = {
data: {
v2: {
v2: 2,
},
v3: 3,
},
};
const a = new AxiosDomain(ds, ((config) => {
expect(config.data).toEqual({
v1: 1,
v2: {
v1: 1,
v2: 2,
},
v3: 3,
});
}) as AxiosDomainRequest);
AxiosDomain.asd.forEach((k) => a[k]('test', d, c));
});
});