/** * @module * @description * The http module provides services to perform http requests. To get started, see the {@link Http} * class. */ import {provide, Provider} from 'angular2/core'; import {Http, Jsonp} from './src/http/http'; import {XHRBackend, XHRConnection} from './src/http/backends/xhr_backend'; import {JSONPBackend, JSONPBackend_, JSONPConnection} from './src/http/backends/jsonp_backend'; import {BrowserXhr} from './src/http/backends/browser_xhr'; import {BrowserJsonp} from './src/http/backends/browser_jsonp'; import {BaseRequestOptions, RequestOptions} from './src/http/base_request_options'; import {ConnectionBackend} from './src/http/interfaces'; import {BaseResponseOptions, ResponseOptions} from './src/http/base_response_options'; export {Request} from './src/http/static_request'; export {Response} from './src/http/static_response'; export { RequestOptionsArgs, ResponseOptionsArgs, Connection, ConnectionBackend } from './src/http/interfaces'; export {BrowserXhr} from './src/http/backends/browser_xhr'; export {BaseRequestOptions, RequestOptions} from './src/http/base_request_options'; export {BaseResponseOptions, ResponseOptions} from './src/http/base_response_options'; export {XHRBackend, XHRConnection} from './src/http/backends/xhr_backend'; export {JSONPBackend, JSONPConnection} from './src/http/backends/jsonp_backend'; export {Http, Jsonp} from './src/http/http'; export {Headers} from './src/http/headers'; export {ResponseType, ReadyState, RequestMethod} from './src/http/enums'; export {URLSearchParams} from './src/http/url_search_params'; /** * Provides a basic set of injectables to use the {@link Http} service in any application. * * The `HTTP_PROVIDERS` should be included either in a component's injector, * or in the root injector when bootstrapping an application. * * ### Example ([live demo](http://plnkr.co/edit/snj7Nv?p=preview)) * * ``` * import {bootstrap, Component, NgFor, View} from 'angular2/angular2'; * import {HTTP_PROVIDERS, Http} from 'angular2/http'; * * @Component({ * selector: 'app', * providers: [HTTP_PROVIDERS], * template: ` *
*

People

* *
* `, * directives: [NgFor] * }) * export class App { * people: Object[]; * constructor(http:Http) { * http.get('people.json').subscribe(res => { * this.people = res.json(); * }); * } * active:boolean = false; * toggleActiveState() { * this.active = !this.active; * } * } * * bootstrap(App) * .catch(err => console.error(err)); * ``` * * The primary public API included in `HTTP_PROVIDERS` is the {@link Http} class. * However, other providers required by `Http` are included, * which may be beneficial to override in certain cases. * * The providers included in `HTTP_PROVIDERS` include: * * {@link Http} * * {@link XHRBackend} * * `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. * To accomplish this, a new provider for {@link RequestOptions} should * be added in the same injector as `HTTP_PROVIDERS`. * * ### Example ([live demo](http://plnkr.co/edit/aCMEXi?p=preview)) * * ``` * import {provide, bootstrap} from 'angular2/angular2'; * import {HTTP_PROVIDERS, BaseRequestOptions, RequestOptions} from 'angular2/http'; * * class MyOptions extends BaseRequestOptions { * search: string = 'coreTeam=true'; * } * * bootstrap(App, [HTTP_PROVIDERS, provide(RequestOptions, {useClass: MyOptions})]) * .catch(err => console.error(err)); * ``` * * Likewise, to use a mock backend for unit tests, the {@link XHRBackend} * provider should be bound to {@link MockBackend}. * * ### Example ([live demo](http://plnkr.co/edit/7LWALD?p=preview)) * * ``` * import {provide, Injector} from 'angular2/angular2'; * import {HTTP_PROVIDERS, Http, Response, XHRBackend, MockBackend} from 'angular2/http'; * * var people = [{name: 'Jeff'}, {name: 'Tobias'}]; * * var injector = Injector.resolveAndCreate([ * HTTP_PROVIDERS, * MockBackend, * provide(XHRBackend, {useExisting: MockBackend}) * ]); * 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); * }); * }); * * http.get('people.json').observer({ * next: res => { * // Response came from mock backend * console.log('first person', res.json()[0].name); * } * }); * ``` */ export const HTTP_PROVIDERS: any[] = [ // TODO(pascal): use factory type annotations once supported in DI // issue: https://github.com/angular/angular/issues/3183 provide(Http, { useFactory: (xhrBackend, requestOptions) => new Http(xhrBackend, requestOptions), deps: [XHRBackend, RequestOptions] }), BrowserXhr, provide(RequestOptions, {useClass: BaseRequestOptions}), provide(ResponseOptions, {useClass: BaseResponseOptions}), XHRBackend ]; /** * @deprecated */ export const HTTP_BINDINGS = HTTP_PROVIDERS; /** * Provides a basic set of providers to use the {@link Jsonp} service in any application. * * The `JSONP_PROVIDERS` should be included either in a component's injector, * or in the root injector when bootstrapping an application. * * ### Example ([live demo](http://plnkr.co/edit/vmeN4F?p=preview)) * * ``` * import {Component, NgFor, View} from 'angular2/angular2'; * import {JSONP_PROVIDERS, Jsonp} from 'angular2/http'; * * @Component({ * selector: 'app', * providers: [JSONP_PROVIDERS], * template: ` *
*

People

* *
* `, * directives: [NgFor] * }) * export class App { * people: Array; * constructor(jsonp:Jsonp) { * jsonp.request('people.json').subscribe(res => { * this.people = res.json(); * }) * } * } * ``` * * The primary public API included in `JSONP_PROVIDERS` is the {@link Jsonp} class. * However, other providers required by `Jsonp` are included, * which may be beneficial to override in certain cases. * * The providers included in `JSONP_PROVIDERS` include: * * {@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. * To accomplish this, a new provider for {@link RequestOptions} should * be added in the same injector as `JSONP_PROVIDERS`. * * ### Example ([live demo](http://plnkr.co/edit/TFug7x?p=preview)) * * ``` * import {provide, bootstrap} from 'angular2/angular2'; * import {JSONP_PROVIDERS, BaseRequestOptions, RequestOptions} from 'angular2/http'; * * class MyOptions extends BaseRequestOptions { * search: string = 'coreTeam=true'; * } * * bootstrap(App, [JSONP_PROVIDERS, provide(RequestOptions, {useClass: MyOptions})]) * .catch(err => console.error(err)); * ``` * * Likewise, to use a mock backend for unit tests, the {@link JSONPBackend} * provider should be bound to {@link MockBackend}. * * ### Example ([live demo](http://plnkr.co/edit/HDqZWL?p=preview)) * * ``` * import {provide, Injector} from 'angular2/angular2'; * import {JSONP_PROVIDERS, Jsonp, Response, JSONPBackend, MockBackend} from 'angular2/http'; * * var people = [{name: 'Jeff'}, {name: 'Tobias'}]; * var injector = Injector.resolveAndCreate([ * JSONP_PROVIDERS, * MockBackend, * provide(JSONPBackend, {useExisting: MockBackend}) * ]); * 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); * }); * }); * jsonp.get('people.json').observer({ * next: res => { * // Response came from mock backend * console.log('first person', res.json()[0].name); * } * }); * ``` */ export const JSONP_PROVIDERS: any[] = [ // TODO(pascal): use factory type annotations once supported in DI // issue: https://github.com/angular/angular/issues/3183 provide(Jsonp, { useFactory: (jsonpBackend, requestOptions) => new Jsonp(jsonpBackend, requestOptions), deps: [JSONPBackend, RequestOptions] }), BrowserJsonp, provide(RequestOptions, {useClass: BaseRequestOptions}), provide(ResponseOptions, {useClass: BaseResponseOptions}), provide(JSONPBackend, {useClass: JSONPBackend_}) ]; /** * @deprecated */ export const JSON_BINDINGS = JSONP_PROVIDERS;