Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/matcher-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { HttpMethod, MatcherUrl, RequestUrl, RouteMatcher } from './types';
import { findRequestMethod, findRequestUrl } from './internal-utils';
const pathToRegex = require('path-to-regexp');
import { Key } from 'path-to-regexp';

const pathToRegex = require('path-to-regexp');

const heuristics = [/\?\w=\w/, /\&\w=\w/, /\?\w$/, /\&\w$/];
function containsQueryParams(matcherUrl: string): boolean {
if (matcherUrl.includes('?')) {
return heuristics.some(heuristic => heuristic.test(matcherUrl));
}
return false;
}

function httpMethodHelper(matcherUrl: MatcherUrl, httpMethod: HttpMethod): RouteMatcher {
if (typeof matcherUrl === 'string') {
return MatcherUtils.combine(MatcherUtils.method(httpMethod), MatcherUtils.url(matcherUrl));
Expand Down Expand Up @@ -30,6 +39,23 @@ export default class MatcherUtils {
}

static url(matcherUrl: string): RouteMatcher {
if (containsQueryParams(matcherUrl)) {
console.warn(
`
Matching url '${matcherUrl}' seems to contain queryparameters.
This is unfortunatly not supported due to a limitation in the matching library.

If the mock-response is dependent on the queryparameter you must use the following;

mock.get('/path-without-queryparam', ({ queryParams }) => {
if (queryParams.paramName === 'paramValue') {
return mockDataGivenParam;
}
return mockDataWithoutParam;
});
`.trim()
);
}
return {
test: (input: RequestInfo, init?: RequestInit) => {
if (matcherUrl === '*') {
Expand Down