DEV: Modernise rest adapter query parameter construction (#17925)

Using `URLSearchParams` means that we don't have to manually encode/join strings
This commit is contained in:
David Taylor 2022-08-15 14:44:07 +01:00 committed by GitHub
parent b6c6c803c2
commit a9e9adf253
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 10 deletions

View File

@ -47,20 +47,22 @@ export default EmberObject.extend({
appendQueryParams(path, findArgs, extension) {
if (findArgs) {
if (typeof findArgs === "object") {
const queryString = Object.keys(findArgs)
.reject((k) => !findArgs[k])
.map((k) => k + "=" + encodeURIComponent(findArgs[k]));
const urlSearchParams = new URLSearchParams();
if (queryString.length) {
return `${path}${extension ? extension : ""}?${queryString.join(
"&"
)}`;
for (const [key, value] of Object.entries(findArgs)) {
if (value) {
urlSearchParams.set(key, value);
}
}
const queryString = urlSearchParams.toString();
if (queryString) {
return `${path}${extension || ""}?${queryString}`;
}
} else {
// It's serializable as a string if not an object
return `${path}/${encodeURIComponent(findArgs)}${
extension ? extension : ""
}`;
return `${path}/${encodeURIComponent(findArgs)}${extension || ""}`;
}
}
return path;