Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove querystring dependency #693

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
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
12 changes: 1 addition & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/fetch-mock-legacy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
"glob-to-regexp": "^0.4.0",
"is-subset": "^0.1.1",
"lodash.isequal": "^4.5.0",
"path-to-regexp": "^2.2.1",
"querystring": "^0.2.1"
"path-to-regexp": "^2.2.1"
},
"peerDependenciesMeta": {
"node-fetch": {
Expand Down
57 changes: 44 additions & 13 deletions packages/fetch-mock-legacy/src/Route/matchers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import glob from 'glob-to-regexp';
import pathToRegexp from 'path-to-regexp';
import querystring from 'querystring';
import isSubset from 'is-subset';
import isEqual from 'lodash.isequal';
import {
Expand Down Expand Up @@ -78,22 +77,54 @@ const getQueryStringMatcher = ({ query: passedQuery }) => {
debug(' No query parameters expectations defined - skipping');
return;
}
const expectedQuery = querystring.parse(querystring.stringify(passedQuery));
debug(' Expected query parameters:', passedQuery);
const keys = Object.keys(expectedQuery);

const expectedQuery = new URLSearchParams();
for (const [key, value] of Object.entries(passedQuery)) {
if (Array.isArray(value)) {
for (const item of value) {
expectedQuery.append(
key,
typeof item === 'object' || typeof item === 'undefined'
? ''
: item.toString(),
);
}
} else {
expectedQuery.append(
key,
typeof value === 'object' || typeof value === 'undefined'
? ''
: value.toString(),
);
}
}

const keys = Array.from(expectedQuery.keys());
return (url) => {
debug('Attempting to match query parameters');
const query = querystring.parse(getQuery(url));
debug(' Expected query parameters:', expectedQuery);
debug(' Actual query parameters:', query);
const queryString = getQuery(url);
const query = new URLSearchParams(queryString);
debug(
' Expected query parameters:',
Object.fromEntries(expectedQuery.entries()),
);
debug(' Actual query parameters:', Object.fromEntries(query.entries()));

return keys.every((key) => {
if (Array.isArray(query[key])) {
if (!Array.isArray(expectedQuery[key])) {
return false;
}
return isEqual(query[key].sort(), expectedQuery[key].sort());
const expectedValues = expectedQuery.getAll(key).sort();
const actualValues = query.getAll(key).sort();

if (expectedValues.length !== actualValues.length) {
return false;
}
return query[key] === expectedQuery[key];

if (Array.isArray(passedQuery[key])) {
return expectedValues.every(
(expected, index) => expected === actualValues[index],
);
}

return isEqual(actualValues, expectedValues);
});
};
};
Expand Down