|
1 | 1 | import { HttpMethod, MatcherUrl, RequestUrl, RouteMatcher } from './types'; |
2 | 2 | import { findRequestMethod, findRequestUrl } from './internal-utils'; |
3 | | -const pathToRegex = require('path-to-regexp'); |
4 | 3 | import { Key } from 'path-to-regexp'; |
5 | 4 |
|
| 5 | +const pathToRegex = require('path-to-regexp'); |
| 6 | + |
| 7 | +const heuristics = [/\?\w=\w/, /\&\w=\w/, /\?\w$/, /\&\w$/]; |
| 8 | +function containsQueryParams(matcherUrl: string): boolean { |
| 9 | + if (matcherUrl.includes('?')) { |
| 10 | + return heuristics.some(heuristic => heuristic.test(matcherUrl)); |
| 11 | + } |
| 12 | + return false; |
| 13 | +} |
| 14 | + |
6 | 15 | function httpMethodHelper(matcherUrl: MatcherUrl, httpMethod: HttpMethod): RouteMatcher { |
7 | 16 | if (typeof matcherUrl === 'string') { |
8 | 17 | return MatcherUtils.combine(MatcherUtils.method(httpMethod), MatcherUtils.url(matcherUrl)); |
@@ -30,6 +39,23 @@ export default class MatcherUtils { |
30 | 39 | } |
31 | 40 |
|
32 | 41 | static url(matcherUrl: string): RouteMatcher { |
| 42 | + if (containsQueryParams(matcherUrl)) { |
| 43 | + console.warn( |
| 44 | + ` |
| 45 | +Matching url '${matcherUrl}' seems to contain queryparameters. |
| 46 | +This is unfortunatly not supported due to a limitation in the matching library. |
| 47 | +
|
| 48 | +If the mock-response is dependent on the queryparameter you must use the following; |
| 49 | +
|
| 50 | +mock.get('/path-without-queryparam', ({ queryParams }) => { |
| 51 | + if (queryParams.paramName === 'paramValue') { |
| 52 | + return mockDataGivenParam; |
| 53 | + } |
| 54 | + return mockDataWithoutParam; |
| 55 | +}); |
| 56 | + `.trim() |
| 57 | + ); |
| 58 | + } |
33 | 59 | return { |
34 | 60 | test: (input: RequestInfo, init?: RequestInit) => { |
35 | 61 | if (matcherUrl === '*') { |
|
0 commit comments