Skip to content

Commit 0ab6586

Browse files
committed
feat: Add regex support to match urls
1 parent b1a4e2b commit 0ab6586

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

src/runtime/handler.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ export default class Handler {
1212

1313
verb!: string;
1414

15-
path!: string;
15+
path!: string | RegExp;
1616

1717
params?: object;
1818

19-
constructor(scope: Proxy, verb: string, path: string, params?: object) {
19+
constructor(
20+
scope: Proxy,
21+
verb: string,
22+
path: string | RegExp,
23+
params?: object,
24+
) {
2025
this.scope = scope;
2126
this.verb = verb;
2227
this.path = path;

src/runtime/helpers.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
22
import { isEqual } from 'ohash';
33
import { $URL } 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);
9+
return requestURL.pathname === matchURL.pathname;
10+
}
11+
return path.exec(url);
12+
}
13+
514
export function hasSameParams(requestParams: object, proxyParams?: object) {
615
if (!proxyParams) return true;
716
return isEqual(requestParams, proxyParams);
@@ -10,15 +19,15 @@ export function hasSameParams(requestParams: object, proxyParams?: object) {
1019
// eslint-disable-next-line complexity
1120
export function matchRequest(
1221
verb: string,
13-
path: string,
22+
path: string | RegExp,
1423
config: AxiosRequestConfig,
1524
params?: object,
1625
) {
26+
if (!config.url) return false;
27+
const samePath = matchPaths(path, config.url);
1728
const requestURL = new $URL(config.url);
18-
const matchURL = new $URL(path);
1929

2030
const sameMethod = config.method === verb;
21-
const samePath = requestURL.pathname === matchURL.pathname;
2231

2332
if (!sameMethod) return false;
2433
if (!samePath) return false;

src/runtime/proxy.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default class Proxy {
1515
this.axios = axios;
1616
}
1717

18-
private setup(verb: string, path: string, params?: object) {
18+
private setup(verb: string, path: string | RegExp, params?: object) {
1919
const handler = new Handler(this, verb, path, params);
2020
return handler;
2121
}
@@ -24,19 +24,19 @@ export default class Proxy {
2424
clearAll(this.axios);
2525
}
2626

27-
onGet(path: string, params?: object) {
27+
onGet(path: string | RegExp, params?: object) {
2828
return this.setup('get', path, params);
2929
}
3030

31-
onPost(path: string, params?: object) {
31+
onPost(path: string | RegExp, params?: object) {
3232
return this.setup('post', path, params);
3333
}
3434

35-
onPut(path: string, params?: object) {
35+
onPut(path: string | RegExp, params?: object) {
3636
return this.setup('put', path, params);
3737
}
3838

39-
onPatch(path: string, params?: object) {
39+
onPatch(path: string | RegExp, params?: object) {
4040
return this.setup('patch', path, params);
4141
}
4242
}

test/index.test.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
/* eslint-disable max-lines */
12
import axios from 'axios';
23
import nock from 'nock';
34
import { expect, it, describe, beforeEach, vi } from 'vitest';
45
import { defineProxy } from '../src';
56

67
const BASE_URL = 'https://api.com.br';
7-
function to(promise: Promise<unknown>) {
8-
return promise
9-
.then(value => [undefined, value])
10-
.catch(error => [error, undefined]);
8+
async function to(promise: Promise<unknown>) {
9+
try {
10+
const value = await promise;
11+
return [undefined, value];
12+
} catch (error) {
13+
return [error, undefined];
14+
}
1115
}
1216

1317
describe('axios-dev-proxy tests', () => {
@@ -212,6 +216,17 @@ describe('axios-dev-proxy tests', () => {
212216
expect(response2.data).toEqual({ data: 2 });
213217
expect(response2.status).toEqual(201);
214218
});
219+
220+
it('should modify response for regex route', async () => {
221+
server.get('/test/2?q=2').reply(200, { data: 1 });
222+
223+
proxy.onGet(/\/test\/\d+/).replyOnce(201, {
224+
data: 2,
225+
});
226+
const response = await api.get('/test/2?q=2');
227+
expect(response.data).toEqual({ data: 2 });
228+
expect(response.status).toEqual(201);
229+
});
215230
});
216231

217232
describe('always GET configs', () => {

0 commit comments

Comments
 (0)