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

feat(core,schema): enable skipping encoding specific chars from query params (9.x) #499

Merged
merged 1 commit into from
Feb 28, 2022
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
21 changes: 19 additions & 2 deletions packages/core/src/http-middlewares/before/add-query-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,30 @@ const hasQueryParams = ({ params = {} }) => Object.keys(params).length;
// Take params off of req.params and append to url - "?a=1&b=2"".
// This middleware should run *after* custom middlewares, because
// custom middlewares might add params.
const addQueryParams = req => {
const addQueryParams = (req) => {
if (hasQueryParams(req)) {
const splitter = req.url.includes('?') ? '&' : '?';

normalizeEmptyParamFields(req);

const stringifiedParams = querystring.stringify(req.params);
let stringifiedParams = querystring.stringify(req.params);

// it goes against spec, but for compatibility, some APIs want certain
// characters (mostly $) unencoded
if (req.skipEncodingChars) {
for (let i = 0; i < req.skipEncodingChars.length; i++) {
const char = req.skipEncodingChars.charAt(i);
const valToReplace = querystring.escape(char);
if (valToReplace === char) {
continue;
}
// no replaceAll in JS yet, coming in a node version soon!
stringifiedParams = stringifiedParams.replace(
new RegExp(valToReplace, 'g'),
char
);
}
}

if (stringifiedParams) {
req.url += `${splitter}${stringifiedParams}`;
Expand Down
Loading