UNPKG

3.96 kBJavaScriptView Raw
1export class RestHeaders {
2 constructor(headers) {
3 /** @internal header names are lower case */
4 this._headers = new Map();
5 /** @internal map lower case names to actual names */
6 this._normalizedNames = new Map();
7 if (headers instanceof RestHeaders) {
8 headers.forEach((values, name) => {
9 values.forEach(value => this.set(name, value));
10 });
11 }
12 else {
13 Object.keys(headers).forEach((name) => {
14 const values = (Array.isArray(headers[name]) ? headers[name] : [headers[name]]);
15 this.delete(name);
16 values.forEach(value => this.set(name, value));
17 });
18 }
19 }
20 static from(headers) {
21 if (!headers) {
22 return void 0;
23 }
24 return new RestHeaders(headers);
25 }
26 /**
27 * Returns a new RestHeaders instance from the given DOMString of Response RestHeaders
28 */
29 static fromResponseHeaderString(headersString) {
30 const headers = new RestHeaders();
31 headersString.split('\n').forEach(line => {
32 const index = line.indexOf(':');
33 if (index > 0) {
34 const name = line.slice(0, index);
35 const value = line.slice(index + 1).trim();
36 headers.set(name, value);
37 }
38 });
39 return headers;
40 }
41 /**
42 * Appends a header to existing list of header values for a given header name.
43 */
44 append(name, value) {
45 const values = this.getAll(name);
46 if (values === null) {
47 this.set(name, value);
48 }
49 else {
50 values.push(value);
51 }
52 }
53 /**
54 * Deletes all header values for the given name.
55 */
56 delete(name) {
57 const lcName = name.toLowerCase();
58 this._normalizedNames.delete(lcName);
59 this._headers.delete(lcName);
60 }
61 forEach(fn) {
62 this._headers.forEach((values, lcName) => fn(values, this._normalizedNames.get(lcName), this._headers));
63 }
64 /**
65 * Returns first header that matches given name.
66 */
67 get(name) {
68 const values = this.getAll(name);
69 if (values === null) {
70 return null;
71 }
72 return values.length > 0 ? values[0] : null;
73 }
74 /**
75 * Checks for existence of header by given name.
76 */
77 has(name) { return this._headers.has(name.toLowerCase()); }
78 /**
79 * Returns the names of the headers
80 */
81 keys() { return Array.from(this._normalizedNames.values()); }
82 /**
83 * Sets or overrides header value for given name.
84 */
85 set(name, value) {
86 if (Array.isArray(value)) {
87 if (value.length) {
88 this._headers.set(name.toLowerCase(), [value.join(',')]);
89 }
90 }
91 else {
92 this._headers.set(name.toLowerCase(), [value]);
93 }
94 this.mayBeSetNormalizedName(name);
95 }
96 /**
97 * Returns values of all headers.
98 */
99 values() { return Array.from(this._headers.values()); }
100 /**
101 * Returns string of all headers.
102 */
103 // TODO(vicb): returns {[name: string]: string[]}
104 toJSON() {
105 const serialized = {};
106 if (!this._headers) {
107 debugger;
108 }
109 this._headers.forEach((values, name) => {
110 const split = [];
111 values.forEach(v => split.push(...v.split(',')));
112 serialized[this._normalizedNames.get(name)] = split;
113 });
114 return serialized;
115 }
116 /**
117 * Returns list of header values for a given name.
118 */
119 getAll(name) {
120 return this.has(name) ? this._headers.get(name.toLowerCase()) : null;
121 }
122 mayBeSetNormalizedName(name) {
123 const lcName = name.toLowerCase();
124 if (!this._normalizedNames.has(lcName)) {
125 this._normalizedNames.set(lcName, name);
126 }
127 }
128}
129//# sourceMappingURL=rest-headers.js.map
\No newline at end of file