Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions modules/angular2/src/http/backends/jsonp_backend.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import {ConnectionBackend, Connection} from '../interfaces';
import {ReadyStates, RequestMethods} from '../enums';
import {ReadyStates, RequestMethods, ResponseTypes} from '../enums';
import {Request} from '../static_request';
import {Response} from '../static_response';
import {ResponseOptions, BaseResponseOptions} from '../base_response_options';
import {Injectable} from 'angular2/angular2';
import {BrowserJsonp} from './browser_jsonp';
import {EventEmitter, ObservableWrapper} from 'angular2/src/core/facade/async';
import {makeTypeError} from 'angular2/src/core/facade/exceptions';
import {StringWrapper, isPresent} from 'angular2/src/core/facade/lang';
// todo(robwormald): temporary until https://github.com/angular/angular/issues/4390 decided
var Rx = require('@reactivex/rxjs/dist/cjs/Rx');
var {Observable} = Rx;
import {Observable} from 'angular2/angular2';

const JSONP_ERR_NO_CALLBACK = 'JSONP injected script did not invoke callback.';
const JSONP_ERR_WRONG_METHOD = 'JSONP requests must use GET request method.';

export abstract class JSONPConnection implements Connection {
readyState: ReadyStates;
request: Request;
response: any;
response: Observable<Response>;
abstract finished(data?: any): void;
}

Expand All @@ -28,7 +29,7 @@ export class JSONPConnection_ extends JSONPConnection {
private baseResponseOptions?: ResponseOptions) {
super();
if (req.method !== RequestMethods.Get) {
throw makeTypeError("JSONP requests must use GET request method.");
throw makeTypeError(JSONP_ERR_WRONG_METHOD);
}
this.request = req;
this.response = new Observable(responseObserver => {
Expand Down Expand Up @@ -56,7 +57,12 @@ export class JSONPConnection_ extends JSONPConnection {
this.readyState = ReadyStates.Done;
_dom.cleanup(script);
if (!this._finished) {
responseObserver.error(makeTypeError('JSONP injected script did not invoke callback.'));
let responseOptions =
new ResponseOptions({body: JSONP_ERR_NO_CALLBACK, type: ResponseTypes.Error});
if (isPresent(baseResponseOptions)) {
responseOptions = baseResponseOptions.merge(responseOptions);
}
responseObserver.error(new Response(responseOptions));
return;
}

Expand All @@ -73,7 +79,11 @@ export class JSONPConnection_ extends JSONPConnection {
if (this.readyState === ReadyStates.Cancelled) return;
this.readyState = ReadyStates.Done;
_dom.cleanup(script);
responseObserver.error(error);
let responseOptions = new ResponseOptions({body: error.message, type: ResponseTypes.Error});
if (isPresent(baseResponseOptions)) {
responseOptions = baseResponseOptions.merge(responseOptions);
}
responseObserver.error(new Response(responseOptions));
};

script.addEventListener('load', onLoad);
Expand Down
6 changes: 2 additions & 4 deletions modules/angular2/src/http/backends/xhr_backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import {ResponseOptions, BaseResponseOptions} from '../base_response_options';
import {Injectable} from 'angular2/angular2';
import {BrowserXhr} from './browser_xhr';
import {isPresent} from 'angular2/src/core/facade/lang';
// todo(robwormald): temporary until https://github.com/angular/angular/issues/4390 decided
var Rx = require('@reactivex/rxjs/dist/cjs/Rx');
var {Observable} = Rx;
import {Observable} from 'angular2/angular2';
/**
* Creates connections using `XMLHttpRequest`. Given a fully-qualified
* request, an `XHRConnection` will immediately create an `XMLHttpRequest` object and send the
Expand All @@ -23,7 +21,7 @@ export class XHRConnection implements Connection {
* Response {@link EventEmitter} which emits a single {@link Response} value on load event of
* `XMLHttpRequest`.
*/
response: any; // TODO: Make generic of <Response>;
response: Observable<Response>;
readyState: ReadyStates;
constructor(req: Request, browserXHR: BrowserXhr, baseResponseOptions?: ResponseOptions) {
this.request = req;
Expand Down
20 changes: 11 additions & 9 deletions modules/angular2/src/http/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import {makeTypeError} from 'angular2/src/core/facade/exceptions';
import {Injectable} from 'angular2/angular2';
import {RequestOptionsArgs, Connection, ConnectionBackend} from './interfaces';
import {Request} from './static_request';
import {Response} from './static_response';
import {BaseRequestOptions, RequestOptions} from './base_request_options';
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;
}

Expand Down Expand Up @@ -96,7 +98,7 @@ export class Http {
* object can be provided as the 2nd argument. The options object will be merged with the values
* 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;
if (isString(url)) {
responseObservable = httpRequest(
Expand All @@ -113,15 +115,15 @@ export class Http {
/**
* 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,
RequestMethods.Get, url)));
}

/**
* 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(
this._backend,
new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({body: body})),
Expand All @@ -131,7 +133,7 @@ export class Http {
/**
* 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(
this._backend,
new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({body: body})),
Expand All @@ -141,15 +143,15 @@ export class Http {
/**
* 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,
RequestMethods.Delete, url)));
}

/**
* 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(
this._backend,
new Request(mergeOptions(this._defaultOptions.merge(new RequestOptions({body: body})),
Expand All @@ -159,7 +161,7 @@ export class Http {
/**
* 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,
RequestMethods.Head, url)));
}
Expand All @@ -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
* 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;
if (isString(url)) {
url = new Request(mergeOptions(this._defaultOptions, options, RequestMethods.Get, url));
Expand Down
4 changes: 2 additions & 2 deletions modules/angular2/test/http/backends/jsonp_backend_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export function main() {
async.done();
},
err => {
expect(StringWrapper.contains(err.message, 'did not invoke callback')).toBe(true);
expect(err.text()).toEqual('JSONP injected script did not invoke callback.');
async.done();
});

Expand All @@ -150,7 +150,7 @@ export function main() {
async.done();
},
err => {
expect(err['message']).toBe('Oops!');
expect(err.text()).toBe('Oops!');
async.done();
});

Expand Down