-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Make non-GET body canonicalization consistent with pywb
- Loading branch information
Showing
2 changed files
with
141 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -156,13 +156,48 @@ export function jsonToQueryParams(json: string | any, ignoreInvalid = true) { | |
return key + "." + ++dupes[key] + "_"; | ||
}; | ||
|
||
try { | ||
JSON.stringify(json, (k, v) => { | ||
if (!["object", "function"].includes(typeof v)) { | ||
q.set(getKey(k), v); | ||
const parser = (jsonObj: object, key: string = "") => { | ||
// if object with keys, recurse through key/values | ||
let queryValue = ""; | ||
|
||
if (typeof(jsonObj) === "object" && !(jsonObj instanceof Array)) { | ||
try { | ||
// recurse through key/value pairs | ||
const keys = Object.keys(jsonObj); | ||
for (let i=0; i < keys.length; i++) { | ||
// @ts-ignore | ||
Check failure on line 168 in src/lib/utils.ts GitHub Actions / build (18.x)
Check failure on line 168 in src/lib/utils.ts GitHub Actions / build (18.x)
|
||
const key = keys[i]; | ||
// @ts-ignore | ||
Check failure on line 170 in src/lib/utils.ts GitHub Actions / build (18.x)
Check failure on line 170 in src/lib/utils.ts GitHub Actions / build (18.x)
|
||
const value = jsonObj[key]; | ||
parser(value, key); | ||
} | ||
} catch (e) { | ||
// special case for null | ||
if (jsonObj === null) { | ||
queryValue = "null"; | ||
} | ||
} | ||
} | ||
|
||
// if array, recurse through values, re-using key | ||
else if (jsonObj instanceof Array) { | ||
for (let i=0; i < jsonObj.length; i++) { | ||
parser(jsonObj[i], key); | ||
} | ||
return v; | ||
}); | ||
} | ||
|
||
// otherwise, try to cast to string and set query param | ||
if (["string", "number", "boolean"].includes(typeof(jsonObj))) { | ||
queryValue = jsonObj.toString(); | ||
} | ||
|
||
if (!!queryValue) { | ||
q.set(getKey(key), queryValue); | ||
} | ||
} | ||
|
||
try { | ||
parser(json); | ||
} catch (e) { | ||
if (!ignoreInvalid) { | ||
throw e; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters