-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutility.ts
38 lines (32 loc) · 952 Bytes
/
utility.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
38
export interface ErrorBody {
message: string;
}
export const inputParameterName = "input";
export const jsonHeaders = {
// eslint-disable-next-line @typescript-eslint/naming-convention
"content-type": "application/json",
};
export const getJsonBody = async (
response: Request | Response,
): Promise<unknown> => {
try {
return (await response.json()) as unknown;
} catch (_error) {
// Even when clients pass `new Request("url", { body: undefined })`,
// bodies are defined as `ReadableStream` after transferred to the other side...
// TODO Does inspection of content length headers work in general?
return undefined;
}
};
export const mergeHeaders = (
one?: HeadersInit,
other?: HeadersInit,
): Headers => {
const headers = new Headers();
for (const initial of [one, other]) {
for (const [key, value] of new Headers(initial).entries()) {
headers.set(key, value);
}
}
return headers;
};