/
httpHeaders.ts
241 lines (221 loc) · 6.76 KB
/
httpHeaders.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/**
* A collection of HttpHeaders that can be sent with a HTTP request.
*/
function getHeaderKey(headerName: string): string {
return headerName.toLowerCase();
}
/**
* An individual header within a HttpHeaders collection.
*/
export interface HttpHeader {
/**
* The name of the header.
*/
name: string;
/**
* The value of the header.
*/
value: string;
}
/**
* A HttpHeaders collection represented as a simple JSON object.
*/
export type RawHttpHeaders = { [headerName: string]: string };
/**
* A collection of HTTP header key/value pairs.
*/
export interface HttpHeadersLike {
/**
* Set a header in this collection with the provided name and value. The name is
* case-insensitive.
* @param headerName - The name of the header to set. This value is case-insensitive.
* @param headerValue - The value of the header to set.
*/
set(headerName: string, headerValue: string | number): void;
/**
* Get the header value for the provided header name, or undefined if no header exists in this
* collection with the provided name.
* @param headerName - The name of the header.
*/
get(headerName: string): string | undefined;
/**
* Get whether or not this header collection contains a header entry for the provided header name.
*/
contains(headerName: string): boolean;
/**
* Remove the header with the provided headerName. Return whether or not the header existed and
* was removed.
* @param headerName - The name of the header to remove.
*/
remove(headerName: string): boolean;
/**
* Get the headers that are contained this collection as an object.
*/
rawHeaders(): RawHttpHeaders;
/**
* Get the headers that are contained in this collection as an array.
*/
headersArray(): HttpHeader[];
/**
* Get the header names that are contained in this collection.
*/
headerNames(): string[];
/**
* Get the header values that are contained in this collection.
*/
headerValues(): string[];
/**
* Create a deep clone/copy of this HttpHeaders collection.
*/
clone(): HttpHeadersLike;
/**
* Get the JSON object representation of this HTTP header collection.
* The result is the same as `rawHeaders()`.
*/
toJson(): RawHttpHeaders;
}
export function isHttpHeadersLike(object?: unknown): object is HttpHeadersLike {
if (object && typeof object === "object") {
const castObject = object as {
rawHeaders: unknown;
clone: unknown;
get: unknown;
set: unknown;
contains: unknown;
remove: unknown;
headersArray: unknown;
headerValues: unknown;
headerNames: unknown;
toJson: unknown;
};
if (
typeof castObject.rawHeaders === "function" &&
typeof castObject.clone === "function" &&
typeof castObject.get === "function" &&
typeof castObject.set === "function" &&
typeof castObject.contains === "function" &&
typeof castObject.remove === "function" &&
typeof castObject.headersArray === "function" &&
typeof castObject.headerValues === "function" &&
typeof castObject.headerNames === "function" &&
typeof castObject.toJson === "function"
) {
return true;
}
}
return false;
}
/**
* A collection of HTTP header key/value pairs.
*/
export class HttpHeaders implements HttpHeadersLike {
private readonly _headersMap: { [headerKey: string]: HttpHeader };
constructor(rawHeaders?: RawHttpHeaders) {
this._headersMap = {};
if (rawHeaders) {
for (const headerName in rawHeaders) {
this.set(headerName, rawHeaders[headerName]);
}
}
}
/**
* Set a header in this collection with the provided name and value. The name is
* case-insensitive.
* @param headerName - The name of the header to set. This value is case-insensitive.
* @param headerValue - The value of the header to set.
*/
public set(headerName: string, headerValue: string | number): void {
this._headersMap[getHeaderKey(headerName)] = {
name: headerName,
value: headerValue.toString()
};
}
/**
* Get the header value for the provided header name, or undefined if no header exists in this
* collection with the provided name.
* @param headerName - The name of the header.
*/
public get(headerName: string): string | undefined {
const header: HttpHeader = this._headersMap[getHeaderKey(headerName)];
return !header ? undefined : header.value;
}
/**
* Get whether or not this header collection contains a header entry for the provided header name.
*/
public contains(headerName: string): boolean {
return !!this._headersMap[getHeaderKey(headerName)];
}
/**
* Remove the header with the provided headerName. Return whether or not the header existed and
* was removed.
* @param headerName - The name of the header to remove.
*/
public remove(headerName: string): boolean {
const result: boolean = this.contains(headerName);
delete this._headersMap[getHeaderKey(headerName)];
return result;
}
/**
* Get the headers that are contained this collection as an object.
*/
public rawHeaders(): RawHttpHeaders {
const result: RawHttpHeaders = {};
for (const headerKey in this._headersMap) {
const header: HttpHeader = this._headersMap[headerKey];
result[header.name.toLowerCase()] = header.value;
}
return result;
}
/**
* Get the headers that are contained in this collection as an array.
*/
public headersArray(): HttpHeader[] {
const headers: HttpHeader[] = [];
for (const headerKey in this._headersMap) {
headers.push(this._headersMap[headerKey]);
}
return headers;
}
/**
* Get the header names that are contained in this collection.
*/
public headerNames(): string[] {
const headerNames: string[] = [];
const headers: HttpHeader[] = this.headersArray();
for (let i = 0; i < headers.length; ++i) {
headerNames.push(headers[i].name);
}
return headerNames;
}
/**
* Get the header values that are contained in this collection.
*/
public headerValues(): string[] {
const headerValues: string[] = [];
const headers: HttpHeader[] = this.headersArray();
for (let i = 0; i < headers.length; ++i) {
headerValues.push(headers[i].value);
}
return headerValues;
}
/**
* Get the JSON object representation of this HTTP header collection.
*/
public toJson(): RawHttpHeaders {
return this.rawHeaders();
}
/**
* Get the string representation of this HTTP header collection.
*/
public toString(): string {
return JSON.stringify(this.toJson());
}
/**
* Create a deep clone/copy of this HttpHeaders collection.
*/
public clone(): HttpHeaders {
return new HttpHeaders(this.rawHeaders());
}
}