httpHeaders.js 4.1 KB

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