@angular/common
Version:
Angular - commonly needed directives and services
1,444 lines (1,431 loc) • 104 kB
JavaScript
/**
* @license Angular v5.2.11
* (c) 2010-2018 Google, Inc. https://angular.io/
* License: MIT
*/
import { Inject, Injectable, InjectionToken, Injector, NgModule, PLATFORM_ID } from '@angular/core';
import { of } from 'rxjs/observable/of';
import { concatMap } from 'rxjs/operator/concatMap';
import { filter } from 'rxjs/operator/filter';
import { map } from 'rxjs/operator/map';
import { __extends } from 'tslib';
import { DOCUMENT, ɵparseCookieValue } from '@angular/common';
import { Observable } from 'rxjs/Observable';
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @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
*/
/**
* Transforms an `HttpRequest` into a stream of `HttpEvent`s, one of which will likely be a
* `HttpResponse`.
*
* `HttpHandler` is injectable. When injected, the handler instance dispatches requests to the
* first interceptor in the chain, which dispatches to the second, etc, eventually reaching the
* `HttpBackend`.
*
* In an `HttpInterceptor`, the `HttpHandler` parameter is the next interceptor in the chain.
*
* \@stable
* @abstract
*/
var HttpHandler = /** @class */ (function () {
function HttpHandler() {
}
return HttpHandler;
}());
/**
* A final `HttpHandler` which will dispatch the request via browser HTTP APIs to a backend.
*
* Interceptors sit between the `HttpClient` interface and the `HttpBackend`.
*
* When injected, `HttpBackend` dispatches requests directly to the backend, without going
* through the interceptor chain.
*
* \@stable
* @abstract
*/
var HttpBackend = /** @class */ (function () {
function HttpBackend() {
}
return HttpBackend;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @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
*/
/**
* @record
*/
/**
* Immutable set of Http headers, with lazy parsing.
* \@stable
*/
var HttpHeaders = /** @class */ (function () {
function HttpHeaders(headers) {
var _this = this;
/**
* Internal map of lowercased header names to the normalized
* form of the name (the form seen first).
*/
this.normalizedNames = new Map();
/**
* Queued updates to be materialized the next initialization.
*/
this.lazyUpdate = null;
if (!headers) {
this.headers = new Map();
}
else if (typeof headers === 'string') {
this.lazyInit = function () {
_this.headers = new Map();
headers.split('\n').forEach(function (line) {
var /** @type {?} */ index = line.indexOf(':');
if (index > 0) {
var /** @type {?} */ name_1 = line.slice(0, index);
var /** @type {?} */ key = name_1.toLowerCase();
var /** @type {?} */ value = line.slice(index + 1).trim();
_this.maybeSetNormalizedName(name_1, key);
if (_this.headers.has(key)) {
/** @type {?} */ ((_this.headers.get(key))).push(value);
}
else {
_this.headers.set(key, [value]);
}
}
});
};
}
else {
this.lazyInit = function () {
_this.headers = new Map();
Object.keys(headers).forEach(function (name) {
var /** @type {?} */ values = headers[name];
var /** @type {?} */ key = name.toLowerCase();
if (typeof values === 'string') {
values = [values];
}
if (values.length > 0) {
_this.headers.set(key, values);
_this.maybeSetNormalizedName(name, key);
}
});
};
}
}
/**
* Checks for existence of header by given name.
*/
/**
* Checks for existence of header by given name.
* @param {?} name
* @return {?}
*/
HttpHeaders.prototype.has = /**
* Checks for existence of header by given name.
* @param {?} name
* @return {?}
*/
function (name) {
this.init();
return this.headers.has(name.toLowerCase());
};
/**
* Returns first header that matches given name.
*/
/**
* Returns first header that matches given name.
* @param {?} name
* @return {?}
*/
HttpHeaders.prototype.get = /**
* Returns first header that matches given name.
* @param {?} name
* @return {?}
*/
function (name) {
this.init();
var /** @type {?} */ values = this.headers.get(name.toLowerCase());
return values && values.length > 0 ? values[0] : null;
};
/**
* Returns the names of the headers
*/
/**
* Returns the names of the headers
* @return {?}
*/
HttpHeaders.prototype.keys = /**
* Returns the names of the headers
* @return {?}
*/
function () {
this.init();
return Array.from(this.normalizedNames.values());
};
/**
* Returns list of header values for a given name.
*/
/**
* Returns list of header values for a given name.
* @param {?} name
* @return {?}
*/
HttpHeaders.prototype.getAll = /**
* Returns list of header values for a given name.
* @param {?} name
* @return {?}
*/
function (name) {
this.init();
return this.headers.get(name.toLowerCase()) || null;
};
/**
* @param {?} name
* @param {?} value
* @return {?}
*/
HttpHeaders.prototype.append = /**
* @param {?} name
* @param {?} value
* @return {?}
*/
function (name, value) {
return this.clone({ name: name, value: value, op: 'a' });
};
/**
* @param {?} name
* @param {?} value
* @return {?}
*/
HttpHeaders.prototype.set = /**
* @param {?} name
* @param {?} value
* @return {?}
*/
function (name, value) {
return this.clone({ name: name, value: value, op: 's' });
};
/**
* @param {?} name
* @param {?=} value
* @return {?}
*/
HttpHeaders.prototype.delete = /**
* @param {?} name
* @param {?=} value
* @return {?}
*/
function (name, value) {
return this.clone({ name: name, value: value, op: 'd' });
};
/**
* @param {?} name
* @param {?} lcName
* @return {?}
*/
HttpHeaders.prototype.maybeSetNormalizedName = /**
* @param {?} name
* @param {?} lcName
* @return {?}
*/
function (name, lcName) {
if (!this.normalizedNames.has(lcName)) {
this.normalizedNames.set(lcName, name);
}
};
/**
* @return {?}
*/
HttpHeaders.prototype.init = /**
* @return {?}
*/
function () {
var _this = this;
if (!!this.lazyInit) {
if (this.lazyInit instanceof HttpHeaders) {
this.copyFrom(this.lazyInit);
}
else {
this.lazyInit();
}
this.lazyInit = null;
if (!!this.lazyUpdate) {
this.lazyUpdate.forEach(function (update) { return _this.applyUpdate(update); });
this.lazyUpdate = null;
}
}
};
/**
* @param {?} other
* @return {?}
*/
HttpHeaders.prototype.copyFrom = /**
* @param {?} other
* @return {?}
*/
function (other) {
var _this = this;
other.init();
Array.from(other.headers.keys()).forEach(function (key) {
_this.headers.set(key, /** @type {?} */ ((other.headers.get(key))));
_this.normalizedNames.set(key, /** @type {?} */ ((other.normalizedNames.get(key))));
});
};
/**
* @param {?} update
* @return {?}
*/
HttpHeaders.prototype.clone = /**
* @param {?} update
* @return {?}
*/
function (update) {
var /** @type {?} */ clone = new HttpHeaders();
clone.lazyInit =
(!!this.lazyInit && this.lazyInit instanceof HttpHeaders) ? this.lazyInit : this;
clone.lazyUpdate = (this.lazyUpdate || []).concat([update]);
return clone;
};
/**
* @param {?} update
* @return {?}
*/
HttpHeaders.prototype.applyUpdate = /**
* @param {?} update
* @return {?}
*/
function (update) {
var /** @type {?} */ key = update.name.toLowerCase();
switch (update.op) {
case 'a':
case 's':
var /** @type {?} */ value = /** @type {?} */ ((update.value));
if (typeof value === 'string') {
value = [value];
}
if (value.length === 0) {
return;
}
this.maybeSetNormalizedName(update.name, key);
var /** @type {?} */ base = (update.op === 'a' ? this.headers.get(key) : undefined) || [];
base.push.apply(base, value);
this.headers.set(key, base);
break;
case 'd':
var /** @type {?} */ toDelete_1 = /** @type {?} */ (update.value);
if (!toDelete_1) {
this.headers.delete(key);
this.normalizedNames.delete(key);
}
else {
var /** @type {?} */ existing = this.headers.get(key);
if (!existing) {
return;
}
existing = existing.filter(function (value) { return toDelete_1.indexOf(value) === -1; });
if (existing.length === 0) {
this.headers.delete(key);
this.normalizedNames.delete(key);
}
else {
this.headers.set(key, existing);
}
}
break;
}
};
/**
* @internal
*/
/**
* \@internal
* @param {?} fn
* @return {?}
*/
HttpHeaders.prototype.forEach = /**
* \@internal
* @param {?} fn
* @return {?}
*/
function (fn) {
var _this = this;
this.init();
Array.from(this.normalizedNames.keys())
.forEach(function (key) { return fn(/** @type {?} */ ((_this.normalizedNames.get(key))), /** @type {?} */ ((_this.headers.get(key)))); });
};
return HttpHeaders;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @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
*/
/**
* A codec for encoding and decoding parameters in URLs.
*
* Used by `HttpParams`.
*
* \@stable
*
* @record
*/
/**
* A `HttpParameterCodec` that uses `encodeURIComponent` and `decodeURIComponent` to
* serialize and parse URL parameter keys and values.
*
* \@stable
*/
var HttpUrlEncodingCodec = /** @class */ (function () {
function HttpUrlEncodingCodec() {
}
/**
* @param {?} k
* @return {?}
*/
HttpUrlEncodingCodec.prototype.encodeKey = /**
* @param {?} k
* @return {?}
*/
function (k) { return standardEncoding(k); };
/**
* @param {?} v
* @return {?}
*/
HttpUrlEncodingCodec.prototype.encodeValue = /**
* @param {?} v
* @return {?}
*/
function (v) { return standardEncoding(v); };
/**
* @param {?} k
* @return {?}
*/
HttpUrlEncodingCodec.prototype.decodeKey = /**
* @param {?} k
* @return {?}
*/
function (k) { return decodeURIComponent(k); };
/**
* @param {?} v
* @return {?}
*/
HttpUrlEncodingCodec.prototype.decodeValue = /**
* @param {?} v
* @return {?}
*/
function (v) { return decodeURIComponent(v); };
return HttpUrlEncodingCodec;
}());
/**
* @param {?} rawParams
* @param {?} codec
* @return {?}
*/
function paramParser(rawParams, codec) {
var /** @type {?} */ map$$1 = new Map();
if (rawParams.length > 0) {
var /** @type {?} */ params = rawParams.split('&');
params.forEach(function (param) {
var /** @type {?} */ eqIdx = param.indexOf('=');
var _a = eqIdx == -1 ?
[codec.decodeKey(param), ''] :
[codec.decodeKey(param.slice(0, eqIdx)), codec.decodeValue(param.slice(eqIdx + 1))], key = _a[0], val = _a[1];
var /** @type {?} */ list = map$$1.get(key) || [];
list.push(val);
map$$1.set(key, list);
});
}
return map$$1;
}
/**
* @param {?} v
* @return {?}
*/
function standardEncoding(v) {
return encodeURIComponent(v)
.replace(/%40/gi, '@')
.replace(/%3A/gi, ':')
.replace(/%24/gi, '$')
.replace(/%2C/gi, ',')
.replace(/%3B/gi, ';')
.replace(/%2B/gi, '+')
.replace(/%3D/gi, '=')
.replace(/%3F/gi, '?')
.replace(/%2F/gi, '/');
}
/**
* An HTTP request/response body that represents serialized parameters,
* per the MIME type `application/x-www-form-urlencoded`.
*
* This class is immutable - all mutation operations return a new instance.
*
* \@stable
*/
var HttpParams = /** @class */ (function () {
function HttpParams(options) {
if (options === void 0) { options = /** @type {?} */ ({}); }
var _this = this;
this.updates = null;
this.cloneFrom = null;
this.encoder = options.encoder || new HttpUrlEncodingCodec();
if (!!options.fromString) {
if (!!options.fromObject) {
throw new Error("Cannot specify both fromString and fromObject.");
}
this.map = paramParser(options.fromString, this.encoder);
}
else if (!!options.fromObject) {
this.map = new Map();
Object.keys(options.fromObject).forEach(function (key) {
var /** @type {?} */ value = (/** @type {?} */ (options.fromObject))[key]; /** @type {?} */
((_this.map)).set(key, Array.isArray(value) ? value : [value]);
});
}
else {
this.map = null;
}
}
/**
* Check whether the body has one or more values for the given parameter name.
*/
/**
* Check whether the body has one or more values for the given parameter name.
* @param {?} param
* @return {?}
*/
HttpParams.prototype.has = /**
* Check whether the body has one or more values for the given parameter name.
* @param {?} param
* @return {?}
*/
function (param) {
this.init();
return /** @type {?} */ ((this.map)).has(param);
};
/**
* Get the first value for the given parameter name, or `null` if it's not present.
*/
/**
* Get the first value for the given parameter name, or `null` if it's not present.
* @param {?} param
* @return {?}
*/
HttpParams.prototype.get = /**
* Get the first value for the given parameter name, or `null` if it's not present.
* @param {?} param
* @return {?}
*/
function (param) {
this.init();
var /** @type {?} */ res = /** @type {?} */ ((this.map)).get(param);
return !!res ? res[0] : null;
};
/**
* Get all values for the given parameter name, or `null` if it's not present.
*/
/**
* Get all values for the given parameter name, or `null` if it's not present.
* @param {?} param
* @return {?}
*/
HttpParams.prototype.getAll = /**
* Get all values for the given parameter name, or `null` if it's not present.
* @param {?} param
* @return {?}
*/
function (param) {
this.init();
return /** @type {?} */ ((this.map)).get(param) || null;
};
/**
* Get all the parameter names for this body.
*/
/**
* Get all the parameter names for this body.
* @return {?}
*/
HttpParams.prototype.keys = /**
* Get all the parameter names for this body.
* @return {?}
*/
function () {
this.init();
return Array.from(/** @type {?} */ ((this.map)).keys());
};
/**
* Construct a new body with an appended value for the given parameter name.
*/
/**
* Construct a new body with an appended value for the given parameter name.
* @param {?} param
* @param {?} value
* @return {?}
*/
HttpParams.prototype.append = /**
* Construct a new body with an appended value for the given parameter name.
* @param {?} param
* @param {?} value
* @return {?}
*/
function (param, value) { return this.clone({ param: param, value: value, op: 'a' }); };
/**
* Construct a new body with a new value for the given parameter name.
*/
/**
* Construct a new body with a new value for the given parameter name.
* @param {?} param
* @param {?} value
* @return {?}
*/
HttpParams.prototype.set = /**
* Construct a new body with a new value for the given parameter name.
* @param {?} param
* @param {?} value
* @return {?}
*/
function (param, value) { return this.clone({ param: param, value: value, op: 's' }); };
/**
* Construct a new body with either the given value for the given parameter
* removed, if a value is given, or all values for the given parameter removed
* if not.
*/
/**
* Construct a new body with either the given value for the given parameter
* removed, if a value is given, or all values for the given parameter removed
* if not.
* @param {?} param
* @param {?=} value
* @return {?}
*/
HttpParams.prototype.delete = /**
* Construct a new body with either the given value for the given parameter
* removed, if a value is given, or all values for the given parameter removed
* if not.
* @param {?} param
* @param {?=} value
* @return {?}
*/
function (param, value) { return this.clone({ param: param, value: value, op: 'd' }); };
/**
* Serialize the body to an encoded string, where key-value pairs (separated by `=`) are
* separated by `&`s.
*/
/**
* Serialize the body to an encoded string, where key-value pairs (separated by `=`) are
* separated by `&`s.
* @return {?}
*/
HttpParams.prototype.toString = /**
* Serialize the body to an encoded string, where key-value pairs (separated by `=`) are
* separated by `&`s.
* @return {?}
*/
function () {
var _this = this;
this.init();
return this.keys()
.map(function (key) {
var /** @type {?} */ eKey = _this.encoder.encodeKey(key);
return /** @type {?} */ ((/** @type {?} */ ((_this.map)).get(key))).map(function (value) { return eKey + '=' + _this.encoder.encodeValue(value); }).join('&');
})
.join('&');
};
/**
* @param {?} update
* @return {?}
*/
HttpParams.prototype.clone = /**
* @param {?} update
* @return {?}
*/
function (update) {
var /** @type {?} */ clone = new HttpParams({ encoder: this.encoder });
clone.cloneFrom = this.cloneFrom || this;
clone.updates = (this.updates || []).concat([update]);
return clone;
};
/**
* @return {?}
*/
HttpParams.prototype.init = /**
* @return {?}
*/
function () {
var _this = this;
if (this.map === null) {
this.map = new Map();
}
if (this.cloneFrom !== null) {
this.cloneFrom.init();
this.cloneFrom.keys().forEach(function (key) { return ((_this.map)).set(key, /** @type {?} */ ((/** @type {?} */ ((/** @type {?} */ ((_this.cloneFrom)).map)).get(key)))); }); /** @type {?} */
((this.updates)).forEach(function (update) {
switch (update.op) {
case 'a':
case 's':
var /** @type {?} */ base = (update.op === 'a' ? /** @type {?} */ ((_this.map)).get(update.param) : undefined) || [];
base.push(/** @type {?} */ ((update.value))); /** @type {?} */
((_this.map)).set(update.param, base);
break;
case 'd':
if (update.value !== undefined) {
var /** @type {?} */ base_1 = /** @type {?} */ ((_this.map)).get(update.param) || [];
var /** @type {?} */ idx = base_1.indexOf(update.value);
if (idx !== -1) {
base_1.splice(idx, 1);
}
if (base_1.length > 0) {
/** @type {?} */ ((_this.map)).set(update.param, base_1);
}
else {
/** @type {?} */ ((_this.map)).delete(update.param);
}
}
else {
/** @type {?} */ ((_this.map)).delete(update.param);
break;
}
}
});
this.cloneFrom = null;
}
};
return HttpParams;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @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
*/
/**
* Determine whether the given HTTP method may include a body.
* @param {?} method
* @return {?}
*/
function mightHaveBody(method) {
switch (method) {
case 'DELETE':
case 'GET':
case 'HEAD':
case 'OPTIONS':
case 'JSONP':
return false;
default:
return true;
}
}
/**
* Safely assert whether the given value is an ArrayBuffer.
*
* In some execution environments ArrayBuffer is not defined.
* @param {?} value
* @return {?}
*/
function isArrayBuffer(value) {
return typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer;
}
/**
* Safely assert whether the given value is a Blob.
*
* In some execution environments Blob is not defined.
* @param {?} value
* @return {?}
*/
function isBlob(value) {
return typeof Blob !== 'undefined' && value instanceof Blob;
}
/**
* Safely assert whether the given value is a FormData instance.
*
* In some execution environments FormData is not defined.
* @param {?} value
* @return {?}
*/
function isFormData(value) {
return typeof FormData !== 'undefined' && value instanceof FormData;
}
/**
* An outgoing HTTP request with an optional typed body.
*
* `HttpRequest` represents an outgoing request, including URL, method,
* headers, body, and other request configuration options. Instances should be
* assumed to be immutable. To modify a `HttpRequest`, the `clone`
* method should be used.
*
* \@stable
* @template T
*/
var HttpRequest = /** @class */ (function () {
function HttpRequest(method, url, third, fourth) {
this.url = url;
/**
* The request body, or `null` if one isn't set.
*
* Bodies are not enforced to be immutable, as they can include a reference to any
* user-defined data type. However, interceptors should take care to preserve
* idempotence by treating them as such.
*/
this.body = null;
/**
* Whether this request should be made in a way that exposes progress events.
*
* Progress events are expensive (change detection runs on each event) and so
* they should only be requested if the consumer intends to monitor them.
*/
this.reportProgress = false;
/**
* Whether this request should be sent with outgoing credentials (cookies).
*/
this.withCredentials = false;
/**
* The expected response type of the server.
*
* This is used to parse the response appropriately before returning it to
* the requestee.
*/
this.responseType = 'json';
this.method = method.toUpperCase();
// Next, need to figure out which argument holds the HttpRequestInit
// options, if any.
var /** @type {?} */ options;
// Check whether a body argument is expected. The only valid way to omit
// the body argument is to use a known no-body method like GET.
if (mightHaveBody(this.method) || !!fourth) {
// Body is the third argument, options are the fourth.
this.body = (third !== undefined) ? /** @type {?} */ (third) : null;
options = fourth;
}
else {
// No body required, options are the third argument. The body stays null.
options = /** @type {?} */ (third);
}
// If options have been passed, interpret them.
if (options) {
// Normalize reportProgress and withCredentials.
this.reportProgress = !!options.reportProgress;
this.withCredentials = !!options.withCredentials;
// Override default response type of 'json' if one is provided.
if (!!options.responseType) {
this.responseType = options.responseType;
}
// Override headers if they're provided.
if (!!options.headers) {
this.headers = options.headers;
}
if (!!options.params) {
this.params = options.params;
}
}
// If no headers have been passed in, construct a new HttpHeaders instance.
if (!this.headers) {
this.headers = new HttpHeaders();
}
// If no parameters have been passed in, construct a new HttpUrlEncodedParams instance.
if (!this.params) {
this.params = new HttpParams();
this.urlWithParams = url;
}
else {
// Encode the parameters to a string in preparation for inclusion in the URL.
var /** @type {?} */ params = this.params.toString();
if (params.length === 0) {
// No parameters, the visible URL is just the URL given at creation time.
this.urlWithParams = url;
}
else {
// Does the URL already have query parameters? Look for '?'.
var /** @type {?} */ qIdx = url.indexOf('?');
// There are 3 cases to handle:
// 1) No existing parameters -> append '?' followed by params.
// 2) '?' exists and is followed by existing query string ->
// append '&' followed by params.
// 3) '?' exists at the end of the url -> append params directly.
// This basically amounts to determining the character, if any, with
// which to join the URL and parameters.
var /** @type {?} */ sep = qIdx === -1 ? '?' : (qIdx < url.length - 1 ? '&' : '');
this.urlWithParams = url + sep + params;
}
}
}
/**
* Transform the free-form body into a serialized format suitable for
* transmission to the server.
*/
/**
* Transform the free-form body into a serialized format suitable for
* transmission to the server.
* @return {?}
*/
HttpRequest.prototype.serializeBody = /**
* Transform the free-form body into a serialized format suitable for
* transmission to the server.
* @return {?}
*/
function () {
// If no body is present, no need to serialize it.
if (this.body === null) {
return null;
}
// Check whether the body is already in a serialized form. If so,
// it can just be returned directly.
if (isArrayBuffer(this.body) || isBlob(this.body) || isFormData(this.body) ||
typeof this.body === 'string') {
return this.body;
}
// Check whether the body is an instance of HttpUrlEncodedParams.
if (this.body instanceof HttpParams) {
return this.body.toString();
}
// Check whether the body is an object or array, and serialize with JSON if so.
if (typeof this.body === 'object' || typeof this.body === 'boolean' ||
Array.isArray(this.body)) {
return JSON.stringify(this.body);
}
// Fall back on toString() for everything else.
return (/** @type {?} */ (this.body)).toString();
};
/**
* Examine the body and attempt to infer an appropriate MIME type
* for it.
*
* If no such type can be inferred, this method will return `null`.
*/
/**
* Examine the body and attempt to infer an appropriate MIME type
* for it.
*
* If no such type can be inferred, this method will return `null`.
* @return {?}
*/
HttpRequest.prototype.detectContentTypeHeader = /**
* Examine the body and attempt to infer an appropriate MIME type
* for it.
*
* If no such type can be inferred, this method will return `null`.
* @return {?}
*/
function () {
// An empty body has no content type.
if (this.body === null) {
return null;
}
// FormData bodies rely on the browser's content type assignment.
if (isFormData(this.body)) {
return null;
}
// Blobs usually have their own content type. If it doesn't, then
// no type can be inferred.
if (isBlob(this.body)) {
return this.body.type || null;
}
// Array buffers have unknown contents and thus no type can be inferred.
if (isArrayBuffer(this.body)) {
return null;
}
// Technically, strings could be a form of JSON data, but it's safe enough
// to assume they're plain strings.
if (typeof this.body === 'string') {
return 'text/plain';
}
// `HttpUrlEncodedParams` has its own content-type.
if (this.body instanceof HttpParams) {
return 'application/x-www-form-urlencoded;charset=UTF-8';
}
// Arrays, objects, and numbers will be encoded as JSON.
if (typeof this.body === 'object' || typeof this.body === 'number' ||
Array.isArray(this.body)) {
return 'application/json';
}
// No type could be inferred.
return null;
};
/**
* @param {?=} update
* @return {?}
*/
HttpRequest.prototype.clone = /**
* @param {?=} update
* @return {?}
*/
function (update) {
if (update === void 0) { update = {}; }
// For method, url, and responseType, take the current value unless
// it is overridden in the update hash.
var /** @type {?} */ method = update.method || this.method;
var /** @type {?} */ url = update.url || this.url;
var /** @type {?} */ responseType = update.responseType || this.responseType;
// The body is somewhat special - a `null` value in update.body means
// whatever current body is present is being overridden with an empty
// body, whereas an `undefined` value in update.body implies no
// override.
var /** @type {?} */ body = (update.body !== undefined) ? update.body : this.body;
// Carefully handle the boolean options to differentiate between
// `false` and `undefined` in the update args.
var /** @type {?} */ withCredentials = (update.withCredentials !== undefined) ? update.withCredentials : this.withCredentials;
var /** @type {?} */ reportProgress = (update.reportProgress !== undefined) ? update.reportProgress : this.reportProgress;
// Headers and params may be appended to if `setHeaders` or
// `setParams` are used.
var /** @type {?} */ headers = update.headers || this.headers;
var /** @type {?} */ params = update.params || this.params;
// Check whether the caller has asked to add headers.
if (update.setHeaders !== undefined) {
// Set every requested header.
headers =
Object.keys(update.setHeaders)
.reduce(function (headers, name) { return headers.set(name, /** @type {?} */ ((update.setHeaders))[name]); }, headers);
}
// Check whether the caller has asked to set params.
if (update.setParams) {
// Set every requested param.
params = Object.keys(update.setParams)
.reduce(function (params, param) { return params.set(param, /** @type {?} */ ((update.setParams))[param]); }, params);
}
// Finally, construct the new HttpRequest using the pieces from above.
return new HttpRequest(method, url, body, {
params: params, headers: headers, reportProgress: reportProgress, responseType: responseType, withCredentials: withCredentials,
});
};
return HttpRequest;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @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
*/
/** @enum {number} */
var HttpEventType = {
/**
* The request was sent out over the wire.
*/
Sent: 0,
/**
* An upload progress event was received.
*/
UploadProgress: 1,
/**
* The response status code and headers were received.
*/
ResponseHeader: 2,
/**
* A download progress event was received.
*/
DownloadProgress: 3,
/**
* The full response including the body was received.
*/
Response: 4,
/**
* A custom event from an interceptor or a backend.
*/
User: 5,
};
HttpEventType[HttpEventType.Sent] = "Sent";
HttpEventType[HttpEventType.UploadProgress] = "UploadProgress";
HttpEventType[HttpEventType.ResponseHeader] = "ResponseHeader";
HttpEventType[HttpEventType.DownloadProgress] = "DownloadProgress";
HttpEventType[HttpEventType.Response] = "Response";
HttpEventType[HttpEventType.User] = "User";
/**
* Base interface for progress events.
*
* \@stable
* @record
*/
/**
* A download progress event.
*
* \@stable
* @record
*/
/**
* An upload progress event.
*
* \@stable
* @record
*/
/**
* An event indicating that the request was sent to the server. Useful
* when a request may be retried multiple times, to distinguish between
* retries on the final event stream.
*
* \@stable
* @record
*/
/**
* A user-defined event.
*
* Grouping all custom events under this type ensures they will be handled
* and forwarded by all implementations of interceptors.
*
* \@stable
* @record
* @template T
*/
/**
* An error that represents a failed attempt to JSON.parse text coming back
* from the server.
*
* It bundles the Error object with the actual response body that failed to parse.
*
* \@stable
* @record
*/
/**
* Base class for both `HttpResponse` and `HttpHeaderResponse`.
*
* \@stable
* @abstract
*/
var HttpResponseBase = /** @class */ (function () {
/**
* Super-constructor for all responses.
*
* The single parameter accepted is an initialization hash. Any properties
* of the response passed there will override the default values.
*/
function HttpResponseBase(init, defaultStatus, defaultStatusText) {
if (defaultStatus === void 0) { defaultStatus = 200; }
if (defaultStatusText === void 0) { defaultStatusText = 'OK'; }
// If the hash has values passed, use them to initialize the response.
// Otherwise use the default values.
this.headers = init.headers || new HttpHeaders();
this.status = init.status !== undefined ? init.status : defaultStatus;
this.statusText = init.statusText || defaultStatusText;
this.url = init.url || null;
// Cache the ok value to avoid defining a getter.
this.ok = this.status >= 200 && this.status < 300;
}
return HttpResponseBase;
}());
/**
* A partial HTTP response which only includes the status and header data,
* but no response body.
*
* `HttpHeaderResponse` is a `HttpEvent` available on the response
* event stream, only when progress events are requested.
*
* \@stable
*/
var HttpHeaderResponse = /** @class */ (function (_super) {
__extends(HttpHeaderResponse, _super);
/**
* Create a new `HttpHeaderResponse` with the given parameters.
*/
function HttpHeaderResponse(init) {
if (init === void 0) { init = {}; }
var _this = _super.call(this, init) || this;
_this.type = HttpEventType.ResponseHeader;
return _this;
}
/**
* Copy this `HttpHeaderResponse`, overriding its contents with the
* given parameter hash.
*/
/**
* Copy this `HttpHeaderResponse`, overriding its contents with the
* given parameter hash.
* @param {?=} update
* @return {?}
*/
HttpHeaderResponse.prototype.clone = /**
* Copy this `HttpHeaderResponse`, overriding its contents with the
* given parameter hash.
* @param {?=} update
* @return {?}
*/
function (update) {
if (update === void 0) { update = {}; }
// Perform a straightforward initialization of the new HttpHeaderResponse,
// overriding the current parameters with new ones if given.
return new HttpHeaderResponse({
headers: update.headers || this.headers,
status: update.status !== undefined ? update.status : this.status,
statusText: update.statusText || this.statusText,
url: update.url || this.url || undefined,
});
};
return HttpHeaderResponse;
}(HttpResponseBase));
/**
* A full HTTP response, including a typed response body (which may be `null`
* if one was not returned).
*
* `HttpResponse` is a `HttpEvent` available on the response event
* stream.
*
* \@stable
* @template T
*/
var HttpResponse = /** @class */ (function (_super) {
__extends(HttpResponse, _super);
/**
* Construct a new `HttpResponse`.
*/
function HttpResponse(init) {
if (init === void 0) { init = {}; }
var _this = _super.call(this, init) || this;
_this.type = HttpEventType.Response;
_this.body = init.body !== undefined ? init.body : null;
return _this;
}
/**
* @param {?=} update
* @return {?}
*/
HttpResponse.prototype.clone = /**
* @param {?=} update
* @return {?}
*/
function (update) {
if (update === void 0) { update = {}; }
return new HttpResponse({
body: (update.body !== undefined) ? update.body : this.body,
headers: update.headers || this.headers,
status: (update.status !== undefined) ? update.status : this.status,
statusText: update.statusText || this.statusText,
url: update.url || this.url || undefined,
});
};
return HttpResponse;
}(HttpResponseBase));
/**
* A response that represents an error or failure, either from a
* non-successful HTTP status, an error while executing the request,
* or some other failure which occurred during the parsing of the response.
*
* Any error returned on the `Observable` response stream will be
* wrapped in an `HttpErrorResponse` to provide additional context about
* the state of the HTTP layer when the error occurred. The error property
* will contain either a wrapped Error object or the error response returned
* from the server.
*
* \@stable
*/
var HttpErrorResponse = /** @class */ (function (_super) {
__extends(HttpErrorResponse, _super);
function HttpErrorResponse(init) {
var _this =
// Initialize with a default status of 0 / Unknown Error.
_super.call(this, init, 0, 'Unknown Error') || this;
_this.name = 'HttpErrorResponse';
/**
* Errors are never okay, even when the status code is in the 2xx success range.
*/
_this.ok = false;
// If the response was successful, then this was a parse error. Otherwise, it was
// a protocol-level failure of some sort. Either the request failed in transit
// or the server returned an unsuccessful status code.
if (_this.status >= 200 && _this.status < 300) {
_this.message = "Http failure during parsing for " + (init.url || '(unknown url)');
}
else {
_this.message =
"Http failure response for " + (init.url || '(unknown url)') + ": " + init.status + " " + init.statusText;
}
_this.error = init.error || null;
return _this;
}
return HttpErrorResponse;
}(HttpResponseBase));
/**
* @fileoverview added by tsickle
* @suppress {checkTypes} checked by tsc
*/
/**
* @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
*/
/**
* Construct an instance of `HttpRequestOptions<T>` from a source `HttpMethodOptions` and
* the given `body`. Basically, this clones the object and adds the body.
* @template T
* @param {?} options
* @param {?} body
* @return {?}
*/
function addBody(options, body) {
return {
body: body,
headers: options.headers,
observe: options.observe,
params: options.params,
reportProgress: options.reportProgress,
responseType: options.responseType,
withCredentials: options.withCredentials,
};
}
/**
* Perform HTTP requests.
*
* `HttpClient` is available as an injectable class, with methods to perform HTTP requests.
* Each request method has multiple signatures, and the return type varies according to which
* signature is called (mainly the values of `observe` and `responseType`).
*
* \@stable
*/
var HttpClient = /** @class */ (function () {
function HttpClient(handler) {
this.handler = handler;
}
/**
* Constructs an `Observable` for a particular HTTP request that, when subscribed,
* fires the request through the chain of registered interceptors and on to the
* server.
*
* This method can be called in one of two ways. Either an `HttpRequest`
* instance can be passed directly as the only parameter, or a method can be
* passed as the first parameter, a string URL as the second, and an
* options hash as the third.
*
* If a `HttpRequest` object is passed directly, an `Observable` of the
* raw `HttpEvent` stream will be returned.
*
* If a request is instead built by providing a URL, the options object
* determines the return type of `request()`. In addition to configuring
* request parameters such as the outgoing headers and/or the body, the options
* hash specifies two key pieces of information about the request: the
* `responseType` and what to `observe`.
*
* The `responseType` value determines how a successful response body will be
* parsed. If `responseType` is the default `json`, a type interface for the
* resulting object may be passed as a type parameter to `request()`.
*
* The `observe` value determines the return type of `request()`, based on what
* the consumer is interested in observing. A value of `events` will return an
* `Observable<HttpEvent>` representing the raw `HttpEvent` stream,
* including progress events by default. A value of `response` will return an
* `Observable<HttpResponse<T>>` where the `T` parameter of `HttpResponse`
* depends on the `responseType` and any optionally provided type parameter.
* A value of `body` will return an `Observable<T>` with the same `T` body type.
*/
/**
* Constructs an `Observable` for a particular HTTP request that, when subscribed,
* fires the request through the chain of registered interceptors and on to the
* server.
*
* This method can be called in one of two ways. Either an `HttpRequest`
* instance can be passed directly as the only parameter, or a method can be
* passed as the first parameter, a string URL as the second, and an
* options hash as the third.
*
* If a `HttpRequest` object is passed directly, an `Observable` of the
* raw `HttpEvent` stream will be returned.
*
* If a request is instead built by providing a URL, the options object
* determines the return type of `request()`. In addition to configuring
* request parameters such as the outgoing headers and/or the body, the options
* hash specifies two key pieces of information about the request: the
* `responseType` and what to `observe`.
*
* The `responseType` value determines how a successful response body will be
* parsed. If `responseType` is the default `json`, a type interface for the
* resulting object may be passed as a type parameter to `request()`.
*
* The `observe` value determines the return type of `request()`, based on what
* the consumer is interested in observing. A value of `events` will return an
* `Observable<HttpEvent>` representing the raw `HttpEvent` stream,
* including progress events by default. A value of `response` will return an
* `Observable<HttpResponse<T>>` where the `T` parameter of `HttpResponse`
* depends on the `responseType` and any optionally provided type parameter.
* A value of `body` will return an `Observable<T>` with the same `T` body type.
* @param {?} first
* @param {?=} url
* @param {?=} options
* @return {?}
*/
HttpClient.prototype.request = /**
* Constructs an `Observable` for a particular HTTP request that, when subscribed,
* fires the request through the chain of registered interceptors and on to the
* server.
*
* This method can be called in one of two ways. Either an `HttpRequest`
* instance can be passed directly as the only parameter, or a method can be
* passed as the first parameter, a string URL as the second, and an
* options hash as the third.
*
* If a `HttpRequest` object is passed directly, an `Observable` of the
* raw `HttpEvent` stream will be returned.
*
* If a request is instead built by providing a URL, the options object
* determines the return type of `request()`. In addition to configuring
* request parameters such as the outgoing headers and/or the body, the options
* hash specifies two key pieces of information about the request: the
* `responseType` and what to `observe`.
*
* The `responseType` value determines how a successful response body will be
* parsed. If `responseType` is the default `json`, a type interface for the
* resulting object may be passed as a type parameter to `request()`.
*
* The `observe` value determines the return type of `request()`, based on what
* the consumer is interested in observing. A value of `events` will return an
* `Observable<HttpEvent>` representing the raw `HttpEvent` stream,
* including progress events by default. A value of `response` will return an
* `Observable<HttpResponse<T>>` where the `T` parameter of `HttpResponse`
* depends on the `responseType` and any optionally provided type parameter.
* A value of `body` will return an `Observable<T>` with the same `T` body type.
* @param {?} first
* @param {?=} url
* @param {?=} options
* @return {?}
*/
function (first, url, options) {
var _this = this;
if (options === void 0) { options = {}; }
var /** @type {?} */ req;
// Firstly, check whether the primary argument is an instance of `HttpRequest`.
if (first instanceof HttpRequest) {
// It is. The other arguments must be undefined (per the signatures) and can be
// ignored.
req = /** @type {?} */ (first);
}
else {
// It's a string, so it represents a URL. Construct a request based on it,
// and incorporate the remaining arguments (assuming GET unless a method is
// provided.
// Figure out the headers.
var /** @type {?} */ headers = undefined;
if (options.headers instanceof HttpHeaders) {
headers = options.headers;
}
else {
headers = new HttpHeaders(options.headers);
}
// Sort out parameters.
var /** @type {?} */ params = undefined;
if (!!options.params) {
if (options.params instanceof HttpParams) {
p