UNPKG

6.62 kBPlain TextView Raw
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License. See License.txt in the project root for license information.
3
4/**
5 * A collection of HttpHeaders that can be sent with a HTTP request.
6 */
7function getHeaderKey(headerName: string) {
8 return headerName.toLowerCase();
9}
10
11/**
12 * An individual header within a HttpHeaders collection.
13 */
14export interface HttpHeader {
15 /**
16 * The name of the header.
17 */
18 name: string;
19
20 /**
21 * The value of the header.
22 */
23 value: string;
24}
25
26/**
27 * A HttpHeaders collection represented as a simple JSON object.
28 */
29export type RawHttpHeaders = { [headerName: string]: string };
30
31/**
32 * A collection of HTTP header key/value pairs.
33 */
34export interface HttpHeadersLike {
35 /**
36 * Set a header in this collection with the provided name and value. The name is
37 * case-insensitive.
38 * @param headerName The name of the header to set. This value is case-insensitive.
39 * @param headerValue The value of the header to set.
40 */
41 set(headerName: string, headerValue: string | number): void;
42 /**
43 * Get the header value for the provided header name, or undefined if no header exists in this
44 * collection with the provided name.
45 * @param headerName The name of the header.
46 */
47 get(headerName: string): string | undefined;
48 /**
49 * Get whether or not this header collection contains a header entry for the provided header name.
50 */
51 contains(headerName: string): boolean;
52 /**
53 * Remove the header with the provided headerName. Return whether or not the header existed and
54 * was removed.
55 * @param headerName The name of the header to remove.
56 */
57 remove(headerName: string): boolean;
58 /**
59 * Get the headers that are contained this collection as an object.
60 */
61 rawHeaders(): RawHttpHeaders;
62 /**
63 * Get the headers that are contained in this collection as an array.
64 */
65 headersArray(): HttpHeader[];
66 /**
67 * Get the header names that are contained in this collection.
68 */
69 headerNames(): string[];
70 /**
71 * Get the header values that are contained in this collection.
72 */
73 headerValues(): string[];
74 /**
75 * Create a deep clone/copy of this HttpHeaders collection.
76 */
77 clone(): HttpHeadersLike;
78 /**
79 * Get the JSON object representation of this HTTP header collection.
80 * The result is the same as `rawHeaders()`.
81 */
82 toJson(): RawHttpHeaders;
83}
84
85export 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 * A collection of HTTP header key/value pairs.
109 */
110export 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 * Set a header in this collection with the provided name and value. The name is
124 * case-insensitive.
125 * @param headerName The name of the header to set. This value is case-insensitive.
126 * @param headerValue The value of the header to set.
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 * Get the header value for the provided header name, or undefined if no header exists in this
137 * collection with the provided name.
138 * @param headerName The name of the header.
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 * Get whether or not this header collection contains a header entry for the provided header name.
147 */
148 public contains(headerName: string): boolean {
149 return !!this._headersMap[getHeaderKey(headerName)];
150 }
151
152 /**
153 * Remove the header with the provided headerName. Return whether or not the header existed and
154 * was removed.
155 * @param headerName The name of the header to remove.
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 * Get the headers that are contained this collection as an object.
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 * Get the headers that are contained in this collection as an array.
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 * Get the header names that are contained in this collection.
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 * Get the header names that are contained in this collection.
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 * Get the JSON object representation of this HTTP header collection.
212 */
213 public toJson(): RawHttpHeaders {
214 return this.rawHeaders();
215 }
216
217 /**
218 * Get the string representation of this HTTP header collection.
219 */
220 public toString(): string {
221 return JSON.stringify(this.toJson());
222 }
223
224 /**
225 * Create a deep clone/copy of this HttpHeaders collection.
226 */
227 public clone(): HttpHeaders {
228 return new HttpHeaders(this.rawHeaders());
229 }
230}