-
Notifications
You must be signed in to change notification settings - Fork 3
/
httpResponseHeaders.ts
36 lines (32 loc) · 1.02 KB
/
httpResponseHeaders.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
import { getHeader, parseHeaders } from './helpers';
import { HttpHeaders } from './httpClient.types';
import { IncomingHttpHeaders } from 'http';
export class HttpResponseHeaders {
public readonly map: Record<string, string>;
constructor(headers: IncomingHttpHeaders | HttpHeaders | Headers | string = {}) {
if (typeof headers === 'string') {
this.map = headers
.split('\n')
.reduce((map, header) => {
const [key, value] = header
.split(':')
.map(element => decodeURIComponent(element.trim()));
map[key.toLowerCase()] = value;
return map;
}, {});
} else if (headers.forEach) {
this.map = parseHeaders(headers as Headers);
} else {
this.map = Object.keys(headers).reduce((result, key) => {
result[key.toLowerCase()] = headers[key];
return result;
}, {});
}
}
set(key: string, value: any) {
this.map[key.toLowerCase()] = value;
}
get(key: string) {
return getHeader(this.map, key);
}
}