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,
|
|
|
|
ListWrapper
|
|
|
|
} 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>>;
|
|
|
|
constructor(headers?: Headers | Object) {
|
|
|
|
if (isBlank(headers)) {
|
|
|
|
this._headersMap = MapWrapper.create();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isPresent((<Headers>headers)._headersMap)) {
|
|
|
|
this._headersMap = (<Headers>headers)._headersMap;
|
|
|
|
} else if (isJsObject(headers)) {
|
|
|
|
this._headersMap = MapWrapper.createFromStringMap(headers);
|
|
|
|
MapWrapper.forEach(this._headersMap, (v, k) => {
|
|
|
|
if (!isListLikeIterable(v)) {
|
|
|
|
var list = ListWrapper.create();
|
|
|
|
ListWrapper.push(list, v);
|
|
|
|
MapWrapper.set(this._headersMap, k, list);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
append(name: string, value: string): void {
|
|
|
|
var list = MapWrapper.get(this._headersMap, name) || ListWrapper.create();
|
|
|
|
ListWrapper.push(list, value);
|
|
|
|
MapWrapper.set(this._headersMap, name, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete (name: string): void { MapWrapper.delete(this._headersMap, name); }
|
|
|
|
|
|
|
|
forEach(fn: Function) { return MapWrapper.forEach(this._headersMap, fn); }
|
|
|
|
|
|
|
|
get(header: string): string {
|
|
|
|
return ListWrapper.first(MapWrapper.get(this._headersMap, header));
|
|
|
|
}
|
|
|
|
|
|
|
|
has(header: string) { return MapWrapper.contains(this._headersMap, header); }
|
|
|
|
|
|
|
|
keys() { return MapWrapper.keys(this._headersMap); }
|
|
|
|
|
|
|
|
// TODO: this implementation seems wrong. create list then check if it's iterable?
|
|
|
|
set(header: string, value: string | List<string>): void {
|
|
|
|
var list = ListWrapper.create();
|
|
|
|
if (!isListLikeIterable(value)) {
|
|
|
|
ListWrapper.push(list, value);
|
|
|
|
} else {
|
|
|
|
ListWrapper.push(list, ListWrapper.toString((<List<string>>value)));
|
|
|
|
}
|
|
|
|
|
|
|
|
MapWrapper.set(this._headersMap, header, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
values() { return MapWrapper.values(this._headersMap); }
|
|
|
|
|
|
|
|
getAll(header: string): Array<string> {
|
|
|
|
return MapWrapper.get(this._headersMap, header) || ListWrapper.create();
|
|
|
|
}
|
|
|
|
|
|
|
|
entries() { throw new BaseException('"entries" method is not implemented on Headers class'); }
|
|
|
|
}
|