Skip to content

Commit 3bd9ab8

Browse files
committed
feat: use parseURL and parseQuery from ufo
1 parent 4a8f99c commit 3bd9ab8

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

src/runtime/helpers.ts

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,65 @@
11
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
22
import { isEqual } from 'ohash';
3-
import { $URL } from 'ufo';
3+
import { parseURL, parseQuery } from 'ufo';
44

5-
function matchPaths(path: string | RegExp, url: string) {
6-
if (typeof path === 'string') {
7-
const requestURL = new $URL(url);
8-
const matchURL = new $URL(path);
5+
function matchPaths(pathToMatch: string | RegExp, url: string) {
6+
if (typeof pathToMatch === 'string') {
7+
const requestURL = parseURL(url);
8+
const matchURL = parseURL(pathToMatch);
99
return requestURL.pathname === matchURL.pathname;
1010
}
11-
return path.exec(url);
11+
return pathToMatch.exec(url);
1212
}
1313

14-
export function hasSameParams(requestParams: object, proxyParams?: object) {
15-
if (!proxyParams) return true;
16-
return isEqual(requestParams, proxyParams);
14+
export function hasSameParams(paramsToMatch: object, requestParams?: object) {
15+
return isEqual(paramsToMatch, requestParams);
1716
}
1817

1918
// eslint-disable-next-line complexity
2019
export function matchRequest(
21-
verb: string,
22-
path: string | RegExp,
20+
verbToMatch: string,
21+
pathToMatch: string | RegExp,
2322
config: AxiosRequestConfig,
24-
params?: object,
23+
paramsToMatch?: object,
2524
) {
2625
if (!config.url) return false;
27-
const samePath = matchPaths(path, config.url);
28-
const requestURL = new $URL(config.url);
26+
const samePath = matchPaths(pathToMatch, config.url);
27+
const requestURL = parseURL(config.url);
2928

30-
const sameMethod = config.method === verb;
29+
const sameMethod = config.method === verbToMatch;
3130

3231
if (!sameMethod) return false;
3332
if (!samePath) return false;
3433

35-
if (params) return hasSameParams(params, config.params || requestURL.query);
34+
const searchParams = parseQuery(requestURL.search || '');
35+
36+
if (paramsToMatch)
37+
return hasSameParams(paramsToMatch, config.params || searchParams);
3638

3739
return true;
3840
}
3941

4042
// eslint-disable-next-line complexity
4143
export function matchResponse(
42-
verb: string,
43-
path: string | RegExp,
44+
verbToMatch: string,
45+
pathToMatch: string | RegExp,
4446
response: AxiosResponse,
45-
params?: object,
47+
paramsToMatch?: object,
4648
) {
4749
const { config } = response;
4850
if (!config.url) return false;
49-
const samePath = matchPaths(path, config.url);
50-
const requestURL = new $URL(config.url);
51+
const samePath = matchPaths(pathToMatch, config.url);
52+
const requestURL = parseURL(config.url);
5153

52-
const sameMethod = config.method === verb;
54+
const sameMethod = config.method === verbToMatch;
5355

5456
if (!sameMethod) return false;
5557
if (!samePath) return false;
5658

57-
if (params) return hasSameParams(params, config.params || requestURL.query);
59+
const searchParams = parseQuery(requestURL.search || '');
60+
61+
if (paramsToMatch)
62+
return hasSameParams(paramsToMatch, config.params || searchParams);
5863

5964
return true;
6065
}

test/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ describe('axios-dev-proxy tests', () => {
182182
});
183183

184184
it('should modify response for route with specific query params in options', async () => {
185-
server.get('/').reply(200, { data: 1 }).get('/').reply(200, { data: 2 });
185+
server.get('/').reply(200, { data: 1 }).get('/').reply(200, { data: 1 });
186186

187187
proxy.onGet('/', { q: 'param' }).replyOnce(201, {
188188
data: 2,
@@ -202,7 +202,7 @@ describe('axios-dev-proxy tests', () => {
202202
.get('/?q=param')
203203
.reply(200, { data: 1 })
204204
.get('/?q=param2')
205-
.reply(200, { data: 2 });
205+
.reply(200, { data: 1 });
206206

207207
proxy.onGet('/', { q: 'param2' }).replyOnce(201, {
208208
data: 2,

0 commit comments

Comments
 (0)