This commit is contained in:
Benjamin Toby
2024-12-12 14:38:37 +01:00
parent 1c2798b93f
commit 2a4e111160
2 changed files with 13 additions and 2 deletions
+12 -1
View File
@@ -1,5 +1,7 @@
// @ts-check
const EJSON = require("./ejson");
/** @type {import("../types").SerializeQueryFnType} */
function serializeQuery({ query }) {
let str = "?";
@@ -7,10 +9,19 @@ function serializeQuery({ query }) {
/** @type {string[]} */
const queryArr = [];
keys.forEach((key) => {
if (!key || !query[key]) return;
queryArr.push(`${key}=${query[key]}`);
const value = query[key];
if (typeof value === "object") {
const jsonStr = EJSON.stringify(value);
queryArr.push(`${key}=${encodeURIComponent(String(jsonStr))}`);
} else if (typeof value === "string" || typeof value === "number") {
queryArr.push(`${key}=${encodeURIComponent(value)}`);
}
});
str += queryArr.join("&");
return str;
}