fix: 移除 url 末尾的斜线
parent
222b935f68
commit
1c09ffdd91
|
@ -1,14 +1,16 @@
|
|||
import { isPlainObject, isString } from '../helpers/isTypes';
|
||||
import { AxiosRequestConfig, AxiosRequestData, AxiosResponse } from './Axios';
|
||||
import { mergeConfig } from './mergeConfig';
|
||||
|
||||
export interface AxiosDomainAsRequest {
|
||||
export interface AxiosDomainRequest {
|
||||
<TData = unknown>(
|
||||
/**
|
||||
* 请求配置
|
||||
*/
|
||||
config?: AxiosRequestConfig,
|
||||
config: AxiosRequestConfig,
|
||||
): Promise<AxiosResponse<TData>>;
|
||||
}
|
||||
|
||||
export interface AxiosDomainAsRequest {
|
||||
<TData = unknown>(
|
||||
/**
|
||||
* 请求地址
|
||||
|
@ -22,16 +24,6 @@ export interface AxiosDomainAsRequest {
|
|||
}
|
||||
|
||||
export interface AxiosDomainAsRequestWithParams {
|
||||
<TData = unknown>(
|
||||
/**
|
||||
* 请求参数
|
||||
*/
|
||||
params?: AnyObject,
|
||||
/**
|
||||
* 请求配置
|
||||
*/
|
||||
config?: AxiosRequestConfig,
|
||||
): Promise<AxiosResponse<TData>>;
|
||||
<TData = unknown>(
|
||||
/**
|
||||
* 请求地址
|
||||
|
@ -49,16 +41,6 @@ export interface AxiosDomainAsRequestWithParams {
|
|||
}
|
||||
|
||||
export interface AxiosDomainAsRequestWithData {
|
||||
<TData = unknown>(
|
||||
/**
|
||||
* 请求数据
|
||||
*/
|
||||
data?: AnyObject,
|
||||
/**
|
||||
* 请求配置
|
||||
*/
|
||||
config?: AxiosRequestConfig,
|
||||
): Promise<AxiosResponse<TData>>;
|
||||
<TData = unknown>(
|
||||
/**
|
||||
* 请求地址
|
||||
|
@ -75,10 +57,6 @@ export interface AxiosDomainAsRequestWithData {
|
|||
): Promise<AxiosResponse<TData>>;
|
||||
}
|
||||
|
||||
export interface AxiosDomainRequest {
|
||||
<TData = unknown>(config: AxiosRequestConfig): Promise<AxiosResponse<TData>>;
|
||||
}
|
||||
|
||||
export default class AxiosDomain {
|
||||
/**
|
||||
* 普通请求别名
|
||||
|
@ -162,14 +140,10 @@ export default class AxiosDomain {
|
|||
#createAsRequests() {
|
||||
for (const alias of AxiosDomain.as) {
|
||||
this[alias] = function processAsRequest(
|
||||
urlOrConfig?: string | AxiosRequestConfig,
|
||||
url: string,
|
||||
config: AxiosRequestConfig = {},
|
||||
) {
|
||||
if (isString(urlOrConfig)) {
|
||||
config.url = urlOrConfig;
|
||||
} else if (isPlainObject(urlOrConfig)) {
|
||||
config = urlOrConfig;
|
||||
}
|
||||
config.url = url;
|
||||
config.method = alias;
|
||||
|
||||
return this.request(config);
|
||||
|
@ -180,18 +154,13 @@ export default class AxiosDomain {
|
|||
#createAspRequests() {
|
||||
for (const alias of AxiosDomain.asp) {
|
||||
this[alias] = function processAspRequest(
|
||||
urlOrParams?: string | AxiosRequestConfig,
|
||||
paramsOrConfig: AxiosRequestConfig | AnyObject = {},
|
||||
url: string,
|
||||
params: AnyObject = {},
|
||||
config: AxiosRequestConfig = {},
|
||||
) {
|
||||
if (isString(urlOrParams)) {
|
||||
config.url = urlOrParams;
|
||||
config.params = paramsOrConfig;
|
||||
} else if (isPlainObject(urlOrParams)) {
|
||||
config = paramsOrConfig;
|
||||
config.params = urlOrParams;
|
||||
}
|
||||
config.url = url;
|
||||
config.method = alias;
|
||||
config.params = Object.assign(params, config.params);
|
||||
|
||||
return this.request(config);
|
||||
};
|
||||
|
@ -201,18 +170,13 @@ export default class AxiosDomain {
|
|||
#createAsdRequests() {
|
||||
for (const alias of AxiosDomain.asd) {
|
||||
this[alias] = function processAsdRequest(
|
||||
urlOrData?: string | AxiosRequestConfig,
|
||||
dataOrConfig: AxiosRequestData | AxiosRequestConfig = {},
|
||||
url: string,
|
||||
data: AxiosRequestData = {},
|
||||
config: AxiosRequestConfig = {},
|
||||
) {
|
||||
if (isString(urlOrData)) {
|
||||
config.url = urlOrData;
|
||||
config.data = dataOrConfig;
|
||||
} else if (isPlainObject(urlOrData)) {
|
||||
config = dataOrConfig;
|
||||
config.data = urlOrData;
|
||||
}
|
||||
config.url = url;
|
||||
config.method = alias;
|
||||
config.data = Object.assign(data, config.data);
|
||||
|
||||
return this.request(config);
|
||||
};
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
const combineRE = /(^|[^:])\/{2,}/g;
|
||||
const removeRE = /\/$/;
|
||||
export function combineURL(baseURL: string, url: string): string {
|
||||
return url ? `${baseURL}/${url}`.replace(combineRE, '$1/') : baseURL;
|
||||
return `${baseURL}/${url}`.replace(combineRE, '$1/').replace(removeRE, '');
|
||||
}
|
||||
|
|
|
@ -72,25 +72,19 @@ describe('src/core/Axios.ts', () => {
|
|||
|
||||
a.request(c);
|
||||
|
||||
AxiosDomain.as.forEach((k) => a[k](c));
|
||||
AxiosDomain.as.forEach((k) => a[k](c.url, ignore(c, 'url')));
|
||||
|
||||
AxiosDomain.asp.forEach((k) => a[k](c.params, ignore(c, 'params')));
|
||||
AxiosDomain.asp.forEach((k) =>
|
||||
a[k](c.url, c.params, ignore(c, 'url', 'params')),
|
||||
);
|
||||
|
||||
AxiosDomain.asd.forEach((k) => a[k](c.data, ignore(c, 'data')));
|
||||
AxiosDomain.asd.forEach((k) =>
|
||||
a[k](c.url, c.data, ignore(c, 'url', 'data')),
|
||||
);
|
||||
|
||||
const t =
|
||||
(AxiosDomain.as.length +
|
||||
const l =
|
||||
AxiosDomain.as.length +
|
||||
AxiosDomain.asp.length +
|
||||
AxiosDomain.asd.length) *
|
||||
2 +
|
||||
AxiosDomain.asd.length +
|
||||
1;
|
||||
expect(cb.mock.calls.length).toBe(t);
|
||||
expect(cb.mock.calls.length).toBe(l);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -17,10 +17,14 @@ describe('src/helpers/combineURL.ts', () => {
|
|||
});
|
||||
|
||||
test('应该清理多余的斜线', () => {
|
||||
expect(combineURL('//api//', 'test//')).toBe('/api/test/');
|
||||
expect(combineURL('//api//', '//test//')).toBe('/api/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/',
|
||||
'http://api.com/test',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue