feat: 支持深度合并 params & data
parent
1c09ffdd91
commit
22f65cf69c
|
@ -207,7 +207,9 @@ export default class Axios extends AxiosDomain {
|
||||||
};
|
};
|
||||||
|
|
||||||
constructor(defaults: AxiosRequestConfig = {}) {
|
constructor(defaults: AxiosRequestConfig = {}) {
|
||||||
super(defaults, (...args) => this.#processRequest(...args));
|
super(defaults, (...args) => {
|
||||||
|
return this.#processRequest(...args);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getUri(config: AxiosRequestConfig): string {
|
getUri(config: AxiosRequestConfig): string {
|
||||||
|
@ -222,15 +224,16 @@ export default class Axios extends AxiosDomain {
|
||||||
* 派生领域
|
* 派生领域
|
||||||
*/
|
*/
|
||||||
fork(defaults: AxiosRequestConfig) {
|
fork(defaults: AxiosRequestConfig) {
|
||||||
const config = mergeConfig(this.defaults, defaults);
|
|
||||||
const { baseURL = '' } = defaults;
|
const { baseURL = '' } = defaults;
|
||||||
if (!isAbsoluteURL(baseURL)) {
|
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;
|
const { request, response } = this.interceptors;
|
||||||
|
|
||||||
let promiseRequest = Promise.resolve(config);
|
let promiseRequest = Promise.resolve(config);
|
||||||
|
@ -247,5 +250,5 @@ export default class Axios extends AxiosDomain {
|
||||||
>;
|
>;
|
||||||
});
|
});
|
||||||
return promiseResponse as Promise<AxiosResponse<TData>>;
|
return promiseResponse as Promise<AxiosResponse<TData>>;
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { AxiosRequestConfig, AxiosRequestData, AxiosResponse } from './Axios';
|
import { deepMerge } from '../helpers/deepMerge';
|
||||||
import { mergeConfig } from './mergeConfig';
|
import { mergeConfig } from './mergeConfig';
|
||||||
|
import { AxiosRequestConfig, AxiosRequestData, AxiosResponse } from './Axios';
|
||||||
|
|
||||||
export interface AxiosDomainRequest {
|
export interface AxiosDomainRequest {
|
||||||
<TData = unknown>(
|
<TData = unknown>(
|
||||||
|
@ -49,7 +50,7 @@ export interface AxiosDomainAsRequestWithData {
|
||||||
/**
|
/**
|
||||||
* 请求数据
|
* 请求数据
|
||||||
*/
|
*/
|
||||||
data?: AnyObject,
|
data?: AxiosRequestData,
|
||||||
/**
|
/**
|
||||||
* 请求配置
|
* 请求配置
|
||||||
*/
|
*/
|
||||||
|
@ -128,8 +129,8 @@ export default class AxiosDomain {
|
||||||
processRequest: AxiosDomainRequest,
|
processRequest: AxiosDomainRequest,
|
||||||
) {
|
) {
|
||||||
this.defaults = defaults;
|
this.defaults = defaults;
|
||||||
this.request = <TData = unknown>(config: AxiosRequestConfig) => {
|
this.request = (config) => {
|
||||||
return processRequest<TData>(mergeConfig(this.defaults, config));
|
return processRequest(mergeConfig(this.defaults, config));
|
||||||
};
|
};
|
||||||
|
|
||||||
this.#createAsRequests();
|
this.#createAsRequests();
|
||||||
|
@ -139,10 +140,7 @@ export default class AxiosDomain {
|
||||||
|
|
||||||
#createAsRequests() {
|
#createAsRequests() {
|
||||||
for (const alias of AxiosDomain.as) {
|
for (const alias of AxiosDomain.as) {
|
||||||
this[alias] = function processAsRequest(
|
this[alias] = function processAsRequest(url, config = {}) {
|
||||||
url: string,
|
|
||||||
config: AxiosRequestConfig = {},
|
|
||||||
) {
|
|
||||||
config.url = url;
|
config.url = url;
|
||||||
config.method = alias;
|
config.method = alias;
|
||||||
|
|
||||||
|
@ -153,14 +151,10 @@ export default class AxiosDomain {
|
||||||
|
|
||||||
#createAspRequests() {
|
#createAspRequests() {
|
||||||
for (const alias of AxiosDomain.asp) {
|
for (const alias of AxiosDomain.asp) {
|
||||||
this[alias] = function processAspRequest(
|
this[alias] = function processAspRequest(url, params = {}, config = {}) {
|
||||||
url: string,
|
|
||||||
params: AnyObject = {},
|
|
||||||
config: AxiosRequestConfig = {},
|
|
||||||
) {
|
|
||||||
config.url = url;
|
config.url = url;
|
||||||
config.method = alias;
|
config.method = alias;
|
||||||
config.params = Object.assign(params, config.params);
|
config.params = deepMerge(params, config.params ?? {});
|
||||||
|
|
||||||
return this.request(config);
|
return this.request(config);
|
||||||
};
|
};
|
||||||
|
@ -169,14 +163,10 @@ export default class AxiosDomain {
|
||||||
|
|
||||||
#createAsdRequests() {
|
#createAsdRequests() {
|
||||||
for (const alias of AxiosDomain.asd) {
|
for (const alias of AxiosDomain.asd) {
|
||||||
this[alias] = function processAsdRequest(
|
this[alias] = function processAsdRequest(url, data = {}, config = {}) {
|
||||||
url: string,
|
|
||||||
data: AxiosRequestData = {},
|
|
||||||
config: AxiosRequestConfig = {},
|
|
||||||
) {
|
|
||||||
config.url = url;
|
config.url = url;
|
||||||
config.method = alias;
|
config.method = alias;
|
||||||
config.data = Object.assign(data, config.data);
|
config.data = deepMerge(data, config.data ?? {});
|
||||||
|
|
||||||
return this.request(config);
|
return this.request(config);
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { describe, test, expect, vi } from 'vitest';
|
||||||
import AxiosDomain, { AxiosDomainRequest } from 'src/core/AxiosDomain';
|
import AxiosDomain, { AxiosDomainRequest } from 'src/core/AxiosDomain';
|
||||||
import { ignore } from 'src/helpers/ignore';
|
import { ignore } from 'src/helpers/ignore';
|
||||||
|
|
||||||
describe('src/core/Axios.ts', () => {
|
describe('src/core/AxiosDomain.ts', () => {
|
||||||
test('应该有这些静态属性', () => {
|
test('应该有这些静态属性', () => {
|
||||||
expect(AxiosDomain.as).toEqual(['options', 'trace', 'connect']);
|
expect(AxiosDomain.as).toEqual(['options', 'trace', 'connect']);
|
||||||
expect(AxiosDomain.asp).toEqual(['head', 'get', 'delete']);
|
expect(AxiosDomain.asp).toEqual(['head', 'get', 'delete']);
|
||||||
|
@ -87,4 +87,70 @@ describe('src/core/Axios.ts', () => {
|
||||||
1;
|
1;
|
||||||
expect(cb.mock.calls.length).toBe(l);
|
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));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue