refactor(http): Removed deprecated HTTP_PROVIDERS and JSONP_PROVIDERS (#10864)
BREAKING CHANGE: previously deprecated HTTP_PROVIDERS and JSONP_PROVIDERS were removed; see deprecation notice for migration instructions.
This commit is contained in:
parent
0a22e8eefd
commit
675e582ffd
|
@ -36,154 +36,7 @@ export {Request} from './src/static_request';
|
|||
export {Response} from './src/static_response';
|
||||
export {QueryEncoder, URLSearchParams} from './src/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 {Component} from '@angular/core';
|
||||
* import {bootstrap} from '@angular/platform-browser/browser';
|
||||
* import {NgFor} from '@angular/common';
|
||||
* import {HTTP_PROVIDERS, Http} from '@angular/http';
|
||||
*
|
||||
* @Component({
|
||||
* selector: 'app',
|
||||
* providers: [HTTP_PROVIDERS],
|
||||
* template: `
|
||||
* <div>
|
||||
* <h1>People</h1>
|
||||
* <ul>
|
||||
* <li *ngFor="let person of people">
|
||||
* {{person.name}}
|
||||
* </li>
|
||||
* </ul>
|
||||
* </div>
|
||||
* `,
|
||||
* 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;
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* 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}
|
||||
* * {@link XSRFStrategy} - Bound to {@link CookieXSRFStrategy} class (see below)
|
||||
* * `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} from '@angular/core';
|
||||
* import {bootstrap} from '@angular/platform-browser/browser';
|
||||
* import {HTTP_PROVIDERS, BaseRequestOptions, RequestOptions} from '@angular/http';
|
||||
*
|
||||
* class MyOptions extends BaseRequestOptions {
|
||||
* search: string = 'coreTeam=true';
|
||||
* }
|
||||
*
|
||||
* @NgModule({
|
||||
* imports: [HttpModule],
|
||||
* providers: [{provide: RequestOptions, useClass: MyOptions}]
|
||||
* })
|
||||
* class MyModule {}
|
||||
* ```
|
||||
*
|
||||
* 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} 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';
|
||||
*
|
||||
* 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);
|
||||
* }
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* `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,
|
||||
* [HTTP_PROVIDERS, {provide: XSRFStrategy,
|
||||
* useValue: new CookieXSRFStrategy('MY-XSRF-COOKIE-NAME', 'X-MY-XSRF-HEADER-NAME')}])
|
||||
* .catch(err => console.error(err));
|
||||
* ```
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
export const HTTP_PROVIDERS: any[] = [
|
||||
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: httpFactory, deps: [XHRBackend, RequestOptions]},
|
||||
|
@ -208,123 +61,7 @@ export function httpFactory(xhrBackend: XHRBackend, requestOptions: RequestOptio
|
|||
return new Http(xhrBackend, requestOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* See {@link HTTP_PROVIDERS} instead.
|
||||
*
|
||||
* @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} from '@angular/core';
|
||||
* import {NgFor} from '@angular/common';
|
||||
* import {JSONP_PROVIDERS, Jsonp} from '@angular/http';
|
||||
*
|
||||
* @Component({
|
||||
* selector: 'app',
|
||||
* providers: [JSONP_PROVIDERS],
|
||||
* template: `
|
||||
* <div>
|
||||
* <h1>People</h1>
|
||||
* <ul>
|
||||
* <li *ngFor="let person of people">
|
||||
* {{person.name}}
|
||||
* </li>
|
||||
* </ul>
|
||||
* </div>
|
||||
* `,
|
||||
* directives: [NgFor]
|
||||
* })
|
||||
* export class App {
|
||||
* people: Array<Object>;
|
||||
* 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} from '@angular/core';
|
||||
* import {bootstrap} from '@angular/platform-browser/browser';
|
||||
* import {JSONP_PROVIDERS, BaseRequestOptions, RequestOptions} from '@angular/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 '@angular/core';
|
||||
* import {JSONP_PROVIDERS, Jsonp, Response, JSONPBackend} from '@angular/http';
|
||||
* import {MockBackend} from '@angular/http/testing';
|
||||
*
|
||||
* 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);
|
||||
* }
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* @experimental
|
||||
*/
|
||||
export const JSONP_PROVIDERS: any[] = [
|
||||
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: jsonpFactory, deps: [JSONPBackend, RequestOptions]},
|
||||
|
@ -342,13 +79,6 @@ export function jsonpFactory(jsonpBackend: JSONPBackend, requestOptions: Request
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* See {@link JSONP_PROVIDERS} instead.
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
export const JSON_BINDINGS = JSONP_PROVIDERS;
|
||||
|
||||
/**
|
||||
* The module that includes http's providers
|
||||
*
|
||||
|
|
|
@ -7,12 +7,13 @@
|
|||
*/
|
||||
|
||||
import {Injector, ReflectiveInjector} from '@angular/core';
|
||||
import {TestBed, getTestBed} from '@angular/core/testing';
|
||||
import {AsyncTestCompleter, afterEach, beforeEach, ddescribe, describe, iit, inject, it, xit} from '@angular/core/testing/testing_internal';
|
||||
import {expect} from '@angular/platform-browser/testing/matchers';
|
||||
import {Observable} from 'rxjs/Observable';
|
||||
import {zip} from 'rxjs/observable/zip';
|
||||
|
||||
import {BaseRequestOptions, ConnectionBackend, HTTP_PROVIDERS, Http, JSONPBackend, JSONP_PROVIDERS, Jsonp, Request, RequestMethod, RequestOptions, Response, ResponseContentType, ResponseOptions, URLSearchParams, XHRBackend} from '../http';
|
||||
import {BaseRequestOptions, ConnectionBackend, Http, HttpModule, JSONPBackend, Jsonp, JsonpModule, Request, RequestMethod, RequestOptions, Response, ResponseContentType, ResponseOptions, URLSearchParams, XHRBackend} from '../http';
|
||||
import {Json} from '../src/facade/lang';
|
||||
import {stringToArrayBuffer} from '../src/http_utils';
|
||||
import {MockBackend, MockConnection} from '../testing/mock_backend';
|
||||
|
@ -21,33 +22,33 @@ export function main() {
|
|||
describe('injectables', () => {
|
||||
var url = 'http://foo.bar';
|
||||
var http: Http;
|
||||
var parentInjector: ReflectiveInjector;
|
||||
var childInjector: ReflectiveInjector;
|
||||
var injector: Injector;
|
||||
var jsonpBackend: MockBackend;
|
||||
var xhrBackend: MockBackend;
|
||||
var jsonp: Jsonp;
|
||||
|
||||
it('should allow using jsonpInjectables and httpInjectables in same injector',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
parentInjector = ReflectiveInjector.resolveAndCreate([
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [HttpModule, JsonpModule],
|
||||
providers: [
|
||||
{provide: XHRBackend, useClass: MockBackend},
|
||||
{provide: JSONPBackend, useClass: MockBackend}
|
||||
]);
|
||||
]
|
||||
});
|
||||
injector = getTestBed();
|
||||
});
|
||||
|
||||
childInjector = parentInjector.resolveAndCreateChild([
|
||||
HTTP_PROVIDERS, JSONP_PROVIDERS, {provide: XHRBackend, useClass: MockBackend},
|
||||
{provide: JSONPBackend, useClass: MockBackend}
|
||||
]);
|
||||
it('should allow using jsonpInjectables and httpInjectables in same injector',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
|
||||
http = childInjector.get(Http);
|
||||
jsonp = childInjector.get(Jsonp);
|
||||
jsonpBackend = childInjector.get(JSONPBackend);
|
||||
xhrBackend = childInjector.get(XHRBackend);
|
||||
http = injector.get(Http);
|
||||
jsonp = injector.get(Jsonp);
|
||||
jsonpBackend = injector.get(JSONPBackend);
|
||||
xhrBackend = injector.get(XHRBackend);
|
||||
|
||||
var xhrCreatedConnections = 0;
|
||||
var jsonpCreatedConnections = 0;
|
||||
|
||||
|
||||
xhrBackend.connections.subscribe(() => {
|
||||
xhrCreatedConnections++;
|
||||
expect(xhrCreatedConnections).toEqual(1);
|
||||
|
|
|
@ -7,13 +7,13 @@
|
|||
*/
|
||||
|
||||
import {NgModule} from '@angular/core';
|
||||
import {HTTP_PROVIDERS} from '@angular/http';
|
||||
import {HttpModule} from '@angular/http';
|
||||
import {BrowserModule} from '@angular/platform-browser';
|
||||
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
|
||||
|
||||
import {HttpCmp} from './app/http_comp';
|
||||
|
||||
@NgModule({bootstrap: [HttpCmp], providers: [HTTP_PROVIDERS], imports: [BrowserModule]})
|
||||
@NgModule({bootstrap: [HttpCmp], imports: [BrowserModule, HttpModule]})
|
||||
class ExampleModule {
|
||||
}
|
||||
|
||||
|
|
|
@ -7,18 +7,13 @@
|
|||
*/
|
||||
|
||||
import {NgModule} from '@angular/core';
|
||||
import {JSONP_PROVIDERS} from '@angular/http';
|
||||
import {JsonpModule} from '@angular/http';
|
||||
import {BrowserModule} from '@angular/platform-browser';
|
||||
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
|
||||
|
||||
import {JsonpCmp} from './app/jsonp_comp';
|
||||
|
||||
@NgModule({
|
||||
bootstrap: [JsonpCmp],
|
||||
declarations: [JsonpCmp],
|
||||
providers: [JSONP_PROVIDERS],
|
||||
imports: [BrowserModule]
|
||||
})
|
||||
@NgModule({bootstrap: [JsonpCmp], declarations: [JsonpCmp], imports: [BrowserModule, JsonpModule]})
|
||||
class ExampleModule {
|
||||
}
|
||||
|
||||
|
|
|
@ -71,12 +71,6 @@ export declare class Http {
|
|||
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response>;
|
||||
}
|
||||
|
||||
/** @deprecated */
|
||||
export declare const HTTP_BINDINGS: any[];
|
||||
|
||||
/** @deprecated */
|
||||
export declare const HTTP_PROVIDERS: any[];
|
||||
|
||||
/** @experimental */
|
||||
export declare function httpFactory(xhrBackend: XHRBackend, requestOptions: RequestOptions): Http;
|
||||
|
||||
|
@ -84,18 +78,12 @@ export declare function httpFactory(xhrBackend: XHRBackend, requestOptions: Requ
|
|||
export declare class HttpModule {
|
||||
}
|
||||
|
||||
/** @deprecated */
|
||||
export declare const JSON_BINDINGS: any[];
|
||||
|
||||
/** @experimental */
|
||||
export declare class Jsonp extends Http {
|
||||
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions);
|
||||
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response>;
|
||||
}
|
||||
|
||||
/** @experimental */
|
||||
export declare const JSONP_PROVIDERS: any[];
|
||||
|
||||
/** @experimental */
|
||||
export declare abstract class JSONPBackend extends ConnectionBackend {
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue