UNPKG

angular2

Version:

Angular 2 - a web framework for modern web apps

120 lines 4.57 kB
'use strict';var lang_1 = require('angular2/src/facade/lang'); var exceptions_1 = require('angular2/src/facade/exceptions'); var collection_1 = require('angular2/src/facade/collection'); /** * 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 ([live demo](http://plnkr.co/edit/MTdwT6?p=preview)) * * ``` * import {Headers} from 'angular2/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' * ``` */ var Headers = (function () { function Headers(headers) { var _this = this; if (headers instanceof Headers) { this._headersMap = headers._headersMap; return; } this._headersMap = new collection_1.Map(); if (lang_1.isBlank(headers)) { return; } // headers instanceof StringMap collection_1.StringMapWrapper.forEach(headers, function (v, k) { _this._headersMap.set(k, collection_1.isListLikeIterable(v) ? v : [v]); }); } /** * Returns a new Headers instance from the given DOMString of Response Headers */ Headers.fromResponseHeaderString = function (headersString) { return headersString.trim() .split('\n') .map(function (val) { return val.split(':'); }) .map(function (_a) { var key = _a[0], parts = _a.slice(1); return ([key.trim(), parts.join(':').trim()]); }) .reduce(function (headers, _a) { var key = _a[0], value = _a[1]; return !headers.set(key, value) && headers; }, new Headers()); }; /** * Appends a header to existing list of header values for a given header name. */ Headers.prototype.append = function (name, value) { var mapName = this._headersMap.get(name); var list = collection_1.isListLikeIterable(mapName) ? mapName : []; list.push(value); this._headersMap.set(name, list); }; /** * Deletes all header values for the given name. */ Headers.prototype.delete = function (name) { this._headersMap.delete(name); }; Headers.prototype.forEach = function (fn) { this._headersMap.forEach(fn); }; /** * Returns first header that matches given name. */ Headers.prototype.get = function (header) { return collection_1.ListWrapper.first(this._headersMap.get(header)); }; /** * Check for existence of header by given name. */ Headers.prototype.has = function (header) { return this._headersMap.has(header); }; /** * Provides names of set headers */ Headers.prototype.keys = function () { return collection_1.MapWrapper.keys(this._headersMap); }; /** * Sets or overrides header value for given name. */ Headers.prototype.set = function (header, value) { var list = []; if (collection_1.isListLikeIterable(value)) { var pushValue = value.join(','); list.push(pushValue); } else { list.push(value); } this._headersMap.set(header, list); }; /** * Returns values of all headers. */ Headers.prototype.values = function () { return collection_1.MapWrapper.values(this._headersMap); }; /** * Returns list of header values for a given name. */ Headers.prototype.getAll = function (header) { var headers = this._headersMap.get(header); return collection_1.isListLikeIterable(headers) ? headers : []; }; /** * This method is not implemented. */ Headers.prototype.entries = function () { throw new exceptions_1.BaseException('"entries" method is not implemented on Headers class'); }; return Headers; })(); exports.Headers = Headers; //# sourceMappingURL=headers.js.map