headers.js
6.22 KB
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
/**
* Polyfill for [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers/Headers), as
* specified in the [Fetch Spec](https://fetch.spec.whatwg.org/#headers-class).
*
* The only known difference between this `Headers` implementation and the spec is the
* lack of an `entries` method.
*
* ### Example
*
* ```
* import {Headers} from '\@angular/http';
*
* var firstHeaders = new Headers();
* firstHeaders.append('Content-Type', 'image/jpeg');
* console.log(firstHeaders.get('Content-Type')) //'image/jpeg'
*
* // Create headers from Plain Old JavaScript Object
* var secondHeaders = new Headers({
* 'X-My-Custom-Header': 'Angular'
* });
* console.log(secondHeaders.get('X-My-Custom-Header')); //'Angular'
*
* var thirdHeaders = new Headers(secondHeaders);
* console.log(thirdHeaders.get('X-My-Custom-Header')); //'Angular'
* ```
*
* \@experimental
*/
export var Headers = (function () {
/**
* @param {?=} headers
*/
function Headers(headers) {
var _this = this;
/** @internal header names are lower case */
this._headers = new Map();
/** @internal map lower case names to actual names */
this._normalizedNames = new Map();
if (!headers) {
return;
}
if (headers instanceof Headers) {
headers.forEach(function (values, name) {
values.forEach(function (value) { return _this.append(name, value); });
});
return;
}
Object.keys(headers).forEach(function (name) {
var values = Array.isArray(headers[name]) ? headers[name] : [headers[name]];
_this.delete(name);
values.forEach(function (value) { return _this.append(name, value); });
});
}
/**
* Returns a new Headers instance from the given DOMString of Response Headers
* @param {?} headersString
* @return {?}
*/
Headers.fromResponseHeaderString = function (headersString) {
var /** @type {?} */ headers = new Headers();
headersString.split('\n').forEach(function (line) {
var /** @type {?} */ index = line.indexOf(':');
if (index > 0) {
var /** @type {?} */ name_1 = line.slice(0, index);
var /** @type {?} */ value = line.slice(index + 1).trim();
headers.set(name_1, value);
}
});
return headers;
};
/**
* Appends a header to existing list of header values for a given header name.
* @param {?} name
* @param {?} value
* @return {?}
*/
Headers.prototype.append = function (name, value) {
var /** @type {?} */ values = this.getAll(name);
if (values === null) {
this.set(name, value);
}
else {
values.push(value);
}
};
/**
* Deletes all header values for the given name.
* @param {?} name
* @return {?}
*/
Headers.prototype.delete = function (name) {
var /** @type {?} */ lcName = name.toLowerCase();
this._normalizedNames.delete(lcName);
this._headers.delete(lcName);
};
/**
* @param {?} fn
* @return {?}
*/
Headers.prototype.forEach = function (fn) {
var _this = this;
this._headers.forEach(function (values, lcName) { return fn(values, _this._normalizedNames.get(lcName), _this._headers); });
};
/**
* Returns first header that matches given name.
* @param {?} name
* @return {?}
*/
Headers.prototype.get = function (name) {
var /** @type {?} */ values = this.getAll(name);
if (values === null) {
return null;
}
return values.length > 0 ? values[0] : null;
};
/**
* Checks for existence of header by given name.
* @param {?} name
* @return {?}
*/
Headers.prototype.has = function (name) { return this._headers.has(name.toLowerCase()); };
/**
* Returns the names of the headers
* @return {?}
*/
Headers.prototype.keys = function () { return Array.from(this._normalizedNames.values()); };
/**
* Sets or overrides header value for given name.
* @param {?} name
* @param {?} value
* @return {?}
*/
Headers.prototype.set = function (name, value) {
if (Array.isArray(value)) {
if (value.length) {
this._headers.set(name.toLowerCase(), [value.join(',')]);
}
}
else {
this._headers.set(name.toLowerCase(), [value]);
}
this.mayBeSetNormalizedName(name);
};
/**
* Returns values of all headers.
* @return {?}
*/
Headers.prototype.values = function () { return Array.from(this._headers.values()); };
/**
* @return {?}
*/
Headers.prototype.toJSON = function () {
var _this = this;
var /** @type {?} */ serialized = {};
this._headers.forEach(function (values, name) {
var /** @type {?} */ split = [];
values.forEach(function (v) { return split.push.apply(split, v.split(',')); });
serialized[_this._normalizedNames.get(name)] = split;
});
return serialized;
};
/**
* Returns list of header values for a given name.
* @param {?} name
* @return {?}
*/
Headers.prototype.getAll = function (name) {
return this.has(name) ? this._headers.get(name.toLowerCase()) : null;
};
/**
* This method is not implemented.
* @return {?}
*/
Headers.prototype.entries = function () { throw new Error('"entries" method is not implemented on Headers class'); };
/**
* @param {?} name
* @return {?}
*/
Headers.prototype.mayBeSetNormalizedName = function (name) {
var /** @type {?} */ lcName = name.toLowerCase();
if (!this._normalizedNames.has(lcName)) {
this._normalizedNames.set(lcName, name);
}
};
return Headers;
}());
function Headers_tsickle_Closure_declarations() {
/**
* \@internal header names are lower case
* @type {?}
*/
Headers.prototype._headers;
/**
* \@internal map lower case names to actual names
* @type {?}
*/
Headers.prototype._normalizedNames;
}
//# sourceMappingURL=headers.js.map