From db787a2b5f7f1188d1813ddb715ef23e653120a9 Mon Sep 17 00:00:00 2001 From: zjx0905 <954270063@qq.com> Date: Tue, 25 Apr 2023 15:06:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=B8=A2=E5=A4=B1=E6=9C=AB=E5=B0=BE?= =?UTF-8?q?=E8=87=AA=E5=B8=A6=E7=9A=84=E6=96=9C=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #44 --- src/helpers/combineURL.ts | 7 ++++--- src/request/transformURL.ts | 13 ++++++------ test/helpers/combineURL.test.ts | 30 ++++++++-------------------- test/request/dispatchRequest.test.ts | 4 ++-- 4 files changed, 20 insertions(+), 34 deletions(-) diff --git a/src/helpers/combineURL.ts b/src/helpers/combineURL.ts index 9a276a5..9fa91d5 100644 --- a/src/helpers/combineURL.ts +++ b/src/helpers/combineURL.ts @@ -1,10 +1,11 @@ import { isAbsoluteURL } from './isAbsoluteURL'; -const combineRE = /(^|[^:])\/{2,}/g; -const removeRE = /\/$/; export function combineURL(baseURL = '', url = ''): string { if (isAbsoluteURL(url)) { return url; } - return `${baseURL}/${url}`.replace(combineRE, '$1/').replace(removeRE, ''); + + return url + ? `${baseURL.replace(/\/+$/, '')}/${url.replace(/^\/+/, '')}` + : baseURL; } diff --git a/src/request/transformURL.ts b/src/request/transformURL.ts index de9365e..ee1b934 100644 --- a/src/request/transformURL.ts +++ b/src/request/transformURL.ts @@ -5,13 +5,12 @@ import { dynamicURL } from '../helpers/dynamicURL'; import { AxiosRequestConfig } from '../core/Axios'; export function transformURL(config: AxiosRequestConfig) { - const data = isPlainObject(config.data) ? config.data : {}; - - let url = config.url ?? '/'; - - url = combineURL(config.baseURL ?? '', url); - url = dynamicURL(url, config.params, data); + let url = combineURL(config.baseURL, config.url); + url = dynamicURL( + url, + config.params, + isPlainObject(config.data) ? config.data : {}, + ); url = buildURL(url, config.params, config.paramsSerializer); - return url; } diff --git a/test/helpers/combineURL.test.ts b/test/helpers/combineURL.test.ts index 9a2c6c4..64456a7 100644 --- a/test/helpers/combineURL.test.ts +++ b/test/helpers/combineURL.test.ts @@ -2,41 +2,27 @@ import { describe, test, expect } from 'vitest'; import { combineURL } from '@/helpers/combineURL'; describe('src/helpers/combineURL.ts', () => { - test('应该支持空参数', () => { - expect(combineURL()).toBe(''); - expect(combineURL('')).toBe(''); - expect(combineURL(undefined, '')).toBe(''); - }); - test('应该直接返回第一个参数', () => { expect(combineURL('http://api.com', '')).toBe('http://api.com'); expect(combineURL('file://api.com', '')).toBe('file://api.com'); - expect(combineURL('unknow://api.com', '')).toBe('unknow://api.com'); }); test('应该直接返回第二个参数', () => { expect(combineURL('', 'http://api.com')).toBe('http://api.com'); expect(combineURL('', 'file://api.com')).toBe('file://api.com'); - expect(combineURL('', 'unknow://api.com')).toBe('unknow://api.com'); }); test('应该得到拼接后的结果', () => { expect(combineURL('http://api.com', 'test')).toBe('http://api.com/test'); - expect(combineURL('file://api.com', '/test')).toBe('file://api.com/test'); - expect(combineURL('unknow://api.com', '/test')).toBe( - 'unknow://api.com/test', - ); + expect(combineURL('http://api.com/', 'test')).toBe('http://api.com/test'); + expect(combineURL('http://api.com', '/test')).toBe('http://api.com/test'); + expect(combineURL('http://api.com/', '/test')).toBe('http://api.com/test'); }); - test('应该清理多余的斜线', () => { - expect(combineURL('//api//', 'test//')).toBe('/api/test'); - expect(combineURL('//api//', '//test//')).toBe('/api/test'); - expect(combineURL('////', '')).toBe(''); - expect(combineURL('', '///')).toBe(''); - expect(combineURL('http://api.com//', '')).toBe('http://api.com'); - expect(combineURL('http://api.com/', '/')).toBe('http://api.com'); - expect(combineURL('http://api.com//', '//test//')).toBe( - 'http://api.com/test', - ); + test('应该保留末尾自带的斜线', () => { + expect(combineURL('http://api.com/', '')).toBe('http://api.com/'); + expect(combineURL('http://api.com', '/')).toBe('http://api.com/'); + expect(combineURL('http://api.com/', '/')).toBe('http://api.com/'); + expect(combineURL('http://api.com', 'test/')).toBe('http://api.com/test/'); }); }); diff --git a/test/request/dispatchRequest.test.ts b/test/request/dispatchRequest.test.ts index 9893340..cb3643a 100644 --- a/test/request/dispatchRequest.test.ts +++ b/test/request/dispatchRequest.test.ts @@ -65,7 +65,7 @@ describe('src/request/dispatchRequest.ts', () => { "adapter": [Function], "headers": {}, "method": "GET", - "url": "", + "url": "/", }, "request": undefined, "response": { @@ -73,7 +73,7 @@ describe('src/request/dispatchRequest.ts', () => { "adapter": [Function], "headers": {}, "method": "GET", - "url": "", + "url": "/", }, "data": undefined, "headers": {},