diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index feca74e..5007db5 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 diff --git a/scripts/test.utils.ts b/scripts/test.utils.ts index 92e97ea..598bf5f 100644 --- a/scripts/test.utils.ts +++ b/scripts/test.utils.ts @@ -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(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); } diff --git a/src/core/transformURL.ts b/src/core/transformURL.ts index b3c8bcd..f0c3baa 100644 --- a/src/core/transformURL.ts +++ b/src/core/transformURL.ts @@ -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); diff --git a/src/helpers/combineURL.ts b/src/helpers/combineURL.ts index 5ebeb68..123ec8d 100644 --- a/src/helpers/combineURL.ts +++ b/src/helpers/combineURL.ts @@ -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; } diff --git a/src/helpers/dynamicURL.ts b/src/helpers/dynamicURL.ts index dec0166..4bd4a1f 100644 --- a/src/helpers/dynamicURL.ts +++ b/src/helpers/dynamicURL.ts @@ -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; } diff --git a/test/core/cancel.test.ts b/test/core/cancel.test.ts index 69cd3bb..b33a9ea 100644 --- a/test/core/cancel.test.ts +++ b/test/core/cancel.test.ts @@ -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(); diff --git a/test/helpers/combineURL.test.ts b/test/helpers/combineURL.test.ts new file mode 100644 index 0000000..a4d8fbb --- /dev/null +++ b/test/helpers/combineURL.test.ts @@ -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/', + ); + }); +});