UNPKG

@angular/http

Version:
1 lines 103 kB
{"version":3,"file":"http.js","sources":["../src/backends/browser_xhr.ts","../src/enums.ts","../src/headers.ts","../src/base_response_options.ts","../src/interfaces.ts","../src/http_utils.ts","../src/url_search_params.ts","../src/body.ts","../src/static_response.ts","../src/backends/browser_jsonp.ts","../src/backends/jsonp_backend.ts","../src/backends/xhr_backend.ts","../src/base_request_options.ts","../src/static_request.ts","../src/http.ts","../src/http_module.ts","../src/version.ts","../http.ts"],"sourcesContent":["/**\n * @license\n * Copyright Google Inc. All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\n\nimport {Injectable} from '@angular/core';\n\n/**\n * A backend for http that uses the `XMLHttpRequest` browser API.\n *\n * Take care not to evaluate this in non-browser contexts.\n *\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\n@Injectable()\nexport class BrowserXhr {\n constructor() {}\n build(): any { return <any>(new XMLHttpRequest()); }\n}\n","/**\n * @license\n * Copyright Google Inc. All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\n\n/**\n * Supported http methods.\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\nexport enum RequestMethod {\n Get,\n Post,\n Put,\n Delete,\n Options,\n Head,\n Patch\n}\n\n/**\n * All possible states in which a connection can be, based on\n * [States](http://www.w3.org/TR/XMLHttpRequest/#states) from the `XMLHttpRequest` spec, but with an\n * additional \"CANCELLED\" state.\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\nexport enum ReadyState {\n Unsent,\n Open,\n HeadersReceived,\n Loading,\n Done,\n Cancelled\n}\n\n/**\n * Acceptable response types to be associated with a {@link Response}, based on\n * [ResponseType](https://fetch.spec.whatwg.org/#responsetype) from the Fetch spec.\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\nexport enum ResponseType {\n Basic,\n Cors,\n Default,\n Error,\n Opaque\n}\n\n/**\n * Supported content type to be automatically associated with a {@link Request}.\n * @deprecated see https://angular.io/guide/http\n */\nexport enum ContentType {\n NONE,\n JSON,\n FORM,\n FORM_DATA,\n TEXT,\n BLOB,\n ARRAY_BUFFER\n}\n\n/**\n * Define which buffer to use to store the response\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\nexport enum ResponseContentType {\n Text,\n Json,\n ArrayBuffer,\n Blob\n}\n","/**\n * @license\n * Copyright Google Inc. All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\n\n/**\n * Polyfill for [Headers](https://developer.mozilla.org/en-US/docs/Web/API/Headers/Headers), as\n * specified in the [Fetch Spec](https://fetch.spec.whatwg.org/#headers-class).\n *\n * The only known difference between this `Headers` implementation and the spec is the\n * lack of an `entries` method.\n *\n * @usageNotes\n * ### Example\n *\n * ```\n * import {Headers} from '@angular/http';\n *\n * var firstHeaders = new Headers();\n * firstHeaders.append('Content-Type', 'image/jpeg');\n * console.log(firstHeaders.get('Content-Type')) //'image/jpeg'\n *\n * // Create headers from Plain Old JavaScript Object\n * var secondHeaders = new Headers({\n * 'X-My-Custom-Header': 'Angular'\n * });\n * console.log(secondHeaders.get('X-My-Custom-Header')); //'Angular'\n *\n * var thirdHeaders = new Headers(secondHeaders);\n * console.log(thirdHeaders.get('X-My-Custom-Header')); //'Angular'\n * ```\n *\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\nexport class Headers {\n /** @internal header names are lower case */\n _headers: Map<string, string[]> = new Map();\n /** @internal map lower case names to actual names */\n _normalizedNames: Map<string, string> = new Map();\n\n // TODO(vicb): any -> string|string[]\n constructor(headers?: Headers|{[name: string]: any}|null) {\n if (!headers) {\n return;\n }\n\n if (headers instanceof Headers) {\n headers.forEach((values: string[], name: string) => {\n values.forEach(value => this.append(name, value));\n });\n return;\n }\n\n Object.keys(headers).forEach((name: string) => {\n const values: string[] = Array.isArray(headers[name]) ? headers[name] : [headers[name]];\n this.delete(name);\n values.forEach(value => this.append(name, value));\n });\n }\n\n /**\n * Returns a new Headers instance from the given DOMString of Response Headers\n */\n static fromResponseHeaderString(headersString: string): Headers {\n const headers = new Headers();\n\n headersString.split('\\n').forEach(line => {\n const index = line.indexOf(':');\n if (index > 0) {\n const name = line.slice(0, index);\n const value = line.slice(index + 1).trim();\n headers.set(name, value);\n }\n });\n\n return headers;\n }\n\n /**\n * Appends a header to existing list of header values for a given header name.\n */\n append(name: string, value: string): void {\n const values = this.getAll(name);\n\n if (values === null) {\n this.set(name, value);\n } else {\n values.push(value);\n }\n }\n\n /**\n * Deletes all header values for the given name.\n */\n delete (name: string): void {\n const lcName = name.toLowerCase();\n this._normalizedNames.delete(lcName);\n this._headers.delete(lcName);\n }\n\n forEach(fn: (values: string[], name: string|undefined, headers: Map<string, string[]>) => void):\n void {\n this._headers.forEach(\n (values, lcName) => fn(values, this._normalizedNames.get(lcName), this._headers));\n }\n\n /**\n * Returns first header that matches given name.\n */\n get(name: string): string|null {\n const values = this.getAll(name);\n\n if (values === null) {\n return null;\n }\n\n return values.length > 0 ? values[0] : null;\n }\n\n /**\n * Checks for existence of header by given name.\n */\n has(name: string): boolean { return this._headers.has(name.toLowerCase()); }\n\n /**\n * Returns the names of the headers\n */\n keys(): string[] { return Array.from(this._normalizedNames.values()); }\n\n /**\n * Sets or overrides header value for given name.\n */\n set(name: string, value: string|string[]): void {\n if (Array.isArray(value)) {\n if (value.length) {\n this._headers.set(name.toLowerCase(), [value.join(',')]);\n }\n } else {\n this._headers.set(name.toLowerCase(), [value]);\n }\n this.mayBeSetNormalizedName(name);\n }\n\n /**\n * Returns values of all headers.\n */\n values(): string[][] { return Array.from(this._headers.values()); }\n\n /**\n * Returns string of all headers.\n */\n // TODO(vicb): returns {[name: string]: string[]}\n toJSON(): {[name: string]: any} {\n const serialized: {[name: string]: string[]} = {};\n\n this._headers.forEach((values: string[], name: string) => {\n const split: string[] = [];\n values.forEach(v => split.push(...v.split(',')));\n serialized[this._normalizedNames.get(name) !] = split;\n });\n\n return serialized;\n }\n\n /**\n * Returns list of header values for a given name.\n */\n getAll(name: string): string[]|null {\n return this.has(name) ? this._headers.get(name.toLowerCase()) || null : null;\n }\n\n /**\n * This method is not implemented.\n */\n entries() { throw new Error('\"entries\" method is not implemented on Headers class'); }\n\n private mayBeSetNormalizedName(name: string): void {\n const lcName = name.toLowerCase();\n\n if (!this._normalizedNames.has(lcName)) {\n this._normalizedNames.set(lcName, name);\n }\n }\n}\n","/**\n * @license\n * Copyright Google Inc. All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\n\nimport {Injectable} from '@angular/core';\n\nimport {ResponseType} from './enums';\nimport {Headers} from './headers';\nimport {ResponseOptionsArgs} from './interfaces';\n\n\n/**\n * Creates a response options object to be optionally provided when instantiating a\n * {@link Response}.\n *\n * This class is based on the `ResponseInit` description in the [Fetch\n * Spec](https://fetch.spec.whatwg.org/#responseinit).\n *\n * All values are null by default. Typical defaults can be found in the\n * {@link BaseResponseOptions} class, which sub-classes `ResponseOptions`.\n *\n * This class may be used in tests to build {@link Response Responses} for\n * mock responses (see {@link MockBackend}).\n *\n * @usageNotes\n * ### Example\n *\n * ```typescript\n * import {ResponseOptions, Response} from '@angular/http';\n *\n * var options = new ResponseOptions({\n * body: '{\"name\":\"Jeff\"}'\n * });\n * var res = new Response(options);\n *\n * console.log('res.json():', res.json()); // Object {name: \"Jeff\"}\n * ```\n *\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\nexport class ResponseOptions {\n // TODO: FormData | Blob\n /**\n * String, Object, ArrayBuffer or Blob representing the body of the {@link Response}.\n */\n body: string|Object|ArrayBuffer|Blob|null;\n /**\n * Http {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html status code}\n * associated with the response.\n */\n status: number|null;\n /**\n * Response {@link Headers headers}\n */\n headers: Headers|null;\n /**\n * @internal\n */\n statusText: string|null;\n /**\n * @internal\n */\n type: ResponseType|null;\n url: string|null;\n constructor(opts: ResponseOptionsArgs = {}) {\n const {body, status, headers, statusText, type, url} = opts;\n this.body = body != null ? body : null;\n this.status = status != null ? status : null;\n this.headers = headers != null ? headers : null;\n this.statusText = statusText != null ? statusText : null;\n this.type = type != null ? type : null;\n this.url = url != null ? url : null;\n }\n\n /**\n * Creates a copy of the `ResponseOptions` instance, using the optional input as values to\n * override\n * existing values. This method will not change the values of the instance on which it is being\n * called.\n *\n * This may be useful when sharing a base `ResponseOptions` object inside tests,\n * where certain properties may change from test to test.\n *\n * @usageNotes\n * ### Example\n *\n * ```typescript\n * import {ResponseOptions, Response} from '@angular/http';\n *\n * var options = new ResponseOptions({\n * body: {name: 'Jeff'}\n * });\n * var res = new Response(options.merge({\n * url: 'https://google.com'\n * }));\n * console.log('options.url:', options.url); // null\n * console.log('res.json():', res.json()); // Object {name: \"Jeff\"}\n * console.log('res.url:', res.url); // https://google.com\n * ```\n */\n merge(options?: ResponseOptionsArgs): ResponseOptions {\n return new ResponseOptions({\n body: options && options.body != null ? options.body : this.body,\n status: options && options.status != null ? options.status : this.status,\n headers: options && options.headers != null ? options.headers : this.headers,\n statusText: options && options.statusText != null ? options.statusText : this.statusText,\n type: options && options.type != null ? options.type : this.type,\n url: options && options.url != null ? options.url : this.url,\n });\n }\n}\n\n/**\n * Subclass of {@link ResponseOptions}, with default values.\n *\n * Default values:\n * * status: 200\n * * headers: empty {@link Headers} object\n *\n * This class could be extended and bound to the {@link ResponseOptions} class\n * when configuring an {@link Injector}, in order to override the default options\n * used by {@link Http} to create {@link Response Responses}.\n *\n * @usageNotes\n * ### Example\n *\n * ```typescript\n * import {provide} from '@angular/core';\n * import {bootstrap} from '@angular/platform-browser/browser';\n * import {HTTP_PROVIDERS, Headers, Http, BaseResponseOptions, ResponseOptions} from\n * '@angular/http';\n * import {App} from './myapp';\n *\n * class MyOptions extends BaseResponseOptions {\n * headers:Headers = new Headers({network: 'github'});\n * }\n *\n * bootstrap(App, [HTTP_PROVIDERS, {provide: ResponseOptions, useClass: MyOptions}]);\n * ```\n *\n * The options could also be extended when manually creating a {@link Response}\n * object.\n *\n * ### Example\n *\n * ```\n * import {BaseResponseOptions, Response} from '@angular/http';\n *\n * var options = new BaseResponseOptions();\n * var res = new Response(options.merge({\n * body: 'Angular',\n * headers: new Headers({framework: 'angular'})\n * }));\n * console.log('res.headers.get(\"framework\"):', res.headers.get('framework')); // angular\n * console.log('res.text():', res.text()); // Angular;\n * ```\n *\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\n@Injectable()\nexport class BaseResponseOptions extends ResponseOptions {\n constructor() {\n super({status: 200, statusText: 'Ok', type: ResponseType.Default, headers: new Headers()});\n }\n}\n","/**\n * @license\n * Copyright Google Inc. All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\n\nimport {ReadyState, RequestMethod, ResponseContentType, ResponseType} from './enums';\nimport {Headers} from './headers';\nimport {Request} from './static_request';\nimport {URLSearchParams} from './url_search_params';\n\n/**\n * Abstract class from which real backends are derived.\n *\n * The primary purpose of a `ConnectionBackend` is to create new connections to fulfill a given\n * {@link Request}.\n *\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\nexport abstract class ConnectionBackend { abstract createConnection(request: any): Connection; }\n\n/**\n * Abstract class from which real connections are derived.\n *\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\nexport abstract class Connection {\n // TODO(issue/24571): remove '!'.\n readyState !: ReadyState;\n // TODO(issue/24571): remove '!'.\n request !: Request;\n response: any; // TODO: generic of <Response>;\n}\n\n/**\n * An XSRFStrategy configures XSRF protection (e.g. via headers) on an HTTP request.\n *\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\nexport abstract class XSRFStrategy { abstract configureRequest(req: Request): void; }\n\n/**\n * Interface for options to construct a RequestOptions, based on\n * [RequestInit](https://fetch.spec.whatwg.org/#requestinit) from the Fetch spec.\n *\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\nexport interface RequestOptionsArgs {\n url?: string|null;\n method?: string|RequestMethod|null;\n /** @deprecated from 4.0.0. Use params instead. */\n search?: string|URLSearchParams|{[key: string]: any | any[]}|null;\n params?: string|URLSearchParams|{[key: string]: any | any[]}|null;\n headers?: Headers|null;\n body?: any;\n withCredentials?: boolean|null;\n responseType?: ResponseContentType|null;\n}\n\n/**\n * Required structure when constructing new Request();\n */\nexport interface RequestArgs extends RequestOptionsArgs { url: string|null; }\n\n/**\n * Interface for options to construct a Response, based on\n * [ResponseInit](https://fetch.spec.whatwg.org/#responseinit) from the Fetch spec.\n *\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\nexport interface ResponseOptionsArgs {\n body?: string|Object|FormData|ArrayBuffer|Blob|null;\n status?: number|null;\n statusText?: string|null;\n headers?: Headers|null;\n type?: ResponseType|null;\n url?: string|null;\n}\n","/**\n * @license\n * Copyright Google Inc. All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\n\nimport {RequestMethod} from './enums';\n\nexport function normalizeMethodName(method: string | RequestMethod): RequestMethod {\n if (typeof method !== 'string') return method;\n\n switch (method.toUpperCase()) {\n case 'GET':\n return RequestMethod.Get;\n case 'POST':\n return RequestMethod.Post;\n case 'PUT':\n return RequestMethod.Put;\n case 'DELETE':\n return RequestMethod.Delete;\n case 'OPTIONS':\n return RequestMethod.Options;\n case 'HEAD':\n return RequestMethod.Head;\n case 'PATCH':\n return RequestMethod.Patch;\n }\n throw new Error(`Invalid request method. The method \"${method}\" is not supported.`);\n}\n\nexport const isSuccess = (status: number): boolean => (status >= 200 && status < 300);\n\nexport function getResponseURL(xhr: any): string|null {\n if ('responseURL' in xhr) {\n return xhr.responseURL;\n }\n if (/^X-Request-URL:/m.test(xhr.getAllResponseHeaders())) {\n return xhr.getResponseHeader('X-Request-URL');\n }\n return null;\n}\n\nexport function stringToArrayBuffer8(input: String): ArrayBuffer {\n const view = new Uint8Array(input.length);\n for (let i = 0, strLen = input.length; i < strLen; i++) {\n view[i] = input.charCodeAt(i);\n }\n return view.buffer;\n}\n\n\nexport function stringToArrayBuffer(input: String): ArrayBuffer {\n const view = new Uint16Array(input.length);\n for (let i = 0, strLen = input.length; i < strLen; i++) {\n view[i] = input.charCodeAt(i);\n }\n return view.buffer;\n}\n","/**\n * @license\n * Copyright Google Inc. All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\n\nfunction paramParser(rawParams: string = ''): Map<string, string[]> {\n const map = new Map<string, string[]>();\n if (rawParams.length > 0) {\n const params: string[] = rawParams.split('&');\n params.forEach((param: string) => {\n const eqIdx = param.indexOf('=');\n const [key, val]: string[] =\n eqIdx == -1 ? [param, ''] : [param.slice(0, eqIdx), param.slice(eqIdx + 1)];\n const list = map.get(key) || [];\n list.push(val);\n map.set(key, list);\n });\n }\n return map;\n}\n/**\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n **/\nexport class QueryEncoder {\n encodeKey(key: string): string { return standardEncoding(key); }\n\n encodeValue(value: string): string { return standardEncoding(value); }\n}\n\nfunction standardEncoding(v: string): string {\n return encodeURIComponent(v)\n .replace(/%40/gi, '@')\n .replace(/%3A/gi, ':')\n .replace(/%24/gi, '$')\n .replace(/%2C/gi, ',')\n .replace(/%3B/gi, ';')\n .replace(/%2B/gi, '+')\n .replace(/%3D/gi, '=')\n .replace(/%3F/gi, '?')\n .replace(/%2F/gi, '/');\n}\n\n/**\n * Map-like representation of url search parameters, based on\n * [URLSearchParams](https://url.spec.whatwg.org/#urlsearchparams) in the url living standard,\n * with several extensions for merging URLSearchParams objects:\n * - setAll()\n * - appendAll()\n * - replaceAll()\n *\n * This class accepts an optional second parameter of ${@link QueryEncoder},\n * which is used to serialize parameters before making a request. By default,\n * `QueryEncoder` encodes keys and values of parameters using `encodeURIComponent`,\n * and then un-encodes certain characters that are allowed to be part of the query\n * according to IETF RFC 3986: https://tools.ietf.org/html/rfc3986.\n *\n * These are the characters that are not encoded: `! $ \\' ( ) * + , ; A 9 - . _ ~ ? /`\n *\n * If the set of allowed query characters is not acceptable for a particular backend,\n * `QueryEncoder` can be subclassed and provided as the 2nd argument to URLSearchParams.\n *\n * ```\n * import {URLSearchParams, QueryEncoder} from '@angular/http';\n * class MyQueryEncoder extends QueryEncoder {\n * encodeKey(k: string): string {\n * return myEncodingFunction(k);\n * }\n *\n * encodeValue(v: string): string {\n * return myEncodingFunction(v);\n * }\n * }\n *\n * let params = new URLSearchParams('', new MyQueryEncoder());\n * ```\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\nexport class URLSearchParams {\n paramsMap: Map<string, string[]>;\n constructor(\n public rawParams: string = '', private queryEncoder: QueryEncoder = new QueryEncoder()) {\n this.paramsMap = paramParser(rawParams);\n }\n\n clone(): URLSearchParams {\n const clone = new URLSearchParams('', this.queryEncoder);\n clone.appendAll(this);\n return clone;\n }\n\n has(param: string): boolean { return this.paramsMap.has(param); }\n\n get(param: string): string|null {\n const storedParam = this.paramsMap.get(param);\n\n return Array.isArray(storedParam) ? storedParam[0] : null;\n }\n\n getAll(param: string): string[] { return this.paramsMap.get(param) || []; }\n\n set(param: string, val: string) {\n if (val === void 0 || val === null) {\n this.delete(param);\n return;\n }\n const list = this.paramsMap.get(param) || [];\n list.length = 0;\n list.push(val);\n this.paramsMap.set(param, list);\n }\n\n // A merge operation\n // For each name-values pair in `searchParams`, perform `set(name, values[0])`\n //\n // E.g: \"a=[1,2,3], c=[8]\" + \"a=[4,5,6], b=[7]\" = \"a=[4], c=[8], b=[7]\"\n //\n // TODO(@caitp): document this better\n setAll(searchParams: URLSearchParams) {\n searchParams.paramsMap.forEach((value, param) => {\n const list = this.paramsMap.get(param) || [];\n list.length = 0;\n list.push(value[0]);\n this.paramsMap.set(param, list);\n });\n }\n\n append(param: string, val: string): void {\n if (val === void 0 || val === null) return;\n const list = this.paramsMap.get(param) || [];\n list.push(val);\n this.paramsMap.set(param, list);\n }\n\n // A merge operation\n // For each name-values pair in `searchParams`, perform `append(name, value)`\n // for each value in `values`.\n //\n // E.g: \"a=[1,2], c=[8]\" + \"a=[3,4], b=[7]\" = \"a=[1,2,3,4], c=[8], b=[7]\"\n //\n // TODO(@caitp): document this better\n appendAll(searchParams: URLSearchParams) {\n searchParams.paramsMap.forEach((value, param) => {\n const list = this.paramsMap.get(param) || [];\n for (let i = 0; i < value.length; ++i) {\n list.push(value[i]);\n }\n this.paramsMap.set(param, list);\n });\n }\n\n\n // A merge operation\n // For each name-values pair in `searchParams`, perform `delete(name)`,\n // followed by `set(name, values)`\n //\n // E.g: \"a=[1,2,3], c=[8]\" + \"a=[4,5,6], b=[7]\" = \"a=[4,5,6], c=[8], b=[7]\"\n //\n // TODO(@caitp): document this better\n replaceAll(searchParams: URLSearchParams) {\n searchParams.paramsMap.forEach((value, param) => {\n const list = this.paramsMap.get(param) || [];\n list.length = 0;\n for (let i = 0; i < value.length; ++i) {\n list.push(value[i]);\n }\n this.paramsMap.set(param, list);\n });\n }\n\n toString(): string {\n const paramsList: string[] = [];\n this.paramsMap.forEach((values, k) => {\n values.forEach(\n v => paramsList.push(\n this.queryEncoder.encodeKey(k) + '=' + this.queryEncoder.encodeValue(v)));\n });\n return paramsList.join('&');\n }\n\n delete (param: string): void { this.paramsMap.delete(param); }\n}\n","/**\n * @license\n * Copyright Google Inc. All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\n\nimport {stringToArrayBuffer} from './http_utils';\nimport {URLSearchParams} from './url_search_params';\n\n\n/**\n * HTTP request body used by both {@link Request} and {@link Response}\n * https://fetch.spec.whatwg.org/#body\n */\nexport abstract class Body {\n /**\n * @internal\n */\n protected _body: any;\n\n /**\n * Attempts to return body as parsed `JSON` object, or raises an exception.\n */\n json(): any {\n if (typeof this._body === 'string') {\n return JSON.parse(<string>this._body);\n }\n\n if (this._body instanceof ArrayBuffer) {\n return JSON.parse(this.text());\n }\n\n return this._body;\n }\n\n /**\n * Returns the body as a string, presuming `toString()` can be called on the response body.\n *\n * When decoding an `ArrayBuffer`, the optional `encodingHint` parameter determines how the\n * bytes in the buffer will be interpreted. Valid values are:\n *\n * - `legacy` - incorrectly interpret the bytes as UTF-16 (technically, UCS-2). Only characters\n * in the Basic Multilingual Plane are supported, surrogate pairs are not handled correctly.\n * In addition, the endianness of the 16-bit octet pairs in the `ArrayBuffer` is not taken\n * into consideration. This is the default behavior to avoid breaking apps, but should be\n * considered deprecated.\n *\n * - `iso-8859` - interpret the bytes as ISO-8859 (which can be used for ASCII encoded text).\n */\n text(encodingHint: 'legacy'|'iso-8859' = 'legacy'): string {\n if (this._body instanceof URLSearchParams) {\n return this._body.toString();\n }\n\n if (this._body instanceof ArrayBuffer) {\n switch (encodingHint) {\n case 'legacy':\n return String.fromCharCode.apply(null, new Uint16Array(this._body as ArrayBuffer));\n case 'iso-8859':\n return String.fromCharCode.apply(null, new Uint8Array(this._body as ArrayBuffer));\n default:\n throw new Error(`Invalid value for encodingHint: ${encodingHint}`);\n }\n }\n\n if (this._body == null) {\n return '';\n }\n\n if (typeof this._body === 'object') {\n return JSON.stringify(this._body, null, 2);\n }\n\n return this._body.toString();\n }\n\n /**\n * Return the body as an ArrayBuffer\n */\n arrayBuffer(): ArrayBuffer {\n if (this._body instanceof ArrayBuffer) {\n return <ArrayBuffer>this._body;\n }\n\n return stringToArrayBuffer(this.text());\n }\n\n /**\n * Returns the request's body as a Blob, assuming that body exists.\n */\n blob(): Blob {\n if (this._body instanceof Blob) {\n return <Blob>this._body;\n }\n\n if (this._body instanceof ArrayBuffer) {\n return new Blob([this._body]);\n }\n\n throw new Error('The request body isn\\'t either a blob or an array buffer');\n }\n}\n","/**\n * @license\n * Copyright Google Inc. All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\n\n\n\nimport {ResponseOptions} from './base_response_options';\nimport {Body} from './body';\nimport {ResponseType} from './enums';\nimport {Headers} from './headers';\n\n\n/**\n * Creates `Response` instances from provided values.\n *\n * Though this object isn't\n * usually instantiated by end-users, it is the primary object interacted with when it comes time to\n * add data to a view.\n *\n * @usageNotes\n * ### Example\n *\n * ```\n * http.request('my-friends.txt').subscribe(response => this.friends = response.text());\n * ```\n *\n * The Response's interface is inspired by the Response constructor defined in the [Fetch\n * Spec](https://fetch.spec.whatwg.org/#response-class), but is considered a static value whose body\n * can be accessed many times. There are other differences in the implementation, but this is the\n * most significant.\n *\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\nexport class Response extends Body {\n /**\n * One of \"basic\", \"cors\", \"default\", \"error\", or \"opaque\".\n *\n * Defaults to \"default\".\n */\n type: ResponseType;\n /**\n * True if the response's status is within 200-299\n */\n ok: boolean;\n /**\n * URL of response.\n *\n * Defaults to empty string.\n */\n url: string;\n /**\n * Status code returned by server.\n *\n * Defaults to 200.\n */\n status: number;\n /**\n * Text representing the corresponding reason phrase to the `status`, as defined in [ietf rfc 2616\n * section 6.1.1](https://tools.ietf.org/html/rfc2616#section-6.1.1)\n *\n * Defaults to \"OK\"\n */\n statusText: string|null;\n /**\n * Non-standard property\n *\n * Denotes how many of the response body's bytes have been loaded, for example if the response is\n * the result of a progress event.\n */\n // TODO(issue/24571): remove '!'.\n bytesLoaded !: number;\n /**\n * Non-standard property\n *\n * Denotes how many bytes are expected in the final response body.\n */\n // TODO(issue/24571): remove '!'.\n totalBytes !: number;\n /**\n * Headers object based on the `Headers` class in the [Fetch\n * Spec](https://fetch.spec.whatwg.org/#headers-class).\n */\n headers: Headers|null;\n\n constructor(responseOptions: ResponseOptions) {\n super();\n this._body = responseOptions.body;\n this.status = responseOptions.status !;\n this.ok = (this.status >= 200 && this.status <= 299);\n this.statusText = responseOptions.statusText;\n this.headers = responseOptions.headers;\n this.type = responseOptions.type !;\n this.url = responseOptions.url !;\n }\n\n toString(): string {\n return `Response with status: ${this.status} ${this.statusText} for URL: ${this.url}`;\n }\n}\n","/**\n * @license\n * Copyright Google Inc. All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\n\nimport {Injectable} from '@angular/core';\n\nlet _nextRequestId = 0;\nexport const JSONP_HOME = '__ng_jsonp__';\nlet _jsonpConnections: {[key: string]: any}|null = null;\n\nfunction _getJsonpConnections(): {[key: string]: any} {\n const w: {[key: string]: any} = typeof window == 'object' ? window : {};\n if (_jsonpConnections === null) {\n _jsonpConnections = w[JSONP_HOME] = {};\n }\n return _jsonpConnections;\n}\n\n// Make sure not to evaluate this in a non-browser environment!\n@Injectable()\nexport class BrowserJsonp {\n // Construct a <script> element with the specified URL\n build(url: string): any {\n const node = document.createElement('script');\n node.src = url;\n return node;\n }\n\n nextRequestID(): string { return `__req${_nextRequestId++}`; }\n\n requestCallback(id: string): string { return `${JSONP_HOME}.${id}.finished`; }\n\n exposeConnection(id: string, connection: any) {\n const connections = _getJsonpConnections();\n connections[id] = connection;\n }\n\n removeConnection(id: string) {\n const connections = _getJsonpConnections();\n connections[id] = null;\n }\n\n // Attach the <script> element to the DOM\n send(node: any) { document.body.appendChild(<Node>(node)); }\n\n // Remove <script> element from the DOM\n cleanup(node: any) {\n if (node.parentNode) {\n node.parentNode.removeChild(<Node>(node));\n }\n }\n}\n","/**\n * @license\n * Copyright Google Inc. All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\n\nimport {Injectable} from '@angular/core';\nimport {Observable, Observer} from 'rxjs';\n\nimport {ResponseOptions} from '../base_response_options';\nimport {ReadyState, RequestMethod, ResponseType} from '../enums';\nimport {Connection, ConnectionBackend} from '../interfaces';\nimport {Request} from '../static_request';\nimport {Response} from '../static_response';\n\nimport {BrowserJsonp} from './browser_jsonp';\n\nconst JSONP_ERR_NO_CALLBACK = 'JSONP injected script did not invoke callback.';\nconst JSONP_ERR_WRONG_METHOD = 'JSONP requests must use GET request method.';\n\n/**\n * Base class for an in-flight JSONP request.\n *\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\nexport class JSONPConnection implements Connection {\n // TODO(issue/24571): remove '!'.\n private _id !: string;\n // TODO(issue/24571): remove '!'.\n private _script !: Element;\n private _responseData: any;\n private _finished: boolean = false;\n\n /**\n * The {@link ReadyState} of this request.\n */\n // TODO(issue/24571): remove '!'.\n readyState !: ReadyState;\n\n /**\n * The outgoing HTTP request.\n */\n request: Request;\n\n /**\n * An observable that completes with the response, when the request is finished.\n */\n response: Observable<Response>;\n\n /** @internal */\n constructor(\n req: Request, private _dom: BrowserJsonp, private baseResponseOptions?: ResponseOptions) {\n if (req.method !== RequestMethod.Get) {\n throw new TypeError(JSONP_ERR_WRONG_METHOD);\n }\n this.request = req;\n this.response = new Observable<Response>((responseObserver: Observer<Response>) => {\n\n this.readyState = ReadyState.Loading;\n const id = this._id = _dom.nextRequestID();\n\n _dom.exposeConnection(id, this);\n\n // Workaround Dart\n // url = url.replace(/=JSONP_CALLBACK(&|$)/, `generated method`);\n const callback = _dom.requestCallback(this._id);\n let url: string = req.url;\n if (url.indexOf('=JSONP_CALLBACK&') > -1) {\n url = url.replace('=JSONP_CALLBACK&', `=${callback}&`);\n } else if (url.lastIndexOf('=JSONP_CALLBACK') === url.length - '=JSONP_CALLBACK'.length) {\n url = url.substring(0, url.length - '=JSONP_CALLBACK'.length) + `=${callback}`;\n }\n\n const script = this._script = _dom.build(url);\n\n const onLoad = (event: Event) => {\n if (this.readyState === ReadyState.Cancelled) return;\n this.readyState = ReadyState.Done;\n _dom.cleanup(script);\n if (!this._finished) {\n let responseOptions =\n new ResponseOptions({body: JSONP_ERR_NO_CALLBACK, type: ResponseType.Error, url});\n if (baseResponseOptions) {\n responseOptions = baseResponseOptions.merge(responseOptions);\n }\n responseObserver.error(new Response(responseOptions));\n return;\n }\n\n let responseOptions = new ResponseOptions({body: this._responseData, url});\n if (this.baseResponseOptions) {\n responseOptions = this.baseResponseOptions.merge(responseOptions);\n }\n\n responseObserver.next(new Response(responseOptions));\n responseObserver.complete();\n };\n\n const onError = (error: Error) => {\n if (this.readyState === ReadyState.Cancelled) return;\n this.readyState = ReadyState.Done;\n _dom.cleanup(script);\n let responseOptions = new ResponseOptions({body: error.message, type: ResponseType.Error});\n if (baseResponseOptions) {\n responseOptions = baseResponseOptions.merge(responseOptions);\n }\n responseObserver.error(new Response(responseOptions));\n };\n\n script.addEventListener('load', onLoad);\n script.addEventListener('error', onError);\n\n _dom.send(script);\n\n return () => {\n this.readyState = ReadyState.Cancelled;\n script.removeEventListener('load', onLoad);\n script.removeEventListener('error', onError);\n this._dom.cleanup(script);\n };\n });\n }\n\n /**\n * Callback called when the JSONP request completes, to notify the application\n * of the new data.\n */\n finished(data?: any) {\n // Don't leak connections\n this._finished = true;\n this._dom.removeConnection(this._id);\n if (this.readyState === ReadyState.Cancelled) return;\n this._responseData = data;\n }\n}\n\n/**\n * A {@link ConnectionBackend} that uses the JSONP strategy of making requests.\n *\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\n@Injectable()\nexport class JSONPBackend extends ConnectionBackend {\n /** @internal */\n constructor(private _browserJSONP: BrowserJsonp, private _baseResponseOptions: ResponseOptions) {\n super();\n }\n\n createConnection(request: Request): JSONPConnection {\n return new JSONPConnection(request, this._browserJSONP, this._baseResponseOptions);\n }\n}\n","/**\n * @license\n * Copyright Google Inc. All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\n\nimport {Injectable} from '@angular/core';\nimport {ɵgetDOM as getDOM} from '@angular/platform-browser';\nimport {Observable, Observer} from 'rxjs';\nimport {ResponseOptions} from '../base_response_options';\nimport {ContentType, ReadyState, RequestMethod, ResponseContentType, ResponseType} from '../enums';\nimport {Headers} from '../headers';\nimport {getResponseURL, isSuccess} from '../http_utils';\nimport {Connection, ConnectionBackend, XSRFStrategy} from '../interfaces';\nimport {Request} from '../static_request';\nimport {Response} from '../static_response';\nimport {BrowserXhr} from './browser_xhr';\n\nconst XSSI_PREFIX = /^\\)\\]\\}',?\\n/;\n\n/**\n * Creates connections using `XMLHttpRequest`. Given a fully-qualified\n * request, an `XHRConnection` will immediately create an `XMLHttpRequest` object and send the\n * request.\n *\n * This class would typically not be created or interacted with directly inside applications, though\n * the {@link MockConnection} may be interacted with in tests.\n *\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\nexport class XHRConnection implements Connection {\n request: Request;\n /**\n * Response {@link EventEmitter} which emits a single {@link Response} value on load event of\n * `XMLHttpRequest`.\n */\n response: Observable<Response>;\n // TODO(issue/24571): remove '!'.\n readyState !: ReadyState;\n constructor(req: Request, browserXHR: BrowserXhr, baseResponseOptions?: ResponseOptions) {\n this.request = req;\n this.response = new Observable<Response>((responseObserver: Observer<Response>) => {\n const _xhr: XMLHttpRequest = browserXHR.build();\n _xhr.open(RequestMethod[req.method].toUpperCase(), req.url);\n if (req.withCredentials != null) {\n _xhr.withCredentials = req.withCredentials;\n }\n // load event handler\n const onLoad = () => {\n // normalize IE9 bug (http://bugs.jquery.com/ticket/1450)\n let status: number = _xhr.status === 1223 ? 204 : _xhr.status;\n\n let body: any = null;\n\n // HTTP 204 means no content\n if (status !== 204) {\n // responseText is the old-school way of retrieving response (supported by IE8 & 9)\n // response/responseType properties were introduced in ResourceLoader Level2 spec\n // (supported by IE10)\n body = (typeof _xhr.response === 'undefined') ? _xhr.responseText : _xhr.response;\n\n // Implicitly strip a potential XSSI prefix.\n if (typeof body === 'string') {\n body = body.replace(XSSI_PREFIX, '');\n }\n }\n\n // fix status code when it is 0 (0 status is undocumented).\n // Occurs when accessing file resources or on Android 4.1 stock browser\n // while retrieving files from application cache.\n if (status === 0) {\n status = body ? 200 : 0;\n }\n\n const headers: Headers = Headers.fromResponseHeaderString(_xhr.getAllResponseHeaders());\n // IE 9 does not provide the way to get URL of response\n const url = getResponseURL(_xhr) || req.url;\n const statusText: string = _xhr.statusText || 'OK';\n\n let responseOptions = new ResponseOptions({body, status, headers, statusText, url});\n if (baseResponseOptions != null) {\n responseOptions = baseResponseOptions.merge(responseOptions);\n }\n const response = new Response(responseOptions);\n response.ok = isSuccess(status);\n if (response.ok) {\n responseObserver.next(response);\n // TODO(gdi2290): defer complete if array buffer until done\n responseObserver.complete();\n return;\n }\n responseObserver.error(response);\n };\n // error event handler\n const onError = (err: ErrorEvent) => {\n let responseOptions = new ResponseOptions({\n body: err,\n type: ResponseType.Error,\n status: _xhr.status,\n statusText: _xhr.statusText,\n });\n if (baseResponseOptions != null) {\n responseOptions = baseResponseOptions.merge(responseOptions);\n }\n responseObserver.error(new Response(responseOptions));\n };\n\n this.setDetectedContentType(req, _xhr);\n\n if (req.headers == null) {\n req.headers = new Headers();\n }\n if (!req.headers.has('Accept')) {\n req.headers.append('Accept', 'application/json, text/plain, */*');\n }\n req.headers.forEach((values, name) => _xhr.setRequestHeader(name !, values.join(',')));\n\n // Select the correct buffer type to store the response\n if (req.responseType != null && _xhr.responseType != null) {\n switch (req.responseType) {\n case ResponseContentType.ArrayBuffer:\n _xhr.responseType = 'arraybuffer';\n break;\n case ResponseContentType.Json:\n _xhr.responseType = 'json';\n break;\n case ResponseContentType.Text:\n _xhr.responseType = 'text';\n break;\n case ResponseContentType.Blob:\n _xhr.responseType = 'blob';\n break;\n default:\n throw new Error('The selected responseType is not supported');\n }\n }\n\n _xhr.addEventListener('load', onLoad);\n _xhr.addEventListener('error', onError);\n\n _xhr.send(this.request.getBody());\n\n return () => {\n _xhr.removeEventListener('load', onLoad);\n _xhr.removeEventListener('error', onError);\n _xhr.abort();\n };\n });\n }\n\n setDetectedContentType(req: any /** TODO Request */, _xhr: any /** XMLHttpRequest */) {\n // Skip if a custom Content-Type header is provided\n if (req.headers != null && req.headers.get('Content-Type') != null) {\n return;\n }\n\n // Set the detected content type\n switch (req.contentType) {\n case ContentType.NONE:\n break;\n case ContentType.JSON:\n _xhr.setRequestHeader('content-type', 'application/json');\n break;\n case ContentType.FORM:\n _xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8');\n break;\n case ContentType.TEXT:\n _xhr.setRequestHeader('content-type', 'text/plain');\n break;\n case ContentType.BLOB:\n const blob = req.blob();\n if (blob.type) {\n _xhr.setRequestHeader('content-type', blob.type);\n }\n break;\n }\n }\n}\n\n/**\n * `XSRFConfiguration` sets up Cross Site Request Forgery (XSRF) protection for the application\n * using a cookie. See https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)\n * for more information on XSRF.\n *\n * Applications can configure custom cookie and header names by binding an instance of this class\n * with different `cookieName` and `headerName` values. See the main HTTP documentation for more\n * details.\n *\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\nexport class CookieXSRFStrategy implements XSRFStrategy {\n constructor(\n private _cookieName: string = 'XSRF-TOKEN', private _headerName: string = 'X-XSRF-TOKEN') {}\n\n configureRequest(req: Request): void {\n const xsrfToken = getDOM().getCookie(this._cookieName);\n if (xsrfToken) {\n req.headers.set(this._headerName, xsrfToken);\n }\n }\n}\n\n/**\n * Creates {@link XHRConnection} instances.\n *\n * This class would typically not be used by end users, but could be\n * overridden if a different backend implementation should be used,\n * such as in a node backend.\n *\n * @usageNotes\n * ### Example\n *\n * ```\n * import {Http, MyNodeBackend, HTTP_PROVIDERS, BaseRequestOptions} from '@angular/http';\n * @Component({\n * viewProviders: [\n * HTTP_PROVIDERS,\n * {provide: Http, useFactory: (backend, options) => {\n * return new Http(backend, options);\n * }, deps: [MyNodeBackend, BaseRequestOptions]}]\n * })\n * class MyComponent {\n * constructor(http:Http) {\n * http.request('people.json').subscribe(res => this.people = res.json());\n * }\n * }\n * ```\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\n@Injectable()\nexport class XHRBackend implements ConnectionBackend {\n constructor(\n private _browserXHR: BrowserXhr, private _baseResponseOptions: ResponseOptions,\n private _xsrfStrategy: XSRFStrategy) {}\n\n createConnection(request: Request): XHRConnection {\n this._xsrfStrategy.configureRequest(request);\n return new XHRConnection(request, this._browserXHR, this._baseResponseOptions);\n }\n}\n","/**\n * @license\n * Copyright Google Inc. All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\n\nimport {Injectable} from '@angular/core';\n\nimport {RequestMethod, ResponseContentType} from './enums';\nimport {Headers} from './headers';\nimport {normalizeMethodName} from './http_utils';\nimport {RequestOptionsArgs} from './interfaces';\nimport {URLSearchParams} from './url_search_params';\n\n\n/**\n * Creates a request options object to be optionally provided when instantiating a\n * {@link Request}.\n *\n * This class is based on the `RequestInit` description in the [Fetch\n * Spec](https://fetch.spec.whatwg.org/#requestinit).\n *\n * All values are null by default. Typical defaults can be found in the {@link BaseRequestOptions}\n * class, which sub-classes `RequestOptions`.\n *\n * ```typescript\n * import {RequestOptions, Request, RequestMethod} from '@angular/http';\n *\n * const options = new RequestOptions({\n * method: RequestMethod.Post,\n * url: 'https://google.com'\n * });\n * const req = new Request(options);\n * console.log('req.method:', RequestMethod[req.method]); // Post\n * console.log('options.url:', options.url); // https://google.com\n * ```\n *\n * @deprecated see https://angular.io/guide/http\n * @publicApi\n */\nexport class RequestOptions {\n /**\n * Http method with which to execute a {@link Request}.\n * Acceptable methods are defined in the {@link RequestMethod} enum.\n */\n method: RequestMethod|string|null;\n /**\n * {@link Headers} to be attached to a {@link Request}.\n */\n headers: Headers|null;\n /**\n * Body to be used when creating a {@link Request}.\n */\n body: any;\n /**\n * Url with which to perform a {@link Request}.\n */\n url: string|null;\n /**\n * Search parameters to be included in a {@link Request}.\n */\n params: URLSearchParams;\n /**\n * @deprecated from 4.0.0. Use params instead.\n */\n get search(): URLSearchParams { return this.params; }\n /**\n * @deprecated from 4.0.0. Use params instead.\n */\n set search(params: URLSearchParams) { this.params = params; }\n /**\n * Enable use credentials for a {@link Request}.\n */\n withCredentials: boolean|null;\n /*\n * Select a buffer to store the response, such as ArrayBuffer, Blob, Json (or Document)\n */\n responseType: ResponseContentType|null;\n\n // TODO(Dzmitry): remove search when this.search is removed\n constructor(opts: RequestOptionsArgs = {}) {\n const {method, headers, body, url, search, params, withCredentials, responseType} = opts;\n this.method = method != null ? normalizeMethodName(method) : null;\n this.headers = headers != null ? headers : null;\n this.body = body != null ? body : null;\n this.url = url != null ? url : null;\n this.params = this._mergeSearchParams(params || search);\n this.withCredentials = withCredentials != null ? withCredentials : null;\n this.responseType = responseType != null ? responseType : null;\n }\n\n /**\n * Creates a copy of the `RequestOptions` instance, using the optional input as values to override\n * existing values. This method will not change the values of the instance on which it is being\n * called.\n *\n * Note that `headers` and `search` will override existing values completely if present in\n * the `options` object. If these values should be merged, it should be done prior to calling\n * `merge` on the `RequestOptions` instance.\n *\n * ```typescript\n * import {RequestOptions, Request, RequestMethod} from '@angular/http';\n *\n * const options = new RequestOptions({\n * method: RequestMethod.Post\n * });\n * const req = new Request(options.merge({\n * url: 'https://google.com'\n * }));\n * console.log('req.method:', RequestMethod[req.method]); // Post\n * console.log('options.url:', options.url); // null\n * console.log('req.url:', req.url); // https://google.com\n * ```\n */\n merge(options?: RequestOptionsArgs): RequestOptions {\n return new RequestOptions({\n method: options && options.method != null ? options.method : this.method,\n headers: options && options.headers != null ? options.headers : new Headers(this.headers),\n body: options