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

refactor(http): replace qs usage by URLSearchParams #3634

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 14 additions & 15 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
"node-fetch-commonjs": "^3.3.2",
"openapi-path-templating": "^1.5.1",
"openapi-server-url-templating": "^1.0.0",
"qs": "^6.10.2",
"ramda-adjunct": "^5.0.0",
"neotraverse": "=0.6.18"
},
Expand Down
24 changes: 21 additions & 3 deletions src/http/serializers/request/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import qs from 'qs';

import formatKeyValue from './format.js';
import { isFile, isArrayOfFile, FileWithData } from './file.js';

Expand Down Expand Up @@ -59,8 +57,28 @@ export function encodeFormOrQuery(data) {
}
return result;
}, {});
const buildNestedParams = (params, key, value) => {
if (value == null) {
params.append(key, '');
} else if (Array.isArray(value)) {
value.reduce((acc, v) => buildNestedParams(params, key, v), params);
} else if (typeof value === 'object') {
Object.entries(value).reduce(
(acc, [k, v]) => buildNestedParams(params, `${key}[${k}]`, v),
params
);
} else {
params.append(key, value);
}

return params;
};
const params = Object.entries(encodedQuery).reduce(
(acc, [key, value]) => buildNestedParams(acc, key, value),
new URLSearchParams()
);

return qs.stringify(encodedQuery, { encode: false, indices: false }) || '';
return decodeURIComponent(String(params));
}

// If the request has a `query` object, merge it into the request.url, and delete the object
Expand Down