/** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} 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 */ import { Injectable } from '@angular/core'; import { RequestOptions } from './base_request_options'; import { RequestMethod } from './enums'; import { ConnectionBackend } from './interfaces'; import { Request } from './static_request'; /** * @param {?} backend * @param {?} request * @return {?} */ function httpRequest(backend, request) { return backend.createConnection(request).response; } /** * @param {?} defaultOpts * @param {?} providedOpts * @param {?} method * @param {?} url * @return {?} */ function mergeOptions(defaultOpts, providedOpts, method, url) { /** @type {?} */ const newOptions = defaultOpts; if (providedOpts) { // Hack so Dart can used named parameters return (/** @type {?} */ (newOptions.merge(new RequestOptions({ method: providedOpts.method || method, url: providedOpts.url || url, search: providedOpts.search, params: providedOpts.params, headers: providedOpts.headers, body: providedOpts.body, withCredentials: providedOpts.withCredentials, responseType: providedOpts.responseType })))); } return (/** @type {?} */ (newOptions.merge(new RequestOptions({ method, url })))); } /** * Performs http requests using `XMLHttpRequest` as the default backend. * * `Http` is available as an injectable class, with methods to perform http requests. Calling * `request` returns an `Observable` which will emit a single {\@link Response} when a * response is received. * * \@usageNotes * ### Example * * ```typescript * import {Http, HTTP_PROVIDERS} from '\@angular/http'; * import {map} from 'rxjs/operators'; * * \@Component({ * selector: 'http-app', * viewProviders: [HTTP_PROVIDERS], * templateUrl: 'people.html' * }) * class PeopleComponent { * constructor(http: Http) { * http.get('people.json') * // Call map on the response observable to get the parsed people object * .pipe(map(res => res.json())) * // Subscribe to the observable to get the parsed people object and attach it to the * // component * .subscribe(people => this.people = people); * } * } * ``` * * * ### Example * * ``` * http.get('people.json').subscribe((res:Response) => this.people = res.json()); * ``` * * The default construct used to perform requests, `XMLHttpRequest`, is abstracted as a "Backend" ( * {\@link XHRBackend} in this case), which could be mocked with dependency injection by replacing * the {\@link XHRBackend} provider, as in the following example: * * ### Example * * ```typescript * import {BaseRequestOptions, Http} from '\@angular/http'; * import {MockBackend} from '\@angular/http/testing'; * var injector = Injector.resolveAndCreate([ * BaseRequestOptions, * MockBackend, * {provide: Http, useFactory: * function(backend, defaultOptions) { * return new Http(backend, defaultOptions); * }, * deps: [MockBackend, BaseRequestOptions]} * ]); * var http = injector.get(Http); * http.get('request-from-mock-backend.json').subscribe((res:Response) => doSomething(res)); * ``` * * @deprecated see https://angular.io/guide/http * \@publicApi */ export class Http { /** * @param {?} _backend * @param {?} _defaultOptions */ constructor(_backend, _defaultOptions) { this._backend = _backend; this._defaultOptions = _defaultOptions; } /** * Performs any type of http request. First argument is required, and can either be a url or * a {\@link Request} instance. If the first argument is a url, an optional {\@link RequestOptions} * object can be provided as the 2nd argument. The options object will be merged with the values * of {\@link BaseRequestOptions} before performing the request. * @param {?} url * @param {?=} options * @return {?} */ request(url, options) { /** @type {?} */ let responseObservable; if (typeof url === 'string') { responseObservable = httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, (/** @type {?} */ (url))))); } else if (url instanceof Request) { responseObservable = httpRequest(this._backend, url); } else { throw new Error('First argument must be a url string or Request instance.'); } return responseObservable; } /** * Performs a request with `get` http method. * @param {?} url * @param {?=} options * @return {?} */ get(url, options) { return this.request(new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, url))); } /** * Performs a request with `post` http method. * @param {?} url * @param {?} body * @param {?=} options * @return {?} */ post(url, body, options) { return this.request(new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({ body: body })), options, RequestMethod.Post, url))); } /** * Performs a request with `put` http method. * @param {?} url * @param {?} body * @param {?=} options * @return {?} */ put(url, body, options) { return this.request(new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({ body: body })), options, RequestMethod.Put, url))); } /** * Performs a request with `delete` http method. * @param {?} url * @param {?=} options * @return {?} */ delete(url, options) { return this.request(new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Delete, url))); } /** * Performs a request with `patch` http method. * @param {?} url * @param {?} body * @param {?=} options * @return {?} */ patch(url, body, options) { return this.request(new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({ body: body })), options, RequestMethod.Patch, url))); } /** * Performs a request with `head` http method. * @param {?} url * @param {?=} options * @return {?} */ head(url, options) { return this.request(new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Head, url))); } /** * Performs a request with `options` http method. * @param {?} url * @param {?=} options * @return {?} */ options(url, options) { return this.request(new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Options, url))); } } Http.decorators = [ { type: Injectable } ]; /** @nocollapse */ Http.ctorParameters = () => [ { type: ConnectionBackend }, { type: RequestOptions } ]; if (false) { /** * @type {?} * @protected */ Http.prototype._backend; /** * @type {?} * @protected */ Http.prototype._defaultOptions; } /** * @deprecated see https://angular.io/guide/http * \@publicApi */ export class Jsonp extends Http { /** * @param {?} backend * @param {?} defaultOptions */ constructor(backend, defaultOptions) { super(backend, defaultOptions); } /** * Performs any type of http request. First argument is required, and can either be a url or * a {\@link Request} instance. If the first argument is a url, an optional {\@link RequestOptions} * object can be provided as the 2nd argument. The options object will be merged with the values * of {\@link BaseRequestOptions} before performing the request. * * \@security Regular XHR is the safest alternative to JSONP for most applications, and is * supported by all current browsers. Because JSONP creates a `