fix: 丢失末尾自带的斜线

#44
pull/49/head
zjx0905 2023-04-25 15:06:19 +08:00
parent bfc012b499
commit db787a2b5f
4 changed files with 20 additions and 34 deletions

View File

@ -1,10 +1,11 @@
import { isAbsoluteURL } from './isAbsoluteURL'; import { isAbsoluteURL } from './isAbsoluteURL';
const combineRE = /(^|[^:])\/{2,}/g;
const removeRE = /\/$/;
export function combineURL(baseURL = '', url = ''): string { export function combineURL(baseURL = '', url = ''): string {
if (isAbsoluteURL(url)) { if (isAbsoluteURL(url)) {
return url; return url;
} }
return `${baseURL}/${url}`.replace(combineRE, '$1/').replace(removeRE, '');
return url
? `${baseURL.replace(/\/+$/, '')}/${url.replace(/^\/+/, '')}`
: baseURL;
} }

View File

@ -5,13 +5,12 @@ import { dynamicURL } from '../helpers/dynamicURL';
import { AxiosRequestConfig } from '../core/Axios'; import { AxiosRequestConfig } from '../core/Axios';
export function transformURL(config: AxiosRequestConfig) { export function transformURL(config: AxiosRequestConfig) {
const data = isPlainObject(config.data) ? config.data : {}; let url = combineURL(config.baseURL, config.url);
url = dynamicURL(
let url = config.url ?? '/'; url,
config.params,
url = combineURL(config.baseURL ?? '', url); isPlainObject(config.data) ? config.data : {},
url = dynamicURL(url, config.params, data); );
url = buildURL(url, config.params, config.paramsSerializer); url = buildURL(url, config.params, config.paramsSerializer);
return url; return url;
} }

View File

@ -2,41 +2,27 @@ import { describe, test, expect } from 'vitest';
import { combineURL } from '@/helpers/combineURL'; import { combineURL } from '@/helpers/combineURL';
describe('src/helpers/combineURL.ts', () => { describe('src/helpers/combineURL.ts', () => {
test('应该支持空参数', () => {
expect(combineURL()).toBe('');
expect(combineURL('')).toBe('');
expect(combineURL(undefined, '')).toBe('');
});
test('应该直接返回第一个参数', () => { test('应该直接返回第一个参数', () => {
expect(combineURL('http://api.com', '')).toBe('http://api.com'); expect(combineURL('http://api.com', '')).toBe('http://api.com');
expect(combineURL('file://api.com', '')).toBe('file://api.com'); expect(combineURL('file://api.com', '')).toBe('file://api.com');
expect(combineURL('unknow://api.com', '')).toBe('unknow://api.com');
}); });
test('应该直接返回第二个参数', () => { test('应该直接返回第二个参数', () => {
expect(combineURL('', 'http://api.com')).toBe('http://api.com'); expect(combineURL('', 'http://api.com')).toBe('http://api.com');
expect(combineURL('', 'file://api.com')).toBe('file://api.com'); expect(combineURL('', 'file://api.com')).toBe('file://api.com');
expect(combineURL('', 'unknow://api.com')).toBe('unknow://api.com');
}); });
test('应该得到拼接后的结果', () => { 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('file://api.com', '/test')).toBe('file://api.com/test'); expect(combineURL('http://api.com/', 'test')).toBe('http://api.com/test');
expect(combineURL('unknow://api.com', '/test')).toBe( expect(combineURL('http://api.com', '/test')).toBe('http://api.com/test');
'unknow://api.com/test', expect(combineURL('http://api.com/', '/test')).toBe('http://api.com/test');
);
}); });
test('应该清理多余的斜线', () => { test('应该保留末尾自带的斜线', () => {
expect(combineURL('//api//', 'test//')).toBe('/api/test'); expect(combineURL('http://api.com/', '')).toBe('http://api.com/');
expect(combineURL('//api//', '//test//')).toBe('/api/test'); expect(combineURL('http://api.com', '/')).toBe('http://api.com/');
expect(combineURL('////', '')).toBe(''); expect(combineURL('http://api.com/', '/')).toBe('http://api.com/');
expect(combineURL('', '///')).toBe(''); expect(combineURL('http://api.com', 'test/')).toBe('http://api.com/test/');
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',
);
}); });
}); });

View File

@ -65,7 +65,7 @@ describe('src/request/dispatchRequest.ts', () => {
"adapter": [Function], "adapter": [Function],
"headers": {}, "headers": {},
"method": "GET", "method": "GET",
"url": "", "url": "/",
}, },
"request": undefined, "request": undefined,
"response": { "response": {
@ -73,7 +73,7 @@ describe('src/request/dispatchRequest.ts', () => {
"adapter": [Function], "adapter": [Function],
"headers": {}, "headers": {},
"method": "GET", "method": "GET",
"url": "", "url": "/",
}, },
"data": undefined, "data": undefined,
"headers": {}, "headers": {},