|
1 | 1 | import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; |
2 | 2 | import { isEqual } from 'ohash'; |
3 | | -import { $URL } from 'ufo'; |
| 3 | +import { parseURL, parseQuery } from 'ufo'; |
4 | 4 |
|
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); |
9 | 9 | return requestURL.pathname === matchURL.pathname; |
10 | 10 | } |
11 | | - return path.exec(url); |
| 11 | + return pathToMatch.exec(url); |
12 | 12 | } |
13 | 13 |
|
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); |
17 | 16 | } |
18 | 17 |
|
19 | 18 | // eslint-disable-next-line complexity |
20 | 19 | export function matchRequest( |
21 | | - verb: string, |
22 | | - path: string | RegExp, |
| 20 | + verbToMatch: string, |
| 21 | + pathToMatch: string | RegExp, |
23 | 22 | config: AxiosRequestConfig, |
24 | | - params?: object, |
| 23 | + paramsToMatch?: object, |
25 | 24 | ) { |
26 | 25 | 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); |
29 | 28 |
|
30 | | - const sameMethod = config.method === verb; |
| 29 | + const sameMethod = config.method === verbToMatch; |
31 | 30 |
|
32 | 31 | if (!sameMethod) return false; |
33 | 32 | if (!samePath) return false; |
34 | 33 |
|
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); |
36 | 38 |
|
37 | 39 | return true; |
38 | 40 | } |
39 | 41 |
|
40 | 42 | // eslint-disable-next-line complexity |
41 | 43 | export function matchResponse( |
42 | | - verb: string, |
43 | | - path: string | RegExp, |
| 44 | + verbToMatch: string, |
| 45 | + pathToMatch: string | RegExp, |
44 | 46 | response: AxiosResponse, |
45 | | - params?: object, |
| 47 | + paramsToMatch?: object, |
46 | 48 | ) { |
47 | 49 | const { config } = response; |
48 | 50 | 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); |
51 | 53 |
|
52 | | - const sameMethod = config.method === verb; |
| 54 | + const sameMethod = config.method === verbToMatch; |
53 | 55 |
|
54 | 56 | if (!sameMethod) return false; |
55 | 57 | if (!samePath) return false; |
56 | 58 |
|
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); |
58 | 63 |
|
59 | 64 | return true; |
60 | 65 | } |
|
0 commit comments