2015-04-29 02:07:55 -04:00
|
|
|
import {
|
|
|
|
isPresent,
|
|
|
|
isBlank,
|
|
|
|
isJsObject,
|
|
|
|
isType,
|
|
|
|
StringWrapper,
|
|
|
|
BaseException
|
|
|
|
} from 'angular2/src/facade/lang';
|
|
|
|
import {
|
|
|
|
isListLikeIterable,
|
|
|
|
List,
|
|
|
|
Map,
|
|
|
|
MapWrapper,
|
2015-06-19 15:14:12 -04:00
|
|
|
ListWrapper,
|
|
|
|
StringMap
|
2015-04-29 02:07:55 -04:00
|
|
|
} from 'angular2/src/facade/collection';
|
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* 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 from the spec is the lack of an `entries` method.
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
export class Headers {
|
|
|
|
_headersMap: Map<string, List<string>>;
|
2015-06-19 15:14:12 -04:00
|
|
|
constructor(headers?: Headers | StringMap<string, any>) {
|
2015-04-29 02:07:55 -04:00
|
|
|
if (isBlank(headers)) {
|
2015-06-17 19:21:40 -04:00
|
|
|
this._headersMap = new Map();
|
2015-04-29 02:07:55 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-19 15:14:12 -04:00
|
|
|
if (headers instanceof Headers) {
|
2015-04-29 02:07:55 -04:00
|
|
|
this._headersMap = (<Headers>headers)._headersMap;
|
2015-06-19 15:14:12 -04:00
|
|
|
} else if (headers instanceof StringMap) {
|
2015-04-29 02:07:55 -04:00
|
|
|
this._headersMap = MapWrapper.createFromStringMap(headers);
|
|
|
|
MapWrapper.forEach(this._headersMap, (v, k) => {
|
|
|
|
if (!isListLikeIterable(v)) {
|
2015-06-17 14:17:21 -04:00
|
|
|
var list = [];
|
|
|
|
list.push(v);
|
2015-06-17 19:21:40 -04:00
|
|
|
this._headersMap.set(k, list);
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-29 02:07:55 -04:00
|
|
|
delete (name: string): void { MapWrapper.delete(this._headersMap, name); }
|
|
|
|
|
2015-06-26 14:10:52 -04:00
|
|
|
forEach(fn: Function) { MapWrapper.forEach(this._headersMap, fn); }
|
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-06-26 14:10:52 -04:00
|
|
|
keys(): List<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.
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
set(header: string, value: string | List<string>): void {
|
2015-06-17 14:17:21 -04:00
|
|
|
var list = [];
|
2015-06-24 03:27:07 -04:00
|
|
|
|
2015-06-19 15:14:12 -04:00
|
|
|
if (isListLikeIterable(value)) {
|
2015-06-24 03:27:07 -04:00
|
|
|
var pushValue = (<List<string>>value).join(',');
|
2015-06-19 15:14:12 -04:00
|
|
|
list.push(pushValue);
|
2015-04-29 02:07:55 -04:00
|
|
|
} else {
|
2015-06-19 15:14:12 -04:00
|
|
|
list.push(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-06-26 14:10:52 -04:00
|
|
|
values(): List<List<string>> { return MapWrapper.values(this._headersMap); }
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-24 03:27:07 -04:00
|
|
|
/**
|
|
|
|
* Returns list of header values for a given name.
|
|
|
|
*/
|
2015-06-19 15:14:12 -04:00
|
|
|
getAll(header: string): Array<string> {
|
|
|
|
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'); }
|
|
|
|
}
|