parent
bfc012b499
commit
db787a2b5f
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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/');
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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": {},
|
||||
|
|
Loading…
Reference in New Issue