fix(http): use Observable<Response> on Http methods

use correct type definitions for Http responses.

Closes #5017
This commit is contained in:
Rob Wormald 2015-10-30 23:52:37 -07:00
parent 31687efd64
commit a9b1270a5a
3 changed files with 15 additions and 18 deletions

View File

@ -5,12 +5,9 @@ import {Response} from '../static_response';
import {ResponseOptions, BaseResponseOptions} from '../base_response_options'; import {ResponseOptions, BaseResponseOptions} from '../base_response_options';
import {Injectable} from 'angular2/angular2'; import {Injectable} from 'angular2/angular2';
import {BrowserJsonp} from './browser_jsonp'; import {BrowserJsonp} from './browser_jsonp';
import {EventEmitter, ObservableWrapper} from 'angular2/src/core/facade/async';
import {makeTypeError} from 'angular2/src/core/facade/exceptions'; import {makeTypeError} from 'angular2/src/core/facade/exceptions';
import {StringWrapper, isPresent} from 'angular2/src/core/facade/lang'; import {StringWrapper, isPresent} from 'angular2/src/core/facade/lang';
// todo(robwormald): temporary until https://github.com/angular/angular/issues/4390 decided import {Observable} from 'angular2/angular2';
var Rx = require('@reactivex/rxjs/dist/cjs/Rx');
var {Observable} = Rx;
const JSONP_ERR_NO_CALLBACK = 'JSONP injected script did not invoke callback.'; const JSONP_ERR_NO_CALLBACK = 'JSONP injected script did not invoke callback.';
const JSONP_ERR_WRONG_METHOD = 'JSONP requests must use GET request method.'; const JSONP_ERR_WRONG_METHOD = 'JSONP requests must use GET request method.';
@ -18,7 +15,7 @@ const JSONP_ERR_WRONG_METHOD = 'JSONP requests must use GET request method.';
export abstract class JSONPConnection implements Connection { export abstract class JSONPConnection implements Connection {
readyState: ReadyStates; readyState: ReadyStates;
request: Request; request: Request;
response: any; response: Observable<Response>;
abstract finished(data?: any): void; abstract finished(data?: any): void;
} }

View File

@ -6,9 +6,7 @@ import {ResponseOptions, BaseResponseOptions} from '../base_response_options';
import {Injectable} from 'angular2/angular2'; import {Injectable} from 'angular2/angular2';
import {BrowserXhr} from './browser_xhr'; import {BrowserXhr} from './browser_xhr';
import {isPresent} from 'angular2/src/core/facade/lang'; import {isPresent} from 'angular2/src/core/facade/lang';
// todo(robwormald): temporary until https://github.com/angular/angular/issues/4390 decided import {Observable} from 'angular2/angular2';
var Rx = require('@reactivex/rxjs/dist/cjs/Rx');
var {Observable} = Rx;
/** /**
* Creates connections using `XMLHttpRequest`. Given a fully-qualified * Creates connections using `XMLHttpRequest`. Given a fully-qualified
* request, an `XHRConnection` will immediately create an `XMLHttpRequest` object and send the * request, an `XHRConnection` will immediately create an `XMLHttpRequest` object and send the
@ -23,7 +21,7 @@ export class XHRConnection implements Connection {
* Response {@link EventEmitter} which emits a single {@link Response} value on load event of * Response {@link EventEmitter} which emits a single {@link Response} value on load event of
* `XMLHttpRequest`. * `XMLHttpRequest`.
*/ */
response: any; // TODO: Make generic of <Response>; response: Observable<Response>;
readyState: ReadyStates; readyState: ReadyStates;
constructor(req: Request, browserXHR: BrowserXhr, baseResponseOptions?: ResponseOptions) { constructor(req: Request, browserXHR: BrowserXhr, baseResponseOptions?: ResponseOptions) {
this.request = req; this.request = req;

View File

@ -3,10 +3,12 @@ import {makeTypeError} from 'angular2/src/core/facade/exceptions';
import {Injectable} from 'angular2/angular2'; import {Injectable} from 'angular2/angular2';
import {RequestOptionsArgs, Connection, ConnectionBackend} from './interfaces'; import {RequestOptionsArgs, Connection, ConnectionBackend} from './interfaces';
import {Request} from './static_request'; import {Request} from './static_request';
import {Response} from './static_response';
import {BaseRequestOptions, RequestOptions} from './base_request_options'; import {BaseRequestOptions, RequestOptions} from './base_request_options';
import {RequestMethods} from './enums'; import {RequestMethods} from './enums';
import {Observable} from 'angular2/angular2';
function httpRequest(backend: ConnectionBackend, request: Request): any { function httpRequest(backend: ConnectionBackend, request: Request): Observable<Response> {
return backend.createConnection(request).response; return backend.createConnection(request).response;
} }
@ -96,7 +98,7 @@ export class Http {
* object can be provided as the 2nd argument. The options object will be merged with the values * object can be provided as the 2nd argument. The options object will be merged with the values
* of {@link BaseRequestOptions} before performing the request. * of {@link BaseRequestOptions} before performing the request.
*/ */
request(url: string | Request, options?: RequestOptionsArgs): any { request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
var responseObservable: any; var responseObservable: any;
if (isString(url)) { if (isString(url)) {
responseObservable = httpRequest( responseObservable = httpRequest(
@ -113,7 +115,7 @@ export class Http {
/** /**
* Performs a request with `get` http method. * Performs a request with `get` http method.
*/ */
get(url: string, options?: RequestOptionsArgs): any { get(url: string, options?: RequestOptionsArgs): Observable<Response> {
return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options, return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options,
RequestMethods.Get, url))); RequestMethods.Get, url)));
} }
@ -121,7 +123,7 @@ export class Http {
/** /**
* Performs a request with `post` http method. * Performs a request with `post` http method.
*/ */
post(url: string, body: string, options?: RequestOptionsArgs): any { post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
return httpRequest( return httpRequest(
this._backend, this._backend,
new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({body: body})), new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({body: body})),
@ -131,7 +133,7 @@ export class Http {
/** /**
* Performs a request with `put` http method. * Performs a request with `put` http method.
*/ */
put(url: string, body: string, options?: RequestOptionsArgs): any { put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
return httpRequest( return httpRequest(
this._backend, this._backend,
new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({body: body})), new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({body: body})),
@ -141,7 +143,7 @@ export class Http {
/** /**
* Performs a request with `delete` http method. * Performs a request with `delete` http method.
*/ */
delete (url: string, options?: RequestOptionsArgs): any { delete (url: string, options?: RequestOptionsArgs): Observable<Response> {
return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options, return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options,
RequestMethods.Delete, url))); RequestMethods.Delete, url)));
} }
@ -149,7 +151,7 @@ export class Http {
/** /**
* Performs a request with `patch` http method. * Performs a request with `patch` http method.
*/ */
patch(url: string, body: string, options?: RequestOptionsArgs): any { patch(url: string, body: string, options?: RequestOptionsArgs): Observable<Response> {
return httpRequest( return httpRequest(
this._backend, this._backend,
new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({body: body})), new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({body: body})),
@ -159,7 +161,7 @@ export class Http {
/** /**
* Performs a request with `head` http method. * Performs a request with `head` http method.
*/ */
head(url: string, options?: RequestOptionsArgs): any { head(url: string, options?: RequestOptionsArgs): Observable<Response> {
return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options, return httpRequest(this._backend, new Request(mergeOptions(this._defaultOptions, options,
RequestMethods.Head, url))); RequestMethods.Head, url)));
} }
@ -177,7 +179,7 @@ export class Jsonp extends Http {
* object can be provided as the 2nd argument. The options object will be merged with the values * object can be provided as the 2nd argument. The options object will be merged with the values
* of {@link BaseRequestOptions} before performing the request. * of {@link BaseRequestOptions} before performing the request.
*/ */
request(url: string | Request, options?: RequestOptionsArgs): any { request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
var responseObservable: any; var responseObservable: any;
if (isString(url)) { if (isString(url)) {
url = new Request(mergeOptions(this._defaultOptions, options, RequestMethods.Get, url)); url = new Request(mergeOptions(this._defaultOptions, options, RequestMethods.Get, url));