UNPKG

4.86 kBJavaScriptView 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 * A collection of HttpHeaders that can be sent with a HTTP request.
5 */
6function getHeaderKey(headerName) {
7 return headerName.toLowerCase();
8}
9export function isHttpHeadersLike(object) {
10 if (!object || typeof object !== "object") {
11 return false;
12 }
13 if (typeof object.rawHeaders === "function" &&
14 typeof object.clone === "function" &&
15 typeof object.get === "function" &&
16 typeof object.set === "function" &&
17 typeof object.contains === "function" &&
18 typeof object.remove === "function" &&
19 typeof object.headersArray === "function" &&
20 typeof object.headerValues === "function" &&
21 typeof object.headerNames === "function" &&
22 typeof object.toJson === "function") {
23 return true;
24 }
25 return false;
26}
27/**
28 * A collection of HTTP header key/value pairs.
29 */
30var HttpHeaders = /** @class */ (function () {
31 function HttpHeaders(rawHeaders) {
32 this._headersMap = {};
33 if (rawHeaders) {
34 for (var headerName in rawHeaders) {
35 this.set(headerName, rawHeaders[headerName]);
36 }
37 }
38 }
39 /**
40 * Set a header in this collection with the provided name and value. The name is
41 * case-insensitive.
42 * @param headerName The name of the header to set. This value is case-insensitive.
43 * @param headerValue The value of the header to set.
44 */
45 HttpHeaders.prototype.set = function (headerName, headerValue) {
46 this._headersMap[getHeaderKey(headerName)] = {
47 name: headerName,
48 value: headerValue.toString(),
49 };
50 };
51 /**
52 * Get the header value for the provided header name, or undefined if no header exists in this
53 * collection with the provided name.
54 * @param headerName The name of the header.
55 */
56 HttpHeaders.prototype.get = function (headerName) {
57 var header = this._headersMap[getHeaderKey(headerName)];
58 return !header ? undefined : header.value;
59 };
60 /**
61 * Get whether or not this header collection contains a header entry for the provided header name.
62 */
63 HttpHeaders.prototype.contains = function (headerName) {
64 return !!this._headersMap[getHeaderKey(headerName)];
65 };
66 /**
67 * Remove the header with the provided headerName. Return whether or not the header existed and
68 * was removed.
69 * @param headerName The name of the header to remove.
70 */
71 HttpHeaders.prototype.remove = function (headerName) {
72 var result = this.contains(headerName);
73 delete this._headersMap[getHeaderKey(headerName)];
74 return result;
75 };
76 /**
77 * Get the headers that are contained this collection as an object.
78 */
79 HttpHeaders.prototype.rawHeaders = function () {
80 var result = {};
81 for (var headerKey in this._headersMap) {
82 var header = this._headersMap[headerKey];
83 result[header.name.toLowerCase()] = header.value;
84 }
85 return result;
86 };
87 /**
88 * Get the headers that are contained in this collection as an array.
89 */
90 HttpHeaders.prototype.headersArray = function () {
91 var headers = [];
92 for (var headerKey in this._headersMap) {
93 headers.push(this._headersMap[headerKey]);
94 }
95 return headers;
96 };
97 /**
98 * Get the header names that are contained in this collection.
99 */
100 HttpHeaders.prototype.headerNames = function () {
101 var headerNames = [];
102 var headers = this.headersArray();
103 for (var i = 0; i < headers.length; ++i) {
104 headerNames.push(headers[i].name);
105 }
106 return headerNames;
107 };
108 /**
109 * Get the header names that are contained in this collection.
110 */
111 HttpHeaders.prototype.headerValues = function () {
112 var headerValues = [];
113 var headers = this.headersArray();
114 for (var i = 0; i < headers.length; ++i) {
115 headerValues.push(headers[i].value);
116 }
117 return headerValues;
118 };
119 /**
120 * Get the JSON object representation of this HTTP header collection.
121 */
122 HttpHeaders.prototype.toJson = function () {
123 return this.rawHeaders();
124 };
125 /**
126 * Get the string representation of this HTTP header collection.
127 */
128 HttpHeaders.prototype.toString = function () {
129 return JSON.stringify(this.toJson());
130 };
131 /**
132 * Create a deep clone/copy of this HttpHeaders collection.
133 */
134 HttpHeaders.prototype.clone = function () {
135 return new HttpHeaders(this.rawHeaders());
136 };
137 return HttpHeaders;
138}());
139export { HttpHeaders };
140//# sourceMappingURL=httpHeaders.js.map
\No newline at end of file