fix: 清理 url 前面多余的斜线

pull/41/head
zjx0905 2023-04-01 23:11:55 +08:00
parent 7a61fda10e
commit 666a9427d3
7 changed files with 42 additions and 33 deletions

View File

@ -27,10 +27,10 @@ jobs:
run: pnpm i run: pnpm i
- name: Check prerelease - name: Check prerelease
uses: actions/github-script@v6
id: prerelease id: prerelease
uses: actions/github-script@v6
with: with:
script: return /-/.test("${{github.ref}}") script: return /-/.test("${{ github.ref }}")
- name: Create Release - name: Create Release
id: create_release id: create_release

View File

@ -3,15 +3,13 @@ export function asyncNext() {
} }
export function asyncTimeout(delay = 0) { export function asyncTimeout(delay = 0) {
return new Promise((resolve) => { return new Promise((resolve) => setTimeout(resolve, delay));
setTimeout(resolve, delay);
});
} }
export function captureError<T = any>(fn: () => void): T { export function captureError<T = any>(fn: () => void): T {
try { try {
fn(); fn();
throw new Error('fn not fail...'); throw new Error('without Error');
} catch (err) { } catch (err) {
return err as T; return err as T;
} }
@ -35,16 +33,10 @@ export function mockResponse(
}; };
} }
export function mockSuccessResponse( export function mockSuccess(headers: AnyObject = {}, data: AnyObject = {}) {
headers: AnyObject = {},
data: AnyObject = {},
) {
return mockResponse(200, 'OK', headers, data); return mockResponse(200, 'OK', headers, data);
} }
export function mockFailResponse( export function mockFail(headers: AnyObject = {}, data: AnyObject = {}) {
headers: AnyObject = {},
data: AnyObject = {},
) {
return mockResponse(400, 'FAIL', headers, data); return mockResponse(400, 'FAIL', headers, data);
} }

View File

@ -1,18 +1,16 @@
import { buildURL } from '../helpers/buildURL'; import { buildURL } from '../helpers/buildURL';
import { combineURL } from '../helpers/combineURL'; import { combineURL } from '../helpers/combineURL';
import { dynamicInterpolation, isDynamicURL } from '../helpers/dynamicURL'; import { interpolation, isDynamicURL } from '../helpers/dynamicURL';
import { isAbsoluteURL } from '../helpers/isAbsoluteURL'; import { isAbsoluteURL } from '../helpers/isAbsoluteURL';
import { AxiosRequestConfig } from './Axios'; import { AxiosRequestConfig } from './Axios';
export function transformURL(config: AxiosRequestConfig): string { export function transformURL(config: AxiosRequestConfig): string {
let url = config.url ?? ''; let url = config.url ?? '';
if (!isAbsoluteURL(url)) url = combineURL(config.baseURL, url); if (!isAbsoluteURL(url)) url = combineURL(config.baseURL ?? '', url);
if (isDynamicURL(url)) { if (isDynamicURL(url))
const sourceData = Object.assign({}, config.params, config.data); url = interpolation(url, Object.assign({}, config.params, config.data));
url = dynamicInterpolation(url, sourceData);
}
url = buildURL(url, config.params, config.paramsSerializer); url = buildURL(url, config.params, config.paramsSerializer);

View File

@ -1,7 +1,4 @@
const combineREG = /([^:])\/{2,}/g; const combineREG = /(^|[^:])\/{2,}/g;
export function combineURL(baseURL = '', url: string): string { export function combineURL(baseURL: string, url: string): string {
const separator = '/'; return url ? `${baseURL}/${url}`.replace(combineREG, '$1/') : baseURL;
const replaceStr = `$1${separator}`;
return `${baseURL}${separator}${url}`.replace(combineREG, replaceStr);
} }

View File

@ -2,10 +2,7 @@ import { isPlainObject } from './isTypes';
const dynamicREG = /\/?(:([a-zA-Z_$][\w-$]*))\/??/g; const dynamicREG = /\/?(:([a-zA-Z_$][\w-$]*))\/??/g;
export function dynamicInterpolation( export function interpolation(url: string, sourceData?: unknown): string {
url: string,
sourceData?: unknown,
): string {
if (!isPlainObject(sourceData)) { if (!isPlainObject(sourceData)) {
return url; return url;
} }

View File

@ -2,7 +2,7 @@ import { describe, test, expect, vi } from 'vitest';
import { import {
asyncNext, asyncNext,
captureError, captureError,
mockSuccessResponse, mockSuccess,
noop, noop,
asyncTimeout, asyncTimeout,
} from 'scripts/test.utils'; } from 'scripts/test.utils';
@ -100,7 +100,7 @@ describe('测试 src/helpers/cancel.ts', () => {
source.cancel(); source.cancel();
axios({ axios({
adapter: ({ success }) => success(mockSuccessResponse()), adapter: ({ success }) => success(mockSuccess()),
cancelToken: source.token, cancelToken: source.token,
}).catch(canceled); }).catch(canceled);
@ -114,7 +114,7 @@ describe('测试 src/helpers/cancel.ts', () => {
const source = CancelToken.source(); const source = CancelToken.source();
axios({ axios({
adapter: ({ success }) => success(mockSuccessResponse()), adapter: ({ success }) => success(mockSuccess()),
cancelToken: source.token, cancelToken: source.token,
}).catch(canceled); }).catch(canceled);
source.cancel(); source.cancel();

View File

@ -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/',
);
});
});