Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 56bfdf6

Browse files
committedMay 29, 2025·
feat(parseHeaders): make it case-insensitive
1 parent 83e9760 commit 56bfdf6

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed
 

‎src/parseHeaders.spec.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void describe('parseHeaders()', () => {
1313
const parsedHeaders = parseHeaders(headers)
1414

1515
assert.deepEqual(
16-
parsedHeaders,
16+
new Map(parsedHeaders.entries()),
1717
new Map([
1818
['content-type', 'application/json'],
1919
['content-length', '1234'],
@@ -24,4 +24,12 @@ void describe('parseHeaders()', () => {
2424

2525
void it('should handle null values in headers', () =>
2626
assert.deepEqual(parseHeaders(null), new Map()))
27+
28+
void it('should make header keys case-insensitive', () => {
29+
const headers = {
30+
'x-custom-header': 'value',
31+
}
32+
const parsedHeaders = parseHeaders(headers)
33+
assert.equal(parsedHeaders.get('X-Custom-HEADER'), 'value')
34+
})
2735
})

‎src/parseHeaders.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,29 @@
55
*/
66
export const parseHeaders = (
77
headers: Record<string, string | undefined> | null,
8-
): Map<string, string | null> => {
8+
): ParsedHeaders => {
99
if (headers === null) return new Map()
10-
return Object.entries(headers).reduce((h, [key, value]) => {
11-
h.set(key.toLowerCase(), value ?? null)
12-
return h
13-
}, new Map())
10+
return CaseInsensitiveMap(
11+
Object.entries(headers).reduce((h, [key, value]) => {
12+
h.set(key.toLowerCase(), value ?? null)
13+
return h
14+
}, new Map()),
15+
)
1416
}
17+
18+
export type ParsedHeaders = Pick<
19+
Map<string, string | null>,
20+
'get' | 'has' | 'size' | 'keys' | 'values' | 'entries' | 'forEach'
21+
>
22+
23+
export const CaseInsensitiveMap = (
24+
map: Map<string, string | null>,
25+
): ParsedHeaders => ({
26+
get: (key) => map.get(key.toLowerCase()),
27+
has: (key) => map.has(key.toLowerCase()),
28+
size: map.size,
29+
keys: () => map.keys(),
30+
values: () => map.values(),
31+
entries: () => map.entries(),
32+
forEach: (callback, thisArg) => map.forEach(callback, thisArg),
33+
})

0 commit comments

Comments
 (0)
Please sign in to comment.