@azure/core-rest-pipeline
Version:
Isomorphic client library for making HTTP requests in node.js and browser.
89 lines • 2.9 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
function normalizeName(name) {
return name.toLowerCase();
}
function* headerIterator(map) {
for (const entry of map.values()) {
yield [entry.name, entry.value];
}
}
class HttpHeadersImpl {
constructor(rawHeaders) {
this._headersMap = new Map();
if (rawHeaders) {
for (const headerName of Object.keys(rawHeaders)) {
this.set(headerName, rawHeaders[headerName]);
}
}
}
/**
* Set a header in this collection with the provided name and value. The name is
* case-insensitive.
* @param name - The name of the header to set. This value is case-insensitive.
* @param value - The value of the header to set.
*/
set(name, value) {
this._headersMap.set(normalizeName(name), { name, value: String(value).trim() });
}
/**
* Get the header value for the provided header name, or undefined if no header exists in this
* collection with the provided name.
* @param name - The name of the header. This value is case-insensitive.
*/
get(name) {
var _a;
return (_a = this._headersMap.get(normalizeName(name))) === null || _a === void 0 ? void 0 : _a.value;
}
/**
* Get whether or not this header collection contains a header entry for the provided header name.
* @param name - The name of the header to set. This value is case-insensitive.
*/
has(name) {
return this._headersMap.has(normalizeName(name));
}
/**
* Remove the header with the provided headerName.
* @param name - The name of the header to remove.
*/
delete(name) {
this._headersMap.delete(normalizeName(name));
}
/**
* Get the JSON object representation of this HTTP header collection.
*/
toJSON(options = {}) {
const result = {};
if (options.preserveCase) {
for (const entry of this._headersMap.values()) {
result[entry.name] = entry.value;
}
}
else {
for (const [normalizedName, entry] of this._headersMap) {
result[normalizedName] = entry.value;
}
}
return result;
}
/**
* Get the string representation of this HTTP header collection.
*/
toString() {
return JSON.stringify(this.toJSON({ preserveCase: true }));
}
/**
* Iterate over tuples of header [name, value] pairs.
*/
[Symbol.iterator]() {
return headerIterator(this._headersMap);
}
}
/**
* Creates an object that satisfies the `HttpHeaders` interface.
* @param rawHeaders - A simple object representing initial headers
*/
export function createHttpHeaders(rawHeaders) {
return new HttpHeadersImpl(rawHeaders);
}
//# sourceMappingURL=httpHeaders.js.map