fix: 清理 url 前面多余的斜线
parent
7a61fda10e
commit
666a9427d3
|
@ -27,10 +27,10 @@ jobs:
|
|||
run: pnpm i
|
||||
|
||||
- name: Check prerelease
|
||||
uses: actions/github-script@v6
|
||||
id: prerelease
|
||||
uses: actions/github-script@v6
|
||||
with:
|
||||
script: return /-/.test("${{github.ref}}")
|
||||
script: return /-/.test("${{ github.ref }}")
|
||||
|
||||
- name: Create Release
|
||||
id: create_release
|
||||
|
|
|
@ -3,15 +3,13 @@ export function asyncNext() {
|
|||
}
|
||||
|
||||
export function asyncTimeout(delay = 0) {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, delay);
|
||||
});
|
||||
return new Promise((resolve) => setTimeout(resolve, delay));
|
||||
}
|
||||
|
||||
export function captureError<T = any>(fn: () => void): T {
|
||||
try {
|
||||
fn();
|
||||
throw new Error('fn not fail...');
|
||||
throw new Error('without Error');
|
||||
} catch (err) {
|
||||
return err as T;
|
||||
}
|
||||
|
@ -35,16 +33,10 @@ export function mockResponse(
|
|||
};
|
||||
}
|
||||
|
||||
export function mockSuccessResponse(
|
||||
headers: AnyObject = {},
|
||||
data: AnyObject = {},
|
||||
) {
|
||||
export function mockSuccess(headers: AnyObject = {}, data: AnyObject = {}) {
|
||||
return mockResponse(200, 'OK', headers, data);
|
||||
}
|
||||
|
||||
export function mockFailResponse(
|
||||
headers: AnyObject = {},
|
||||
data: AnyObject = {},
|
||||
) {
|
||||
export function mockFail(headers: AnyObject = {}, data: AnyObject = {}) {
|
||||
return mockResponse(400, 'FAIL', headers, data);
|
||||
}
|
||||
|
|
|
@ -1,18 +1,16 @@
|
|||
import { buildURL } from '../helpers/buildURL';
|
||||
import { combineURL } from '../helpers/combineURL';
|
||||
import { dynamicInterpolation, isDynamicURL } from '../helpers/dynamicURL';
|
||||
import { interpolation, isDynamicURL } from '../helpers/dynamicURL';
|
||||
import { isAbsoluteURL } from '../helpers/isAbsoluteURL';
|
||||
import { AxiosRequestConfig } from './Axios';
|
||||
|
||||
export function transformURL(config: AxiosRequestConfig): string {
|
||||
let url = config.url ?? '';
|
||||
|
||||
if (!isAbsoluteURL(url)) url = combineURL(config.baseURL, url);
|
||||
if (!isAbsoluteURL(url)) url = combineURL(config.baseURL ?? '', url);
|
||||
|
||||
if (isDynamicURL(url)) {
|
||||
const sourceData = Object.assign({}, config.params, config.data);
|
||||
url = dynamicInterpolation(url, sourceData);
|
||||
}
|
||||
if (isDynamicURL(url))
|
||||
url = interpolation(url, Object.assign({}, config.params, config.data));
|
||||
|
||||
url = buildURL(url, config.params, config.paramsSerializer);
|
||||
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
const combineREG = /([^:])\/{2,}/g;
|
||||
export function combineURL(baseURL = '', url: string): string {
|
||||
const separator = '/';
|
||||
const replaceStr = `$1${separator}`;
|
||||
|
||||
return `${baseURL}${separator}${url}`.replace(combineREG, replaceStr);
|
||||
const combineREG = /(^|[^:])\/{2,}/g;
|
||||
export function combineURL(baseURL: string, url: string): string {
|
||||
return url ? `${baseURL}/${url}`.replace(combineREG, '$1/') : baseURL;
|
||||
}
|
||||
|
|
|
@ -2,10 +2,7 @@ import { isPlainObject } from './isTypes';
|
|||
|
||||
const dynamicREG = /\/?(:([a-zA-Z_$][\w-$]*))\/??/g;
|
||||
|
||||
export function dynamicInterpolation(
|
||||
url: string,
|
||||
sourceData?: unknown,
|
||||
): string {
|
||||
export function interpolation(url: string, sourceData?: unknown): string {
|
||||
if (!isPlainObject(sourceData)) {
|
||||
return url;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ import { describe, test, expect, vi } from 'vitest';
|
|||
import {
|
||||
asyncNext,
|
||||
captureError,
|
||||
mockSuccessResponse,
|
||||
mockSuccess,
|
||||
noop,
|
||||
asyncTimeout,
|
||||
} from 'scripts/test.utils';
|
||||
|
@ -100,7 +100,7 @@ describe('测试 src/helpers/cancel.ts', () => {
|
|||
|
||||
source.cancel();
|
||||
axios({
|
||||
adapter: ({ success }) => success(mockSuccessResponse()),
|
||||
adapter: ({ success }) => success(mockSuccess()),
|
||||
cancelToken: source.token,
|
||||
}).catch(canceled);
|
||||
|
||||
|
@ -114,7 +114,7 @@ describe('测试 src/helpers/cancel.ts', () => {
|
|||
const source = CancelToken.source();
|
||||
|
||||
axios({
|
||||
adapter: ({ success }) => success(mockSuccessResponse()),
|
||||
adapter: ({ success }) => success(mockSuccess()),
|
||||
cancelToken: source.token,
|
||||
}).catch(canceled);
|
||||
source.cancel();
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
import { describe, test, expect } from 'vitest';
|
||||
import { combineURL } from 'src/helpers/combineURL';
|
||||
|
||||
describe('测试 src/helpers/combineURL.ts', () => {
|
||||
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', '/user')).toBe('http://api.com/user');
|
||||
expect(combineURL('file://api.com', '/user')).toBe('file://api.com/user');
|
||||
expect(combineURL('unknow://api.com', '/user')).toBe(
|
||||
'unknow://api.com/user',
|
||||
);
|
||||
});
|
||||
|
||||
test('应该清理多余的斜线', () => {
|
||||
expect(combineURL('//api//', '//user//')).toBe('/api/user/');
|
||||
expect(combineURL('http://api.com//', '//user//')).toBe(
|
||||
'http://api.com/user/',
|
||||
);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue