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