-
Notifications
You must be signed in to change notification settings - Fork 85
/
getCanonicalQuery.ts
36 lines (32 loc) · 1.02 KB
/
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
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)) {
if (key.toLowerCase() === SIGNATURE_HEADER) {
continue;
}
const encodedKey = escapeUri(key);
keys.push(encodedKey);
const value = query[key];
if (typeof value === "string") {
serialized[encodedKey] = `${encodedKey}=${escapeUri(value)}`;
} else if (Array.isArray(value)) {
serialized[encodedKey] = value
.slice(0)
.reduce((encoded: Array<string>, value: string) => encoded.concat([`${encodedKey}=${escapeUri(value)}`]), [])
.sort()
.join("&");
}
}
return keys
.sort()
.map((key) => serialized[key])
.filter((serialized) => serialized) // omit any falsy values
.join("&");
};