-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseHeaders.ts
More file actions
33 lines (31 loc) · 973 Bytes
/
parseHeaders.ts
File metadata and controls
33 lines (31 loc) · 973 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
25
26
27
28
29
30
31
32
33
/**
* Returns a Map of headers with keys in lowercase.
* This is useful for consistent header handling, especially in environments
* where header keys may be case-insensitive.
*/
export const parseHeaders = (
headers: Record<string, string | undefined> | null,
): ParsedHeaders => {
if (headers === null) return new Map()
return CaseInsensitiveMap(
Object.entries(headers).reduce((h, [key, value]) => {
h.set(key.toLowerCase(), value ?? null)
return h
}, new Map()),
)
}
export type ParsedHeaders = Pick<
Map<string, string | null>,
'get' | 'has' | 'size' | 'keys' | 'values' | 'entries' | 'forEach'
>
export const CaseInsensitiveMap = (
map: Map<string, string | null>,
): ParsedHeaders => ({
get: (key) => map.get(key.toLowerCase()),
has: (key) => map.has(key.toLowerCase()),
size: map.size,
keys: () => map.keys(),
values: () => map.values(),
entries: () => map.entries(),
forEach: (callback, thisArg) => map.forEach(callback, thisArg),
})