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
24 lines (21 loc) · 795 Bytes
/
headers.ts
File metadata and controls
24 lines (21 loc) · 795 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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));