refactor(Http): remove HttpFactory
BREAKING CHANGE: HttpFactory is no longer available. This factory provided a function alternative to the `request` method of the Http class, but added no real value. The additional factory required an additional IHttp interface, an odd way to inject while preserving type information (`@Inject(HttpFactory) http:IHttp`), and required additional documentation in the http module. Closes #2564
This commit is contained in:
parent
55bf0e554f
commit
146dbf1270
@ -6,7 +6,7 @@
|
|||||||
* class.
|
* class.
|
||||||
*/
|
*/
|
||||||
import {bind, Binding} from 'angular2/di';
|
import {bind, Binding} from 'angular2/di';
|
||||||
import {Http, HttpFactory} from './src/http/http';
|
import {Http} from './src/http/http';
|
||||||
import {XHRBackend, XHRConnection} from 'angular2/src/http/backends/xhr_backend';
|
import {XHRBackend, XHRConnection} from 'angular2/src/http/backends/xhr_backend';
|
||||||
import {BrowserXHR} from 'angular2/src/http/backends/browser_xhr';
|
import {BrowserXHR} from 'angular2/src/http/backends/browser_xhr';
|
||||||
import {BaseRequestOptions, RequestOptions} from 'angular2/src/http/base_request_options';
|
import {BaseRequestOptions, RequestOptions} from 'angular2/src/http/base_request_options';
|
||||||
@ -17,7 +17,6 @@ export {Request} from 'angular2/src/http/static_request';
|
|||||||
export {Response} from 'angular2/src/http/static_response';
|
export {Response} from 'angular2/src/http/static_response';
|
||||||
|
|
||||||
export {
|
export {
|
||||||
IHttp,
|
|
||||||
IRequestOptions,
|
IRequestOptions,
|
||||||
IResponse,
|
IResponse,
|
||||||
Connection,
|
Connection,
|
||||||
@ -26,7 +25,7 @@ export {
|
|||||||
|
|
||||||
export {BaseRequestOptions, RequestOptions} from 'angular2/src/http/base_request_options';
|
export {BaseRequestOptions, RequestOptions} from 'angular2/src/http/base_request_options';
|
||||||
export {XHRBackend, XHRConnection} from 'angular2/src/http/backends/xhr_backend';
|
export {XHRBackend, XHRConnection} from 'angular2/src/http/backends/xhr_backend';
|
||||||
export {Http, HttpFactory} from './src/http/http';
|
export {Http} from './src/http/http';
|
||||||
|
|
||||||
export {Headers} from 'angular2/src/http/headers';
|
export {Headers} from 'angular2/src/http/headers';
|
||||||
|
|
||||||
@ -50,12 +49,5 @@ export {URLSearchParams} from 'angular2/src/http/url_search_params';
|
|||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
export var httpInjectables: List<any> = [
|
export var httpInjectables: List<any> =
|
||||||
bind(ConnectionBackend)
|
[bind(ConnectionBackend).toClass(XHRBackend), BrowserXHR, XHRBackend, BaseRequestOptions, Http];
|
||||||
.toClass(XHRBackend),
|
|
||||||
BrowserXHR,
|
|
||||||
XHRBackend,
|
|
||||||
BaseRequestOptions,
|
|
||||||
bind(HttpFactory).toFactory(HttpFactory, [XHRBackend, BaseRequestOptions]),
|
|
||||||
Http
|
|
||||||
];
|
|
||||||
|
@ -144,36 +144,3 @@ export class Http {
|
|||||||
RequestMethods.HEAD, url)));
|
RequestMethods.HEAD, url)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* Alias to the `request` method of {@link Http}, for those who'd prefer a simple function instead
|
|
||||||
* of an object. In order to get TypeScript type information about the `HttpFactory`, the {@link
|
|
||||||
* IHttp} interface can be used as shown in the following example.
|
|
||||||
*
|
|
||||||
* #Example
|
|
||||||
*
|
|
||||||
* ```
|
|
||||||
* import {httpInjectables, HttpFactory, IHttp} from 'angular2/http';
|
|
||||||
* @Component({
|
|
||||||
* appInjector: [httpInjectables]
|
|
||||||
* })
|
|
||||||
* @View({
|
|
||||||
* templateUrl: 'people.html'
|
|
||||||
* })
|
|
||||||
* class MyComponent {
|
|
||||||
* constructor(@Inject(HttpFactory) http:IHttp) {
|
|
||||||
* http('people.json').subscribe(res => this.people = res.json());
|
|
||||||
* }
|
|
||||||
* }
|
|
||||||
* ```
|
|
||||||
**/
|
|
||||||
export function HttpFactory(backend: ConnectionBackend, defaultOptions: BaseRequestOptions) {
|
|
||||||
return function(url: string | Request, options?: IRequestOptions) {
|
|
||||||
if (isString(url)) {
|
|
||||||
return httpRequest(backend, new Request(mergeOptions(defaultOptions, options, null, url)));
|
|
||||||
} else if (url instanceof Request) {
|
|
||||||
return httpRequest(backend, url);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
@ -55,33 +55,8 @@ export interface IResponse {
|
|||||||
url: string;
|
url: string;
|
||||||
totalBytes: number;
|
totalBytes: number;
|
||||||
bytesLoaded: number;
|
bytesLoaded: number;
|
||||||
blob(): any; // TODO: Blob
|
blob(): any; // TODO: Blob
|
||||||
arrayBuffer(): any; // TODO: ArrayBuffer
|
arrayBuffer(): any; // TODO: ArrayBuffer
|
||||||
text(): string;
|
text(): string;
|
||||||
json(): Object;
|
json(): Object;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Provides an interface to provide type information for {@link HttpFactory} when injecting.
|
|
||||||
*
|
|
||||||
* #Example
|
|
||||||
*
|
|
||||||
* ```
|
|
||||||
* * import {httpInjectables, HttpFactory, IHttp} from 'angular2/http';
|
|
||||||
* @Component({
|
|
||||||
* appInjector: [httpInjectables]
|
|
||||||
* })
|
|
||||||
* @View({
|
|
||||||
* templateUrl: 'people.html'
|
|
||||||
* })
|
|
||||||
* class MyComponent {
|
|
||||||
* constructor(@Inject(HttpFactory) http:IHttp) {
|
|
||||||
* http('people.json').subscribe(res => this.people = res.json());
|
|
||||||
* }
|
|
||||||
* }
|
|
||||||
* ```
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
// Prefixed as IHttp because used in conjunction with Http class, but interface is callable
|
|
||||||
// constructor(@Inject(Http) http:IHttp)
|
|
||||||
export interface IHttp { (url: string, options?: IRequestOptions): EventEmitter }
|
|
||||||
|
@ -11,7 +11,7 @@ import {
|
|||||||
xit,
|
xit,
|
||||||
SpyObject
|
SpyObject
|
||||||
} from 'angular2/test_lib';
|
} from 'angular2/test_lib';
|
||||||
import {Http, HttpFactory} from 'angular2/src/http/http';
|
import {Http} from 'angular2/src/http/http';
|
||||||
import {Injector, bind} from 'angular2/di';
|
import {Injector, bind} from 'angular2/di';
|
||||||
import {MockBackend} from 'angular2/src/http/backends/mock_backend';
|
import {MockBackend} from 'angular2/src/http/backends/mock_backend';
|
||||||
import {Response} from 'angular2/src/http/static_response';
|
import {Response} from 'angular2/src/http/static_response';
|
||||||
@ -40,13 +40,10 @@ export function main() {
|
|||||||
var injector: Injector;
|
var injector: Injector;
|
||||||
var backend: MockBackend;
|
var backend: MockBackend;
|
||||||
var baseResponse;
|
var baseResponse;
|
||||||
var httpFactory;
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
injector = Injector.resolveAndCreate([
|
injector = Injector.resolveAndCreate([
|
||||||
BaseRequestOptions,
|
BaseRequestOptions,
|
||||||
MockBackend,
|
MockBackend,
|
||||||
bind(ConnectionBackend).toClass(MockBackend),
|
|
||||||
bind(HttpFactory).toFactory(HttpFactory, [MockBackend, BaseRequestOptions]),
|
|
||||||
bind(Http).toFactory(
|
bind(Http).toFactory(
|
||||||
function(backend: ConnectionBackend, defaultOptions: BaseRequestOptions) {
|
function(backend: ConnectionBackend, defaultOptions: BaseRequestOptions) {
|
||||||
return new Http(backend, defaultOptions);
|
return new Http(backend, defaultOptions);
|
||||||
@ -54,55 +51,12 @@ export function main() {
|
|||||||
[MockBackend, BaseRequestOptions])
|
[MockBackend, BaseRequestOptions])
|
||||||
]);
|
]);
|
||||||
http = injector.get(Http);
|
http = injector.get(Http);
|
||||||
httpFactory = injector.get(HttpFactory);
|
|
||||||
backend = injector.get(MockBackend);
|
backend = injector.get(MockBackend);
|
||||||
baseResponse = new Response({body: 'base response'});
|
baseResponse = new Response({body: 'base response'});
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => backend.verifyNoPendingRequests());
|
afterEach(() => backend.verifyNoPendingRequests());
|
||||||
|
|
||||||
describe('HttpFactory', () => {
|
|
||||||
it('should return an Observable', () => {
|
|
||||||
expect(ObservableWrapper.isObservable(httpFactory(url))).toBe(true);
|
|
||||||
backend.resolveAllConnections();
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
it('should perform a get request for given url if only passed a string',
|
|
||||||
inject([AsyncTestCompleter], (async) => {
|
|
||||||
ObservableWrapper.subscribe(backend.connections, c => c.mockRespond(baseResponse));
|
|
||||||
ObservableWrapper.subscribe(httpFactory('http://basic.connection'), res => {
|
|
||||||
expect(res.text()).toBe('base response');
|
|
||||||
async.done();
|
|
||||||
});
|
|
||||||
|
|
||||||
}));
|
|
||||||
|
|
||||||
|
|
||||||
it('should accept a fully-qualified request as its only parameter',
|
|
||||||
inject([AsyncTestCompleter], (async) => {
|
|
||||||
var req = new Request(new RequestOptions({url: 'https://google.com'}));
|
|
||||||
ObservableWrapper.subscribe(backend.connections, c => {
|
|
||||||
expect(c.request.url).toBe('https://google.com');
|
|
||||||
c.mockRespond(new Response({body: 'Thank you'}));
|
|
||||||
async.done();
|
|
||||||
});
|
|
||||||
ObservableWrapper.subscribe(httpFactory(req), (res) => {});
|
|
||||||
}));
|
|
||||||
|
|
||||||
// TODO: make dart not complain about "argument type 'Map' cannot be assigned to the parameter
|
|
||||||
// type 'IRequestOptions'"
|
|
||||||
// xit('should perform a get request for given url if passed a dictionary',
|
|
||||||
// inject([AsyncTestCompleter], async => {
|
|
||||||
// ObservableWrapper.subscribe(backend.connections, c => c.mockRespond(baseResponse));
|
|
||||||
// ObservableWrapper.subscribe(httpFactory(url, {method: RequestMethods.GET}), res => {
|
|
||||||
// expect(res.text()).toBe('base response');
|
|
||||||
// async.done();
|
|
||||||
// });
|
|
||||||
// }));
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
describe('Http', () => {
|
describe('Http', () => {
|
||||||
describe('.request()', () => {
|
describe('.request()', () => {
|
||||||
it('should return an Observable',
|
it('should return an Observable',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user