From 22f65cf69c877f314b269d8c3d0fac8f1c8ab71f Mon Sep 17 00:00:00 2001 From: zjx0905 <954270063@qq.com> Date: Fri, 7 Apr 2023 12:39:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=E5=90=88=E5=B9=B6=20params=20&=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/Axios.ts | 15 ++++---- src/core/AxiosDomain.ts | 30 ++++++---------- test/core/AxiosDomain.test.ts | 68 ++++++++++++++++++++++++++++++++++- 3 files changed, 86 insertions(+), 27 deletions(-) diff --git a/src/core/Axios.ts b/src/core/Axios.ts index 3737a02..136f6cd 100644 --- a/src/core/Axios.ts +++ b/src/core/Axios.ts @@ -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 = (config: AxiosRequestConfig) => { + #processRequest(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>; - }; + } } diff --git a/src/core/AxiosDomain.ts b/src/core/AxiosDomain.ts index 95ba7a6..4881e93 100644 --- a/src/core/AxiosDomain.ts +++ b/src/core/AxiosDomain.ts @@ -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 { ( @@ -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 = (config: AxiosRequestConfig) => { - return processRequest(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); }; diff --git a/test/core/AxiosDomain.test.ts b/test/core/AxiosDomain.test.ts index 15c9456..4807c15 100644 --- a/test/core/AxiosDomain.test.ts +++ b/test/core/AxiosDomain.test.ts @@ -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)); + }); });