Skip to content

Commit 531c06c

Browse files
committed
fix: Fixes match url with query params
1 parent f668dae commit 531c06c

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
},
4444
"dependencies": {
4545
"axios": "1.4.0",
46-
"ohash": "1.1.3"
46+
"ohash": "1.1.3",
47+
"ufo": "1.3.0"
4748
}
48-
}
49+
}

src/runtime/helpers.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
22
import { isEqual } from 'ohash';
3+
import { $URL } from 'ufo';
34

45
export function hasSameParams(requestParams: object, proxyParams?: object) {
56
if (!proxyParams) return true;
67
return isEqual(requestParams, proxyParams);
78
}
89

10+
// eslint-disable-next-line complexity
911
export function matchRequest(
1012
verb: string,
1113
path: string,
1214
config: AxiosRequestConfig,
1315
params?: object,
1416
) {
15-
return (
16-
config.method === verb &&
17-
config.url === path &&
18-
hasSameParams(config.params, params)
19-
);
17+
const requestURL = new $URL(config.url);
18+
const matchURL = new $URL(path);
19+
20+
const sameMethod = config.method === verb;
21+
const samePath = requestURL.pathname === matchURL.pathname;
22+
23+
if (!sameMethod) return false;
24+
if (!samePath) return false;
25+
26+
if (params) return hasSameParams(params, config.params || requestURL.query);
27+
28+
return true;
2029
}
2130

2231
export function matchResponse(

test/index.test.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ describe('axios-dev-proxy tests', () => {
153153
expect(responseGet.status).toEqual(201);
154154
});
155155

156-
it('should modify response with query params', async () => {
156+
it('should modify response with query params in config', async () => {
157157
server.get('/').reply(200, { data: 1 });
158158

159159
proxy.onGet('/').replyOnce(201, {
@@ -165,7 +165,19 @@ describe('axios-dev-proxy tests', () => {
165165
expect(response.status).toEqual(201);
166166
});
167167

168-
it('should modify response for route with specific query params', async () => {
168+
it('should modify response with query params in path', async () => {
169+
server.get('/?q=2').reply(200, { data: 1 });
170+
171+
proxy.onGet('/').replyOnce(201, {
172+
data: 2,
173+
});
174+
175+
const response = await api.get('/?q=2');
176+
expect(response.data).toEqual({ data: 2 });
177+
expect(response.status).toEqual(201);
178+
});
179+
180+
it('should modify response for route with specific query params in options', async () => {
169181
server.get('/').reply(200, { data: 1 }).get('/').reply(200, { data: 2 });
170182

171183
proxy.onGet('/', { q: 'param' }).replyOnce(201, {
@@ -180,6 +192,26 @@ describe('axios-dev-proxy tests', () => {
180192
expect(response2.data).toEqual({ data: 2 });
181193
expect(response2.status).toEqual(201);
182194
});
195+
196+
it('should modify response for route with specific query params in path', async () => {
197+
server
198+
.get('/?q=param')
199+
.reply(200, { data: 1 })
200+
.get('/?q=param2')
201+
.reply(200, { data: 2 });
202+
203+
proxy.onGet('/', { q: 'param2' }).replyOnce(201, {
204+
data: 2,
205+
});
206+
207+
const response = await api.get('/?q=param');
208+
expect(response.data).toEqual({ data: 1 });
209+
expect(response.status).toEqual(200);
210+
211+
const response2 = await api.get('/?q=param2');
212+
expect(response2.data).toEqual({ data: 2 });
213+
expect(response2.status).toEqual(201);
214+
});
183215
});
184216

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

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3613,6 +3613,11 @@ [email protected], typescript@^5.0.4:
36133613
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.1.6.tgz#02f8ac202b6dad2c0dd5e0913745b47a37998274"
36143614
integrity sha512-zaWCozRZ6DLEWAWFrVDz1H6FVXzUSfTy5FUMWsQlU8Ym5JP9eO4xkTIROFCQvhQf61z6O/G6ugw3SgAnvvm+HA==
36153615

3616+
3617+
version "1.3.0"
3618+
resolved "https://registry.yarnpkg.com/ufo/-/ufo-1.3.0.tgz#c92f8ac209daff607c57bbd75029e190930a0019"
3619+
integrity sha512-bRn3CsoojyNStCZe0BG0Mt4Nr/4KF+rhFlnNXybgqt5pXHNFRlqinSoQaTrGyzE4X8aHplSb+TorH+COin9Yxw==
3620+
36163621
ufo@^1.1.2:
36173622
version "1.2.0"
36183623
resolved "https://registry.yarnpkg.com/ufo/-/ufo-1.2.0.tgz#28d127a087a46729133fdc89cb1358508b3f80ba"

0 commit comments

Comments
 (0)