-
Notifications
You must be signed in to change notification settings - Fork 69
/
getCanonicalQuery.ts
37 lines (33 loc) · 1020 Bytes
/
getCanonicalQuery.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { HttpRequest } from "@smithy/types";
import { escapeUri } from "@smithy/util-uri-escape";
import { SIGNATURE_HEADER } from "./constants";
/**
* @private
*/
export const getCanonicalQuery = ({ query = {} }: HttpRequest): string => {
const keys: Array<string> = [];
const serialized: Record<string, string> = {};
for (const key of Object.keys(query).sort()) {
if (key.toLowerCase() === SIGNATURE_HEADER) {
continue;
}
keys.push(key);
const value = query[key];
if (typeof value === "string") {
serialized[key] = `${escapeUri(key)}=${escapeUri(value)}`;
} else if (Array.isArray(value)) {
serialized[key] = value
.slice(0)
.reduce(
(encoded: Array<string>, value: string) => encoded.concat([`${escapeUri(key)}=${escapeUri(value)}`]),
[]
)
.sort()
.join("&");
}
}
return keys
.map((key) => serialized[key])
.filter((serialized) => serialized) // omit any falsy values
.join("&");
};