forked from Kong/httpsnippet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheaders.ts
More file actions
40 lines (34 loc) · 1.17 KB
/
headers.ts
File metadata and controls
40 lines (34 loc) · 1.17 KB
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
39
40
type Headers<T> = Record<string, T>;
/**
* Given a headers object retrieve a specific header out of it via a case-insensitive key.
*/
export const getHeaderName = <T>(headers: Headers<T>, name: string) =>
Object.keys(headers).find(header => header.toLowerCase() === name.toLowerCase());
/**
* Given a headers object retrieve the contents of a header out of it via a case-insensitive key.
*/
export const getHeader = <T>(headers: Headers<T>, name: string) => {
const headerName = getHeaderName(headers, name);
if (!headerName) {
return undefined;
}
return headers[headerName];
};
/**
* Determine if a given case-insensitive header exists within a header object.
*/
export const hasHeader = <T>(headers: Headers<T>, name: string) =>
Boolean(getHeaderName(headers, name));
const mimeTypeJson = [
'application/json',
'application/x-json',
'text/json',
'text/x-json',
'+json',
] as const;
type MimeTypeJson = `${string}${typeof mimeTypeJson[number]}${string}`;
/**
* Determines if a given mimetype is JSON, or a variant of such.
*/
export const isMimeTypeJSON = (mimeType: string): mimeType is MimeTypeJson =>
mimeTypeJson.some(type => mimeType.includes(type));