2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2015-07-28 16:10:25 -04:00
|
|
|
/**
|
|
|
|
* @module
|
|
|
|
* @description
|
|
|
|
* The http module provides services to perform http requests. To get started, see the {@link Http}
|
|
|
|
* class.
|
|
|
|
*/
|
2016-04-28 20:50:03 -04:00
|
|
|
import {provide} from '@angular/core';
|
2016-06-08 19:38:52 -04:00
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
import {BrowserJsonp} from './src/backends/browser_jsonp';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {BrowserXhr} from './src/backends/browser_xhr';
|
|
|
|
import {JSONPBackend, JSONPBackend_, JSONPConnection} from './src/backends/jsonp_backend';
|
|
|
|
import {CookieXSRFStrategy, XHRBackend, XHRConnection} from './src/backends/xhr_backend';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {BaseRequestOptions, RequestOptions} from './src/base_request_options';
|
|
|
|
import {BaseResponseOptions, ResponseOptions} from './src/base_response_options';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {Http, Jsonp} from './src/http';
|
|
|
|
import {ConnectionBackend, XSRFStrategy} from './src/interfaces';
|
2015-07-28 16:10:25 -04:00
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
export {BrowserXhr} from './src/backends/browser_xhr';
|
2016-06-08 19:38:52 -04:00
|
|
|
export {JSONPBackend, JSONPConnection} from './src/backends/jsonp_backend';
|
|
|
|
export {CookieXSRFStrategy, XHRBackend, XHRConnection} from './src/backends/xhr_backend';
|
2016-04-28 20:50:03 -04:00
|
|
|
export {BaseRequestOptions, RequestOptions} from './src/base_request_options';
|
|
|
|
export {BaseResponseOptions, ResponseOptions} from './src/base_response_options';
|
2016-02-24 10:37:18 -05:00
|
|
|
export {ReadyState, RequestMethod, ResponseBuffer, ResponseType} from './src/enums';
|
2016-04-28 20:50:03 -04:00
|
|
|
export {Headers} from './src/headers';
|
2016-06-08 19:38:52 -04:00
|
|
|
export {Http, Jsonp} from './src/http';
|
|
|
|
export {Connection, ConnectionBackend, RequestOptionsArgs, ResponseOptionsArgs, XSRFStrategy} from './src/interfaces';
|
|
|
|
export {Request} from './src/static_request';
|
|
|
|
export {Response} from './src/static_response';
|
2016-06-28 14:31:35 -04:00
|
|
|
export {QueryEncoder, URLSearchParams} from './src/url_search_params';
|
|
|
|
|
2015-07-28 16:10:25 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
|
2015-07-28 16:10:25 -04:00
|
|
|
/**
|
|
|
|
* Provides a basic set of injectables to use the {@link Http} service in any application.
|
|
|
|
*
|
2015-10-11 01:11:13 -04:00
|
|
|
* The `HTTP_PROVIDERS` should be included either in a component's injector,
|
2015-09-17 13:33:16 -04:00
|
|
|
* or in the root injector when bootstrapping an application.
|
|
|
|
*
|
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/snj7Nv?p=preview))
|
2015-07-28 16:10:25 -04:00
|
|
|
*
|
|
|
|
* ```
|
2016-04-28 20:50:03 -04:00
|
|
|
* import {Component} from '@angular/core';
|
|
|
|
* import {bootstrap} from '@angular/platform-browser/browser';
|
|
|
|
* import {NgFor} from '@angular/common';
|
|
|
|
* import {HTTP_PROVIDERS, Http} from '@angular/http';
|
2015-09-17 13:33:16 -04:00
|
|
|
*
|
|
|
|
* @Component({
|
|
|
|
* selector: 'app',
|
2015-10-11 10:41:19 -04:00
|
|
|
* providers: [HTTP_PROVIDERS],
|
2015-09-17 13:33:16 -04:00
|
|
|
* template: `
|
|
|
|
* <div>
|
|
|
|
* <h1>People</h1>
|
|
|
|
* <ul>
|
2016-04-25 22:52:24 -04:00
|
|
|
* <li *ngFor="let person of people">
|
2015-09-17 13:33:16 -04:00
|
|
|
* {{person.name}}
|
|
|
|
* </li>
|
|
|
|
* </ul>
|
|
|
|
* </div>
|
|
|
|
* `,
|
|
|
|
* directives: [NgFor]
|
|
|
|
* })
|
|
|
|
* export class App {
|
|
|
|
* people: Object[];
|
2015-07-28 16:10:25 -04:00
|
|
|
* constructor(http:Http) {
|
2015-10-07 06:41:27 -04:00
|
|
|
* http.get('people.json').subscribe(res => {
|
2015-09-17 13:33:16 -04:00
|
|
|
* this.people = res.json();
|
|
|
|
* });
|
|
|
|
* }
|
|
|
|
* active:boolean = false;
|
|
|
|
* toggleActiveState() {
|
|
|
|
* this.active = !this.active;
|
2015-07-28 16:10:25 -04:00
|
|
|
* }
|
|
|
|
* }
|
2015-09-17 13:33:16 -04:00
|
|
|
*
|
|
|
|
* bootstrap(App)
|
|
|
|
* .catch(err => console.error(err));
|
|
|
|
* ```
|
|
|
|
*
|
2015-10-11 01:11:13 -04:00
|
|
|
* The primary public API included in `HTTP_PROVIDERS` is the {@link Http} class.
|
|
|
|
* However, other providers required by `Http` are included,
|
2015-09-17 13:33:16 -04:00
|
|
|
* which may be beneficial to override in certain cases.
|
|
|
|
*
|
2015-10-11 01:11:13 -04:00
|
|
|
* The providers included in `HTTP_PROVIDERS` include:
|
2015-09-17 13:33:16 -04:00
|
|
|
* * {@link Http}
|
|
|
|
* * {@link XHRBackend}
|
2016-05-27 23:15:40 -04:00
|
|
|
* * {@link XSRFStrategy} - Bound to {@link CookieXSRFStrategy} class (see below)
|
2015-09-17 13:33:16 -04:00
|
|
|
* * `BrowserXHR` - Private factory to create `XMLHttpRequest` instances
|
|
|
|
* * {@link RequestOptions} - Bound to {@link BaseRequestOptions} class
|
|
|
|
* * {@link ResponseOptions} - Bound to {@link BaseResponseOptions} class
|
|
|
|
*
|
|
|
|
* There may be cases where it makes sense to extend the base request options,
|
|
|
|
* such as to add a search string to be appended to all URLs.
|
2015-10-11 01:11:13 -04:00
|
|
|
* To accomplish this, a new provider for {@link RequestOptions} should
|
|
|
|
* be added in the same injector as `HTTP_PROVIDERS`.
|
2015-09-17 13:33:16 -04:00
|
|
|
*
|
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/aCMEXi?p=preview))
|
|
|
|
*
|
2015-07-28 16:10:25 -04:00
|
|
|
* ```
|
2016-04-28 20:50:03 -04:00
|
|
|
* import {provide} from '@angular/core';
|
|
|
|
* import {bootstrap} from '@angular/platform-browser/browser';
|
|
|
|
* import {HTTP_PROVIDERS, BaseRequestOptions, RequestOptions} from '@angular/http';
|
2015-09-17 13:33:16 -04:00
|
|
|
*
|
|
|
|
* class MyOptions extends BaseRequestOptions {
|
|
|
|
* search: string = 'coreTeam=true';
|
|
|
|
* }
|
2015-07-28 16:10:25 -04:00
|
|
|
*
|
2016-06-02 20:30:40 -04:00
|
|
|
* bootstrap(App, [HTTP_PROVIDERS, {provide: RequestOptions, useClass: MyOptions}])
|
2015-09-17 13:33:16 -04:00
|
|
|
* .catch(err => console.error(err));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Likewise, to use a mock backend for unit tests, the {@link XHRBackend}
|
2015-10-11 01:11:13 -04:00
|
|
|
* provider should be bound to {@link MockBackend}.
|
2015-09-17 13:33:16 -04:00
|
|
|
*
|
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/7LWALD?p=preview))
|
|
|
|
*
|
|
|
|
* ```
|
2016-04-28 20:50:03 -04:00
|
|
|
* import {provide} from '@angular/core';
|
|
|
|
* import {bootstrap} from '@angular/platform-browser/browser';
|
|
|
|
* import {HTTP_PROVIDERS, Http, Response, XHRBackend} from '@angular/http';
|
|
|
|
* import {MockBackend} from '@angular/http/testing';
|
2015-09-17 13:33:16 -04:00
|
|
|
*
|
|
|
|
* var people = [{name: 'Jeff'}, {name: 'Tobias'}];
|
|
|
|
*
|
|
|
|
* var injector = Injector.resolveAndCreate([
|
2015-10-11 01:11:13 -04:00
|
|
|
* HTTP_PROVIDERS,
|
2015-09-17 13:33:16 -04:00
|
|
|
* MockBackend,
|
2016-06-02 20:30:40 -04:00
|
|
|
* {provide: XHRBackend, useExisting: MockBackend}
|
2015-09-17 13:33:16 -04:00
|
|
|
* ]);
|
|
|
|
* var http = injector.get(Http);
|
|
|
|
* var backend = injector.get(MockBackend);
|
|
|
|
*
|
|
|
|
* // Listen for any new requests
|
|
|
|
* backend.connections.observer({
|
|
|
|
* next: connection => {
|
|
|
|
* var response = new Response({body: people});
|
|
|
|
* setTimeout(() => {
|
|
|
|
* // Send a response to the request
|
|
|
|
* connection.mockRespond(response);
|
|
|
|
* });
|
2016-01-19 22:28:55 -05:00
|
|
|
* }
|
|
|
|
* });
|
2015-09-17 13:33:16 -04:00
|
|
|
*
|
|
|
|
* http.get('people.json').observer({
|
|
|
|
* next: res => {
|
|
|
|
* // Response came from mock backend
|
|
|
|
* console.log('first person', res.json()[0].name);
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
* ```
|
2016-05-27 23:15:40 -04:00
|
|
|
*
|
|
|
|
* `XSRFStrategy` allows customizing how the application protects itself against Cross Site Request
|
|
|
|
* Forgery (XSRF) attacks. By default, Angular will look for a cookie called `'XSRF-TOKEN'`, and set
|
|
|
|
* an HTTP request header called `'X-XSRF-TOKEN'` with the value of the cookie on each request,
|
|
|
|
* allowing the server side to validate that the request comes from its own front end.
|
|
|
|
*
|
|
|
|
* Applications can override the names used by configuring a different `XSRFStrategy` instance. Most
|
|
|
|
* commonly, applications will configure a `CookieXSRFStrategy` with different cookie or header
|
|
|
|
* names, but if needed, they can supply a completely custom implementation.
|
|
|
|
*
|
|
|
|
* See the security documentation for more information.
|
|
|
|
*
|
|
|
|
* ### Example
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* import {provide} from '@angular/core';
|
|
|
|
* import {bootstrap} from '@angular/platform-browser/browser';
|
|
|
|
* import {HTTP_PROVIDERS, XSRFStrategy, CookieXSRFStrategy} from '@angular/http';
|
|
|
|
*
|
|
|
|
* bootstrap(
|
|
|
|
* App,
|
2016-06-02 20:30:40 -04:00
|
|
|
* [HTTP_PROVIDERS, {provide: XSRFStrategy,
|
|
|
|
* useValue: new CookieXSRFStrategy('MY-XSRF-COOKIE-NAME', 'X-MY-XSRF-HEADER-NAME')}])
|
2016-05-27 23:15:40 -04:00
|
|
|
* .catch(err => console.error(err));
|
|
|
|
* ```
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
|
|
|
* @experimental
|
2015-07-28 16:10:25 -04:00
|
|
|
*/
|
2015-10-11 01:11:13 -04:00
|
|
|
export const HTTP_PROVIDERS: any[] = [
|
2015-07-30 10:29:16 -04:00
|
|
|
// TODO(pascal): use factory type annotations once supported in DI
|
|
|
|
// issue: https://github.com/angular/angular/issues/3183
|
2016-06-08 19:38:52 -04:00
|
|
|
{provide: Http, useFactory: httpFactory, deps: [XHRBackend, RequestOptions]},
|
2016-04-12 12:40:37 -04:00
|
|
|
BrowserXhr,
|
2016-06-02 20:30:40 -04:00
|
|
|
{provide: RequestOptions, useClass: BaseRequestOptions},
|
|
|
|
{provide: ResponseOptions, useClass: BaseResponseOptions},
|
2016-05-27 23:15:40 -04:00
|
|
|
XHRBackend,
|
2016-06-02 20:30:40 -04:00
|
|
|
{provide: XSRFStrategy, useValue: new CookieXSRFStrategy()},
|
2015-07-28 16:10:25 -04:00
|
|
|
];
|
|
|
|
|
2016-06-27 15:27:23 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @experimental
|
|
|
|
*/
|
2016-06-01 17:58:11 -04:00
|
|
|
export function httpFactory(xhrBackend: XHRBackend, requestOptions: RequestOptions): Http {
|
2016-06-02 20:30:40 -04:00
|
|
|
return new Http(xhrBackend, requestOptions);
|
|
|
|
}
|
|
|
|
|
2015-10-11 01:11:13 -04:00
|
|
|
/**
|
2015-12-03 18:49:09 -05:00
|
|
|
* See {@link HTTP_PROVIDERS} instead.
|
|
|
|
*
|
2015-10-11 01:11:13 -04:00
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
export const HTTP_BINDINGS = HTTP_PROVIDERS;
|
2015-09-17 18:00:30 -04:00
|
|
|
|
|
|
|
/**
|
2015-10-11 01:11:13 -04:00
|
|
|
* Provides a basic set of providers to use the {@link Jsonp} service in any application.
|
2015-09-17 18:00:30 -04:00
|
|
|
*
|
2015-10-11 01:11:13 -04:00
|
|
|
* The `JSONP_PROVIDERS` should be included either in a component's injector,
|
2015-09-17 18:00:30 -04:00
|
|
|
* or in the root injector when bootstrapping an application.
|
|
|
|
*
|
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/vmeN4F?p=preview))
|
|
|
|
*
|
|
|
|
* ```
|
2016-04-28 20:50:03 -04:00
|
|
|
* import {Component} from '@angular/core';
|
|
|
|
* import {NgFor} from '@angular/common';
|
|
|
|
* import {JSONP_PROVIDERS, Jsonp} from '@angular/http';
|
2015-09-17 18:00:30 -04:00
|
|
|
*
|
|
|
|
* @Component({
|
|
|
|
* selector: 'app',
|
2015-10-11 10:41:19 -04:00
|
|
|
* providers: [JSONP_PROVIDERS],
|
2015-09-17 18:00:30 -04:00
|
|
|
* template: `
|
|
|
|
* <div>
|
|
|
|
* <h1>People</h1>
|
|
|
|
* <ul>
|
2016-04-25 22:52:24 -04:00
|
|
|
* <li *ngFor="let person of people">
|
2015-09-17 18:00:30 -04:00
|
|
|
* {{person.name}}
|
|
|
|
* </li>
|
|
|
|
* </ul>
|
|
|
|
* </div>
|
|
|
|
* `,
|
|
|
|
* directives: [NgFor]
|
|
|
|
* })
|
|
|
|
* export class App {
|
|
|
|
* people: Array<Object>;
|
|
|
|
* constructor(jsonp:Jsonp) {
|
2015-10-07 06:41:27 -04:00
|
|
|
* jsonp.request('people.json').subscribe(res => {
|
2015-09-17 18:00:30 -04:00
|
|
|
* this.people = res.json();
|
|
|
|
* })
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
2015-10-11 01:11:13 -04:00
|
|
|
* The primary public API included in `JSONP_PROVIDERS` is the {@link Jsonp} class.
|
|
|
|
* However, other providers required by `Jsonp` are included,
|
2015-09-17 18:00:30 -04:00
|
|
|
* which may be beneficial to override in certain cases.
|
|
|
|
*
|
2015-10-11 01:11:13 -04:00
|
|
|
* The providers included in `JSONP_PROVIDERS` include:
|
2015-09-17 18:00:30 -04:00
|
|
|
* * {@link Jsonp}
|
|
|
|
* * {@link JSONPBackend}
|
|
|
|
* * `BrowserJsonp` - Private factory
|
|
|
|
* * {@link RequestOptions} - Bound to {@link BaseRequestOptions} class
|
|
|
|
* * {@link ResponseOptions} - Bound to {@link BaseResponseOptions} class
|
|
|
|
*
|
|
|
|
* There may be cases where it makes sense to extend the base request options,
|
|
|
|
* such as to add a search string to be appended to all URLs.
|
2015-10-11 01:11:13 -04:00
|
|
|
* To accomplish this, a new provider for {@link RequestOptions} should
|
|
|
|
* be added in the same injector as `JSONP_PROVIDERS`.
|
2015-09-17 18:00:30 -04:00
|
|
|
*
|
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/TFug7x?p=preview))
|
|
|
|
*
|
|
|
|
* ```
|
2016-04-28 20:50:03 -04:00
|
|
|
* import {provide} from '@angular/core';
|
|
|
|
* import {bootstrap} from '@angular/platform-browser/browser';
|
|
|
|
* import {JSONP_PROVIDERS, BaseRequestOptions, RequestOptions} from '@angular/http';
|
2015-09-17 18:00:30 -04:00
|
|
|
*
|
|
|
|
* class MyOptions extends BaseRequestOptions {
|
|
|
|
* search: string = 'coreTeam=true';
|
|
|
|
* }
|
|
|
|
*
|
2016-06-02 20:30:40 -04:00
|
|
|
* bootstrap(App, [JSONP_PROVIDERS, {provide: RequestOptions, useClass: MyOptions}])
|
2015-09-17 18:00:30 -04:00
|
|
|
* .catch(err => console.error(err));
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* Likewise, to use a mock backend for unit tests, the {@link JSONPBackend}
|
2015-10-11 01:11:13 -04:00
|
|
|
* provider should be bound to {@link MockBackend}.
|
2015-09-17 18:00:30 -04:00
|
|
|
*
|
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/HDqZWL?p=preview))
|
|
|
|
*
|
|
|
|
* ```
|
2016-04-28 20:50:03 -04:00
|
|
|
* import {provide, Injector} from '@angular/core';
|
|
|
|
* import {JSONP_PROVIDERS, Jsonp, Response, JSONPBackend} from '@angular/http';
|
|
|
|
* import {MockBackend} from '@angular/http/testing';
|
2015-09-17 18:00:30 -04:00
|
|
|
*
|
|
|
|
* var people = [{name: 'Jeff'}, {name: 'Tobias'}];
|
|
|
|
* var injector = Injector.resolveAndCreate([
|
2015-10-11 01:11:13 -04:00
|
|
|
* JSONP_PROVIDERS,
|
2015-09-17 18:00:30 -04:00
|
|
|
* MockBackend,
|
2016-06-02 20:30:40 -04:00
|
|
|
* {provide: JSONPBackend, useExisting: MockBackend}
|
2015-09-17 18:00:30 -04:00
|
|
|
* ]);
|
|
|
|
* var jsonp = injector.get(Jsonp);
|
|
|
|
* var backend = injector.get(MockBackend);
|
|
|
|
*
|
|
|
|
* // Listen for any new requests
|
|
|
|
* backend.connections.observer({
|
|
|
|
* next: connection => {
|
|
|
|
* var response = new Response({body: people});
|
|
|
|
* setTimeout(() => {
|
|
|
|
* // Send a response to the request
|
|
|
|
* connection.mockRespond(response);
|
|
|
|
* });
|
2016-01-19 22:28:55 -05:00
|
|
|
* }
|
|
|
|
* });
|
2015-09-17 18:00:30 -04:00
|
|
|
|
|
|
|
* jsonp.get('people.json').observer({
|
|
|
|
* next: res => {
|
|
|
|
* // Response came from mock backend
|
|
|
|
* console.log('first person', res.json()[0].name);
|
|
|
|
* }
|
|
|
|
* });
|
|
|
|
* ```
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
|
|
|
* @experimental
|
2015-09-17 18:00:30 -04:00
|
|
|
*/
|
2015-10-11 01:11:13 -04:00
|
|
|
export const JSONP_PROVIDERS: any[] = [
|
2015-07-30 10:29:16 -04:00
|
|
|
// TODO(pascal): use factory type annotations once supported in DI
|
|
|
|
// issue: https://github.com/angular/angular/issues/3183
|
2016-06-02 20:30:40 -04:00
|
|
|
{provide: Jsonp, useFactory: jsonpFactory, deps: [JSONPBackend, RequestOptions]},
|
2016-04-12 12:40:37 -04:00
|
|
|
BrowserJsonp,
|
2016-06-02 20:30:40 -04:00
|
|
|
{provide: RequestOptions, useClass: BaseRequestOptions},
|
|
|
|
{provide: ResponseOptions, useClass: BaseResponseOptions},
|
|
|
|
{provide: JSONPBackend, useClass: JSONPBackend_},
|
2015-07-28 16:10:25 -04:00
|
|
|
];
|
2015-10-11 01:11:13 -04:00
|
|
|
|
2016-06-02 20:30:40 -04:00
|
|
|
function jsonpFactory(jsonpBackend: JSONPBackend, requestOptions: RequestOptions) {
|
|
|
|
return new Jsonp(jsonpBackend, requestOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-11 01:11:13 -04:00
|
|
|
/**
|
2015-12-03 18:49:09 -05:00
|
|
|
* See {@link JSONP_PROVIDERS} instead.
|
|
|
|
*
|
2015-10-11 01:11:13 -04:00
|
|
|
* @deprecated
|
|
|
|
*/
|
2015-10-10 08:58:46 -04:00
|
|
|
export const JSON_BINDINGS = JSONP_PROVIDERS;
|