1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | function getHeaderKey(headerName: string) {
|
8 | return headerName.toLowerCase();
|
9 | }
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | export interface HttpHeader {
|
15 | |
16 |
|
17 |
|
18 | name: string;
|
19 |
|
20 | |
21 |
|
22 |
|
23 | value: string;
|
24 | }
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | export type RawHttpHeaders = { [headerName: string]: string };
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | export interface HttpHeadersLike {
|
35 | |
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | set(headerName: string, headerValue: string | number): void;
|
42 | |
43 |
|
44 |
|
45 |
|
46 |
|
47 | get(headerName: string): string | undefined;
|
48 | |
49 |
|
50 |
|
51 | contains(headerName: string): boolean;
|
52 | |
53 |
|
54 |
|
55 |
|
56 |
|
57 | remove(headerName: string): boolean;
|
58 | |
59 |
|
60 |
|
61 | rawHeaders(): RawHttpHeaders;
|
62 | |
63 |
|
64 |
|
65 | headersArray(): HttpHeader[];
|
66 | |
67 |
|
68 |
|
69 | headerNames(): string[];
|
70 | |
71 |
|
72 |
|
73 | headerValues(): string[];
|
74 | |
75 |
|
76 |
|
77 | clone(): HttpHeadersLike;
|
78 | |
79 |
|
80 |
|
81 |
|
82 | toJson(): RawHttpHeaders;
|
83 | }
|
84 |
|
85 | export function isHttpHeadersLike(object?: any): object is HttpHeadersLike {
|
86 | if (!object || typeof object !== "object") {
|
87 | return false;
|
88 | }
|
89 |
|
90 | if (
|
91 | typeof object.rawHeaders === "function" &&
|
92 | typeof object.clone === "function" &&
|
93 | typeof object.get === "function" &&
|
94 | typeof object.set === "function" &&
|
95 | typeof object.contains === "function" &&
|
96 | typeof object.remove === "function" &&
|
97 | typeof object.headersArray === "function" &&
|
98 | typeof object.headerValues === "function" &&
|
99 | typeof object.headerNames === "function" &&
|
100 | typeof object.toJson === "function"
|
101 | ) {
|
102 | return true;
|
103 | }
|
104 | return false;
|
105 | }
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | export class HttpHeaders {
|
111 | private readonly _headersMap: { [headerKey: string]: HttpHeader };
|
112 |
|
113 | constructor(rawHeaders?: RawHttpHeaders) {
|
114 | this._headersMap = {};
|
115 | if (rawHeaders) {
|
116 | for (const headerName in rawHeaders) {
|
117 | this.set(headerName, rawHeaders[headerName]);
|
118 | }
|
119 | }
|
120 | }
|
121 |
|
122 | |
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | public set(headerName: string, headerValue: string | number): void {
|
129 | this._headersMap[getHeaderKey(headerName)] = {
|
130 | name: headerName,
|
131 | value: headerValue.toString(),
|
132 | };
|
133 | }
|
134 |
|
135 | |
136 |
|
137 |
|
138 |
|
139 |
|
140 | public get(headerName: string): string | undefined {
|
141 | const header: HttpHeader = this._headersMap[getHeaderKey(headerName)];
|
142 | return !header ? undefined : header.value;
|
143 | }
|
144 |
|
145 | |
146 |
|
147 |
|
148 | public contains(headerName: string): boolean {
|
149 | return !!this._headersMap[getHeaderKey(headerName)];
|
150 | }
|
151 |
|
152 | |
153 |
|
154 |
|
155 |
|
156 |
|
157 | public remove(headerName: string): boolean {
|
158 | const result: boolean = this.contains(headerName);
|
159 | delete this._headersMap[getHeaderKey(headerName)];
|
160 | return result;
|
161 | }
|
162 |
|
163 | |
164 |
|
165 |
|
166 | public rawHeaders(): RawHttpHeaders {
|
167 | const result: RawHttpHeaders = {};
|
168 | for (const headerKey in this._headersMap) {
|
169 | const header: HttpHeader = this._headersMap[headerKey];
|
170 | result[header.name.toLowerCase()] = header.value;
|
171 | }
|
172 | return result;
|
173 | }
|
174 |
|
175 | |
176 |
|
177 |
|
178 | public headersArray(): HttpHeader[] {
|
179 | const headers: HttpHeader[] = [];
|
180 | for (const headerKey in this._headersMap) {
|
181 | headers.push(this._headersMap[headerKey]);
|
182 | }
|
183 | return headers;
|
184 | }
|
185 |
|
186 | |
187 |
|
188 |
|
189 | public headerNames(): string[] {
|
190 | const headerNames: string[] = [];
|
191 | const headers: HttpHeader[] = this.headersArray();
|
192 | for (let i = 0; i < headers.length; ++i) {
|
193 | headerNames.push(headers[i].name);
|
194 | }
|
195 | return headerNames;
|
196 | }
|
197 |
|
198 | |
199 |
|
200 |
|
201 | public headerValues(): string[] {
|
202 | const headerValues: string[] = [];
|
203 | const headers: HttpHeader[] = this.headersArray();
|
204 | for (let i = 0; i < headers.length; ++i) {
|
205 | headerValues.push(headers[i].value);
|
206 | }
|
207 | return headerValues;
|
208 | }
|
209 |
|
210 | |
211 |
|
212 |
|
213 | public toJson(): RawHttpHeaders {
|
214 | return this.rawHeaders();
|
215 | }
|
216 |
|
217 | |
218 |
|
219 |
|
220 | public toString(): string {
|
221 | return JSON.stringify(this.toJson());
|
222 | }
|
223 |
|
224 | |
225 |
|
226 |
|
227 | public clone(): HttpHeaders {
|
228 | return new HttpHeaders(this.rawHeaders());
|
229 | }
|
230 | }
|