2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
import {BaseException} from '../src/facade/exceptions';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {isBlank} from '../src/facade/lang';
|
|
|
|
|
|
|
|
import {isListLikeIterable, iterateListLike, Map, MapWrapper, StringMapWrapper, ListWrapper,} from '../src/facade/collection';
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Polyfill for [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers/Headers), as
|
2015-09-17 18:36:38 -04:00
|
|
|
* 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 ([live demo](http://plnkr.co/edit/MTdwT6?p=preview))
|
|
|
|
*
|
|
|
|
* ```
|
2016-04-28 20:50:03 -04:00
|
|
|
* import {Headers} from '@angular/http';
|
2015-09-17 18:36:38 -04:00
|
|
|
*
|
|
|
|
* 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'
|
|
|
|
* ```
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
|
|
|
* @experimental
|
2015-06-09 18:18:57 -04:00
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
export class Headers {
|
2015-10-09 20:21:25 -04:00
|
|
|
/** @internal */
|
2015-08-28 14:29:19 -04:00
|
|
|
_headersMap: Map<string, string[]>;
|
2016-06-08 19:38:52 -04:00
|
|
|
constructor(headers?: Headers|{[key: string]: any}) {
|
2015-10-08 19:01:18 -04:00
|
|
|
if (headers instanceof Headers) {
|
|
|
|
this._headersMap = (<Headers>headers)._headersMap;
|
2015-04-29 02:07:55 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-08 19:01:18 -04:00
|
|
|
this._headersMap = new Map<string, string[]>();
|
|
|
|
|
|
|
|
if (isBlank(headers)) {
|
|
|
|
return;
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
2015-10-08 19:01:18 -04:00
|
|
|
|
|
|
|
// headers instanceof StringMap
|
2016-02-01 20:05:50 -05:00
|
|
|
StringMapWrapper.forEach(headers, (v: any, k: string) => {
|
|
|
|
this._headersMap.set(k, isListLikeIterable(v) ? v : [v]);
|
|
|
|
});
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
|
|
|
|
2015-11-19 20:51:00 -05:00
|
|
|
/**
|
|
|
|
* Returns a new Headers instance from the given DOMString of Response Headers
|
|
|
|
*/
|
|
|
|
static fromResponseHeaderString(headersString: string): Headers {
|
|
|
|
return headersString.trim()
|
|
|
|
.split('\n')
|
|
|
|
.map(val => val.split(':'))
|
|
|
|
.map(([key, ...parts]) => ([key.trim(), parts.join(':').trim()]))
|
|
|
|
.reduce((headers, [key, value]) => !headers.set(key, value) && headers, new Headers());
|
|
|
|
}
|
|
|
|
|
2015-06-24 03:27:07 -04:00
|
|
|
/**
|
|
|
|
* Appends a header to existing list of header values for a given header name.
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
append(name: string, value: string): void {
|
2015-06-19 15:14:12 -04:00
|
|
|
var mapName = this._headersMap.get(name);
|
|
|
|
var list = isListLikeIterable(mapName) ? mapName : [];
|
2015-06-17 14:17:21 -04:00
|
|
|
list.push(value);
|
2015-06-17 19:21:40 -04:00
|
|
|
this._headersMap.set(name, list);
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
|
|
|
|
2015-06-24 03:27:07 -04:00
|
|
|
/**
|
|
|
|
* Deletes all header values for the given name.
|
|
|
|
*/
|
2015-10-08 19:01:18 -04:00
|
|
|
delete (name: string): void { this._headersMap.delete(name); }
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-10-07 17:56:18 -04:00
|
|
|
forEach(fn: (values: string[], name: string, headers: Map<string, string[]>) => void): void {
|
2015-10-08 19:01:18 -04:00
|
|
|
this._headersMap.forEach(fn);
|
2015-09-17 18:36:38 -04:00
|
|
|
}
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-24 03:27:07 -04:00
|
|
|
/**
|
|
|
|
* Returns first header that matches given name.
|
|
|
|
*/
|
2015-06-17 19:21:40 -04:00
|
|
|
get(header: string): string { return ListWrapper.first(this._headersMap.get(header)); }
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-24 03:27:07 -04:00
|
|
|
/**
|
|
|
|
* Check for existence of header by given name.
|
|
|
|
*/
|
2015-06-26 14:10:52 -04:00
|
|
|
has(header: string): boolean { return this._headersMap.has(header); }
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-24 03:27:07 -04:00
|
|
|
/**
|
|
|
|
* Provides names of set headers
|
|
|
|
*/
|
2015-08-28 14:29:19 -04:00
|
|
|
keys(): string[] { return MapWrapper.keys(this._headersMap); }
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-24 03:27:07 -04:00
|
|
|
/**
|
|
|
|
* Sets or overrides header value for given name.
|
|
|
|
*/
|
2016-06-08 19:38:52 -04:00
|
|
|
set(header: string, value: string|string[]): void {
|
2016-02-01 20:05:50 -05:00
|
|
|
var list: string[] = [];
|
2015-06-24 03:27:07 -04:00
|
|
|
|
2015-06-19 15:14:12 -04:00
|
|
|
if (isListLikeIterable(value)) {
|
2015-08-28 14:29:19 -04:00
|
|
|
var pushValue = (<string[]>value).join(',');
|
2015-06-19 15:14:12 -04:00
|
|
|
list.push(pushValue);
|
2015-04-29 02:07:55 -04:00
|
|
|
} else {
|
2016-02-01 20:05:50 -05:00
|
|
|
list.push(<string>value);
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
|
|
|
|
2015-06-17 19:21:40 -04:00
|
|
|
this._headersMap.set(header, list);
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
|
|
|
|
2015-06-24 03:27:07 -04:00
|
|
|
/**
|
|
|
|
* Returns values of all headers.
|
|
|
|
*/
|
2015-08-28 14:29:19 -04:00
|
|
|
values(): string[][] { return MapWrapper.values(this._headersMap); }
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-12-09 23:56:32 -05:00
|
|
|
/**
|
|
|
|
* Returns string of all headers.
|
|
|
|
*/
|
2016-01-27 02:24:50 -05:00
|
|
|
toJSON(): {[key: string]: any} {
|
|
|
|
let serializableHeaders = {};
|
|
|
|
this._headersMap.forEach((values: string[], name: string) => {
|
2016-06-08 18:45:15 -04:00
|
|
|
let list: any[] /** TODO #9100 */ = [];
|
2016-01-27 02:24:50 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
iterateListLike(
|
|
|
|
values, (val: any /** TODO #9100 */) => list = ListWrapper.concat(list, val.split(',')));
|
2016-01-27 02:24:50 -05:00
|
|
|
|
2016-06-08 18:45:15 -04:00
|
|
|
(serializableHeaders as any /** TODO #9100 */)[name] = list;
|
2016-01-27 02:24:50 -05:00
|
|
|
});
|
|
|
|
return serializableHeaders;
|
|
|
|
}
|
2015-12-09 23:56:32 -05:00
|
|
|
|
2015-06-24 03:27:07 -04:00
|
|
|
/**
|
|
|
|
* Returns list of header values for a given name.
|
|
|
|
*/
|
2015-08-28 14:29:19 -04:00
|
|
|
getAll(header: string): string[] {
|
2015-06-19 15:14:12 -04:00
|
|
|
var headers = this._headersMap.get(header);
|
|
|
|
return isListLikeIterable(headers) ? headers : [];
|
|
|
|
}
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-24 03:27:07 -04:00
|
|
|
/**
|
|
|
|
* This method is not implemented.
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
entries() { throw new BaseException('"entries" method is not implemented on Headers class'); }
|
|
|
|
}
|