refactor(http): rename enums to be singular

ReadyStates -> ReadyState
RequestMethods -> RequestMethod
ResponseTypes -> ResponseType

Fixes #5574

BREAKING CHANGE:

Before

import {ReadyStates, RequestMethods, ResponseTypes} from 'angular2/http';

After

import {ReadyState, RequestMethod, ResponseType} from 'angular2/http';

Closes #5584
This commit is contained in:
cexbrayat 2015-12-03 22:44:14 +01:00 committed by Cédric Exbrayat
parent 654496b315
commit b925ff5b8d
17 changed files with 100 additions and 100 deletions

View File

@ -32,7 +32,7 @@ export {Http, Jsonp} from './src/http/http';
export {Headers} from './src/http/headers'; export {Headers} from './src/http/headers';
export {ResponseTypes, ReadyStates, RequestMethods} from './src/http/enums'; export {ResponseType, ReadyState, RequestMethod} from './src/http/enums';
export {URLSearchParams} from './src/http/url_search_params'; export {URLSearchParams} from './src/http/url_search_params';
/** /**

View File

@ -1,5 +1,5 @@
import {ConnectionBackend, Connection} from '../interfaces'; import {ConnectionBackend, Connection} from '../interfaces';
import {ReadyStates, RequestMethods, ResponseTypes} from '../enums'; import {ReadyState, RequestMethod, ResponseType} from '../enums';
import {Request} from '../static_request'; import {Request} from '../static_request';
import {Response} from '../static_response'; import {Response} from '../static_response';
import {ResponseOptions, BaseResponseOptions} from '../base_response_options'; import {ResponseOptions, BaseResponseOptions} from '../base_response_options';
@ -13,7 +13,7 @@ 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.';
export abstract class JSONPConnection implements Connection { export abstract class JSONPConnection implements Connection {
readyState: ReadyStates; readyState: ReadyState;
request: Request; request: Request;
response: Observable<Response>; response: Observable<Response>;
abstract finished(data?: any): void; abstract finished(data?: any): void;
@ -28,13 +28,13 @@ export class JSONPConnection_ extends JSONPConnection {
constructor(req: Request, private _dom: BrowserJsonp, constructor(req: Request, private _dom: BrowserJsonp,
private baseResponseOptions?: ResponseOptions) { private baseResponseOptions?: ResponseOptions) {
super(); super();
if (req.method !== RequestMethods.Get) { if (req.method !== RequestMethod.Get) {
throw makeTypeError(JSONP_ERR_WRONG_METHOD); throw makeTypeError(JSONP_ERR_WRONG_METHOD);
} }
this.request = req; this.request = req;
this.response = new Observable(responseObserver => { this.response = new Observable(responseObserver => {
this.readyState = ReadyStates.Loading; this.readyState = ReadyState.Loading;
let id = this._id = _dom.nextRequestID(); let id = this._id = _dom.nextRequestID();
_dom.exposeConnection(id, this); _dom.exposeConnection(id, this);
@ -52,12 +52,12 @@ export class JSONPConnection_ extends JSONPConnection {
let script = this._script = _dom.build(url); let script = this._script = _dom.build(url);
let onLoad = event => { let onLoad = event => {
if (this.readyState === ReadyStates.Cancelled) return; if (this.readyState === ReadyState.Cancelled) return;
this.readyState = ReadyStates.Done; this.readyState = ReadyState.Done;
_dom.cleanup(script); _dom.cleanup(script);
if (!this._finished) { if (!this._finished) {
let responseOptions = let responseOptions =
new ResponseOptions({body: JSONP_ERR_NO_CALLBACK, type: ResponseTypes.Error, url}); new ResponseOptions({body: JSONP_ERR_NO_CALLBACK, type: ResponseType.Error, url});
if (isPresent(baseResponseOptions)) { if (isPresent(baseResponseOptions)) {
responseOptions = baseResponseOptions.merge(responseOptions); responseOptions = baseResponseOptions.merge(responseOptions);
} }
@ -75,10 +75,10 @@ export class JSONPConnection_ extends JSONPConnection {
}; };
let onError = error => { let onError = error => {
if (this.readyState === ReadyStates.Cancelled) return; if (this.readyState === ReadyState.Cancelled) return;
this.readyState = ReadyStates.Done; this.readyState = ReadyState.Done;
_dom.cleanup(script); _dom.cleanup(script);
let responseOptions = new ResponseOptions({body: error.message, type: ResponseTypes.Error}); let responseOptions = new ResponseOptions({body: error.message, type: ResponseType.Error});
if (isPresent(baseResponseOptions)) { if (isPresent(baseResponseOptions)) {
responseOptions = baseResponseOptions.merge(responseOptions); responseOptions = baseResponseOptions.merge(responseOptions);
} }
@ -91,7 +91,7 @@ export class JSONPConnection_ extends JSONPConnection {
_dom.send(script); _dom.send(script);
return () => { return () => {
this.readyState = ReadyStates.Cancelled; this.readyState = ReadyState.Cancelled;
script.removeEventListener('load', onLoad); script.removeEventListener('load', onLoad);
script.removeEventListener('error', onError); script.removeEventListener('error', onError);
if (isPresent(script)) { if (isPresent(script)) {
@ -106,7 +106,7 @@ export class JSONPConnection_ extends JSONPConnection {
// Don't leak connections // Don't leak connections
this._finished = true; this._finished = true;
this._dom.removeConnection(this._id); this._dom.removeConnection(this._id);
if (this.readyState === ReadyStates.Cancelled) return; if (this.readyState === ReadyState.Cancelled) return;
this._responseData = data; this._responseData = data;
} }
} }

View File

@ -1,7 +1,7 @@
import {Injectable} from 'angular2/core'; import {Injectable} from 'angular2/core';
import {Request} from '../static_request'; import {Request} from '../static_request';
import {Response} from '../static_response'; import {Response} from '../static_response';
import {ReadyStates} from '../enums'; import {ReadyState} from '../enums';
import {Connection, ConnectionBackend} from '../interfaces'; import {Connection, ConnectionBackend} from '../interfaces';
import {isPresent} from 'angular2/src/facade/lang'; import {isPresent} from 'angular2/src/facade/lang';
import {BaseException, WrappedException} from 'angular2/src/facade/exceptions'; import {BaseException, WrappedException} from 'angular2/src/facade/exceptions';
@ -21,7 +21,7 @@ export class MockConnection implements Connection {
* Describes the state of the connection, based on `XMLHttpRequest.readyState`, but with * Describes the state of the connection, based on `XMLHttpRequest.readyState`, but with
* additional states. For example, state 5 indicates an aborted connection. * additional states. For example, state 5 indicates an aborted connection.
*/ */
readyState: ReadyStates; readyState: ReadyState;
/** /**
* {@link Request} instance used to create the connection. * {@link Request} instance used to create the connection.
@ -36,7 +36,7 @@ export class MockConnection implements Connection {
constructor(req: Request) { constructor(req: Request) {
this.response = new ReplaySubject(1).take(1); this.response = new ReplaySubject(1).take(1);
this.readyState = ReadyStates.Open; this.readyState = ReadyState.Open;
this.request = req; this.request = req;
} }
@ -55,10 +55,10 @@ export class MockConnection implements Connection {
* *
*/ */
mockRespond(res: Response) { mockRespond(res: Response) {
if (this.readyState === ReadyStates.Done || this.readyState === ReadyStates.Cancelled) { if (this.readyState === ReadyState.Done || this.readyState === ReadyState.Cancelled) {
throw new BaseException('Connection has already been resolved'); throw new BaseException('Connection has already been resolved');
} }
this.readyState = ReadyStates.Done; this.readyState = ReadyState.Done;
this.response.next(res); this.response.next(res);
this.response.complete(); this.response.complete();
} }
@ -84,7 +84,7 @@ export class MockConnection implements Connection {
*/ */
mockError(err?: Error) { mockError(err?: Error) {
// Matches XHR semantics // Matches XHR semantics
this.readyState = ReadyStates.Done; this.readyState = ReadyState.Done;
this.response.error(err); this.response.error(err);
} }
} }

View File

@ -1,5 +1,5 @@
import {ConnectionBackend, Connection} from '../interfaces'; import {ConnectionBackend, Connection} from '../interfaces';
import {ReadyStates, RequestMethods, ResponseTypes} from '../enums'; import {ReadyState, RequestMethod, ResponseType} from '../enums';
import {Request} from '../static_request'; import {Request} from '../static_request';
import {Response} from '../static_response'; import {Response} from '../static_response';
import {Headers} from '../headers'; import {Headers} from '../headers';
@ -24,12 +24,12 @@ export class XHRConnection implements Connection {
* `XMLHttpRequest`. * `XMLHttpRequest`.
*/ */
response: Observable<Response>; response: Observable<Response>;
readyState: ReadyStates; readyState: ReadyState;
constructor(req: Request, browserXHR: BrowserXhr, baseResponseOptions?: ResponseOptions) { constructor(req: Request, browserXHR: BrowserXhr, baseResponseOptions?: ResponseOptions) {
this.request = req; this.request = req;
this.response = new Observable(responseObserver => { this.response = new Observable(responseObserver => {
let _xhr: XMLHttpRequest = browserXHR.build(); let _xhr: XMLHttpRequest = browserXHR.build();
_xhr.open(RequestMethods[req.method].toUpperCase(), req.url); _xhr.open(RequestMethod[req.method].toUpperCase(), req.url);
// load event handler // load event handler
let onLoad = () => { let onLoad = () => {
// responseText is the old-school way of retrieving response (supported by IE8 & 9) // responseText is the old-school way of retrieving response (supported by IE8 & 9)
@ -65,7 +65,7 @@ export class XHRConnection implements Connection {
}; };
// error event handler // error event handler
let onError = (err) => { let onError = (err) => {
var responseOptions = new ResponseOptions({body: err, type: ResponseTypes.Error}); var responseOptions = new ResponseOptions({body: err, type: ResponseType.Error});
if (isPresent(baseResponseOptions)) { if (isPresent(baseResponseOptions)) {
responseOptions = baseResponseOptions.merge(responseOptions); responseOptions = baseResponseOptions.merge(responseOptions);
} }

View File

@ -1,6 +1,6 @@
import {isPresent, isString} from 'angular2/src/facade/lang'; import {isPresent, isString} from 'angular2/src/facade/lang';
import {Headers} from './headers'; import {Headers} from './headers';
import {RequestMethods} from './enums'; import {RequestMethod} from './enums';
import {RequestOptionsArgs} from './interfaces'; import {RequestOptionsArgs} from './interfaces';
import {Injectable} from 'angular2/core'; import {Injectable} from 'angular2/core';
import {URLSearchParams} from './url_search_params'; import {URLSearchParams} from './url_search_params';
@ -19,23 +19,23 @@ import {normalizeMethodName} from './http_utils';
* ### Example ([live demo](http://plnkr.co/edit/7Wvi3lfLq41aQPKlxB4O?p=preview)) * ### Example ([live demo](http://plnkr.co/edit/7Wvi3lfLq41aQPKlxB4O?p=preview))
* *
* ```typescript * ```typescript
* import {RequestOptions, Request, RequestMethods} from 'angular2/http'; * import {RequestOptions, Request, RequestMethod} from 'angular2/http';
* *
* var options = new RequestOptions({ * var options = new RequestOptions({
* method: RequestMethods.Post, * method: RequestMethod.Post,
* url: 'https://google.com' * url: 'https://google.com'
* }); * });
* var req = new Request(options); * var req = new Request(options);
* console.log('req.method:', RequestMethods[req.method]); // Post * console.log('req.method:', RequestMethod[req.method]); // Post
* console.log('options.url:', options.url); // https://google.com * console.log('options.url:', options.url); // https://google.com
* ``` * ```
*/ */
export class RequestOptions { export class RequestOptions {
/** /**
* Http method with which to execute a {@link Request}. * Http method with which to execute a {@link Request}.
* Acceptable methods are defined in the {@link RequestMethods} enum. * Acceptable methods are defined in the {@link RequestMethod} enum.
*/ */
method: RequestMethods | string; method: RequestMethod | string;
/** /**
* {@link Headers} to be attached to a {@link Request}. * {@link Headers} to be attached to a {@link Request}.
*/ */
@ -75,15 +75,15 @@ export class RequestOptions {
* ### Example ([live demo](http://plnkr.co/edit/6w8XA8YTkDRcPYpdB9dk?p=preview)) * ### Example ([live demo](http://plnkr.co/edit/6w8XA8YTkDRcPYpdB9dk?p=preview))
* *
* ```typescript * ```typescript
* import {RequestOptions, Request, RequestMethods} from 'angular2/http'; * import {RequestOptions, Request, RequestMethod} from 'angular2/http';
* *
* var options = new RequestOptions({ * var options = new RequestOptions({
* method: RequestMethods.Post * method: RequestMethod.Post
* }); * });
* var req = new Request(options.merge({ * var req = new Request(options.merge({
* url: 'https://google.com' * url: 'https://google.com'
* })); * }));
* console.log('req.method:', RequestMethods[req.method]); // Post * console.log('req.method:', RequestMethod[req.method]); // Post
* console.log('options.url:', options.url); // null * console.log('options.url:', options.url); // null
* console.log('req.url:', req.url); // https://google.com * console.log('req.url:', req.url); // https://google.com
* ``` * ```
@ -107,7 +107,7 @@ export class RequestOptions {
* Subclass of {@link RequestOptions}, with default values. * Subclass of {@link RequestOptions}, with default values.
* *
* Default values: * Default values:
* * method: {@link RequestMethods RequestMethods.Get} * * method: {@link RequestMethod RequestMethod.Get}
* * headers: empty {@link Headers} object * * headers: empty {@link Headers} object
* *
* This class could be extended and bound to the {@link RequestOptions} class * This class could be extended and bound to the {@link RequestOptions} class
@ -134,19 +134,19 @@ export class RequestOptions {
* ### Example ([live demo](http://plnkr.co/edit/oyBoEvNtDhOSfi9YxaVb?p=preview)) * ### Example ([live demo](http://plnkr.co/edit/oyBoEvNtDhOSfi9YxaVb?p=preview))
* *
* ``` * ```
* import {BaseRequestOptions, Request, RequestMethods} from 'angular2/http'; * import {BaseRequestOptions, Request, RequestMethod} from 'angular2/http';
* *
* var options = new BaseRequestOptions(); * var options = new BaseRequestOptions();
* var req = new Request(options.merge({ * var req = new Request(options.merge({
* method: RequestMethods.Post, * method: RequestMethod.Post,
* url: 'https://google.com' * url: 'https://google.com'
* })); * }));
* console.log('req.method:', RequestMethods[req.method]); // Post * console.log('req.method:', RequestMethod[req.method]); // Post
* console.log('options.url:', options.url); // null * console.log('options.url:', options.url); // null
* console.log('req.url:', req.url); // https://google.com * console.log('req.url:', req.url); // https://google.com
* ``` * ```
*/ */
@Injectable() @Injectable()
export class BaseRequestOptions extends RequestOptions { export class BaseRequestOptions extends RequestOptions {
constructor() { super({method: RequestMethods.Get, headers: new Headers()}); } constructor() { super({method: RequestMethod.Get, headers: new Headers()}); }
} }

View File

@ -1,7 +1,7 @@
import {Injectable} from 'angular2/core'; import {Injectable} from 'angular2/core';
import {isPresent, isJsObject} from 'angular2/src/facade/lang'; import {isPresent, isJsObject} from 'angular2/src/facade/lang';
import {Headers} from './headers'; import {Headers} from './headers';
import {ResponseTypes} from './enums'; import {ResponseType} from './enums';
import {ResponseOptionsArgs} from './interfaces'; import {ResponseOptionsArgs} from './interfaces';
/** /**
@ -52,7 +52,7 @@ export class ResponseOptions {
/** /**
* @internal * @internal
*/ */
type: ResponseTypes; type: ResponseType;
url: string; url: string;
constructor({body, status, headers, statusText, type, url}: ResponseOptionsArgs = {}) { constructor({body, status, headers, statusText, type, url}: ResponseOptionsArgs = {}) {
this.body = isPresent(body) ? body : null; this.body = isPresent(body) ? body : null;
@ -147,6 +147,6 @@ export class ResponseOptions {
@Injectable() @Injectable()
export class BaseResponseOptions extends ResponseOptions { export class BaseResponseOptions extends ResponseOptions {
constructor() { constructor() {
super({status: 200, statusText: 'Ok', type: ResponseTypes.Default, headers: new Headers()}); super({status: 200, statusText: 'Ok', type: ResponseType.Default, headers: new Headers()});
} }
} }

View File

@ -3,7 +3,7 @@ import {StringMapWrapper} from 'angular2/src/facade/collection';
/** /**
* Supported http methods. * Supported http methods.
*/ */
export enum RequestMethods { export enum RequestMethod {
Get, Get,
Post, Post,
Put, Put,
@ -18,7 +18,7 @@ export enum RequestMethods {
* [States](http://www.w3.org/TR/XMLHttpRequest/#states) from the `XMLHttpRequest` spec, but with an * [States](http://www.w3.org/TR/XMLHttpRequest/#states) from the `XMLHttpRequest` spec, but with an
* additional "CANCELLED" state. * additional "CANCELLED" state.
*/ */
export enum ReadyStates { export enum ReadyState {
Unsent, Unsent,
Open, Open,
HeadersReceived, HeadersReceived,
@ -31,7 +31,7 @@ export enum ReadyStates {
* Acceptable response types to be associated with a {@link Response}, based on * Acceptable response types to be associated with a {@link Response}, based on
* [ResponseType](https://fetch.spec.whatwg.org/#responsetype) from the Fetch spec. * [ResponseType](https://fetch.spec.whatwg.org/#responsetype) from the Fetch spec.
*/ */
export enum ResponseTypes { export enum ResponseType {
Basic, Basic,
Cors, Cors,
Default, Default,

View File

@ -5,7 +5,7 @@ import {RequestOptionsArgs, Connection, ConnectionBackend} from './interfaces';
import {Request} from './static_request'; import {Request} from './static_request';
import {Response} from './static_response'; import {Response} from './static_response';
import {BaseRequestOptions, RequestOptions} from './base_request_options'; import {BaseRequestOptions, RequestOptions} from './base_request_options';
import {RequestMethods} from './enums'; import {RequestMethod} from './enums';
import {Observable} from 'angular2/core'; import {Observable} from 'angular2/core';
function httpRequest(backend: ConnectionBackend, request: Request): Observable<Response> { function httpRequest(backend: ConnectionBackend, request: Request): Observable<Response> {
@ -103,7 +103,7 @@ export class Http {
if (isString(url)) { if (isString(url)) {
responseObservable = httpRequest( responseObservable = httpRequest(
this._backend, this._backend,
new Request(mergeOptions(this._defaultOptions, options, RequestMethods.Get, url))); new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, url)));
} else if (url instanceof Request) { } else if (url instanceof Request) {
responseObservable = httpRequest(this._backend, url); responseObservable = httpRequest(this._backend, url);
} else { } else {
@ -117,7 +117,7 @@ export class Http {
*/ */
get(url: string, options?: RequestOptionsArgs): Observable<Response> { 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))); RequestMethod.Get, url)));
} }
/** /**
@ -127,7 +127,7 @@ export class Http {
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})),
options, RequestMethods.Post, url))); options, RequestMethod.Post, url)));
} }
/** /**
@ -137,7 +137,7 @@ export class Http {
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})),
options, RequestMethods.Put, url))); options, RequestMethod.Put, url)));
} }
/** /**
@ -145,7 +145,7 @@ export class Http {
*/ */
delete (url: string, options?: RequestOptionsArgs): Observable<Response> { 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))); RequestMethod.Delete, url)));
} }
/** /**
@ -155,7 +155,7 @@ export class Http {
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})),
options, RequestMethods.Patch, url))); options, RequestMethod.Patch, url)));
} }
/** /**
@ -163,7 +163,7 @@ export class Http {
*/ */
head(url: string, options?: RequestOptionsArgs): Observable<Response> { 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))); RequestMethod.Head, url)));
} }
} }
@ -182,10 +182,10 @@ export class Jsonp extends Http {
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> { 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, RequestMethod.Get, url));
} }
if (url instanceof Request) { if (url instanceof Request) {
if (url.method !== RequestMethods.Get) { if (url.method !== RequestMethod.Get) {
makeTypeError('JSONP requests must use GET request method.'); makeTypeError('JSONP requests must use GET request method.');
} }
responseObservable = httpRequest(this._backend, url); responseObservable = httpRequest(this._backend, url);

View File

@ -1,13 +1,13 @@
import {isString} from 'angular2/src/facade/lang'; import {isString} from 'angular2/src/facade/lang';
import {RequestMethods} from './enums'; import {RequestMethod} from './enums';
import {makeTypeError} from 'angular2/src/facade/exceptions'; import {makeTypeError} from 'angular2/src/facade/exceptions';
import {Response} from './static_response'; import {Response} from './static_response';
export function normalizeMethodName(method): RequestMethods { export function normalizeMethodName(method): RequestMethod {
if (isString(method)) { if (isString(method)) {
var originalMethod = method; var originalMethod = method;
method = method.replace(/(\w)(\w*)/g, (g0, g1, g2) => g1.toUpperCase() + g2.toLowerCase()); method = method.replace(/(\w)(\w*)/g, (g0, g1, g2) => g1.toUpperCase() + g2.toLowerCase());
method = RequestMethods[method]; method = RequestMethod[method];
if (typeof method !== 'number') if (typeof method !== 'number')
throw makeTypeError( throw makeTypeError(
`Invalid request method. The method "${originalMethod}" is not supported.`); `Invalid request method. The method "${originalMethod}" is not supported.`);

View File

@ -1,4 +1,4 @@
import {ReadyStates, RequestMethods, ResponseTypes} from './enums'; import {ReadyState, RequestMethod, ResponseType} from './enums';
import {Headers} from './headers'; import {Headers} from './headers';
import {BaseException, WrappedException} from 'angular2/src/facade/exceptions'; import {BaseException, WrappedException} from 'angular2/src/facade/exceptions';
import {EventEmitter} from 'angular2/src/facade/async'; import {EventEmitter} from 'angular2/src/facade/async';
@ -17,7 +17,7 @@ export abstract class ConnectionBackend { abstract createConnection(request: any
* Abstract class from which real connections are derived. * Abstract class from which real connections are derived.
*/ */
export abstract class Connection { export abstract class Connection {
readyState: ReadyStates; readyState: ReadyState;
request: Request; request: Request;
response: any; // TODO: generic of <Response>; response: any; // TODO: generic of <Response>;
} }
@ -28,7 +28,7 @@ export abstract class Connection {
*/ */
export interface RequestOptionsArgs { export interface RequestOptionsArgs {
url?: string; url?: string;
method?: string | RequestMethods; method?: string | RequestMethod;
search?: string | URLSearchParams; search?: string | URLSearchParams;
headers?: Headers; headers?: Headers;
// TODO: Support Blob, ArrayBuffer, JSON, URLSearchParams, FormData // TODO: Support Blob, ArrayBuffer, JSON, URLSearchParams, FormData
@ -50,6 +50,6 @@ export type ResponseOptionsArgs = {
status?: number; status?: number;
statusText?: string; statusText?: string;
headers?: Headers; headers?: Headers;
type?: ResponseTypes; type?: ResponseType;
url?: string; url?: string;
} }

View File

@ -1,4 +1,4 @@
import {RequestMethods} from './enums'; import {RequestMethod} from './enums';
import {RequestArgs} from './interfaces'; import {RequestArgs} from './interfaces';
import {Headers} from './headers'; import {Headers} from './headers';
import {normalizeMethodName} from './http_utils'; import {normalizeMethodName} from './http_utils';
@ -26,14 +26,14 @@ import {
* *
* ```typescript * ```typescript
* import {Injectable, Injector} from 'angular2/angular2'; * import {Injectable, Injector} from 'angular2/angular2';
* import {HTTP_PROVIDERS, Http, Request, RequestMethods} from 'angular2/http'; * import {HTTP_PROVIDERS, Http, Request, RequestMethod} from 'angular2/http';
* *
* @Injectable() * @Injectable()
* class AutoAuthenticator { * class AutoAuthenticator {
* constructor(public http:Http) {} * constructor(public http:Http) {}
* request(url:string) { * request(url:string) {
* return this.http.request(new Request({ * return this.http.request(new Request({
* method: RequestMethods.Get, * method: RequestMethod.Get,
* url: url, * url: url,
* search: 'password=123' * search: 'password=123'
* })); * }));
@ -52,7 +52,7 @@ export class Request {
/** /**
* Http method with which to perform the request. * Http method with which to perform the request.
*/ */
method: RequestMethods; method: RequestMethod;
/** /**
* {@link Headers} instance * {@link Headers} instance
*/ */

View File

@ -1,4 +1,4 @@
import {ResponseTypes} from './enums'; import {ResponseType} from './enums';
import {CONST_EXPR, isString, isPresent, Json} from 'angular2/src/facade/lang'; import {CONST_EXPR, isString, isPresent, Json} from 'angular2/src/facade/lang';
import {BaseException, WrappedException} from 'angular2/src/facade/exceptions'; import {BaseException, WrappedException} from 'angular2/src/facade/exceptions';
import {Headers} from './headers'; import {Headers} from './headers';
@ -29,7 +29,7 @@ export class Response {
* *
* Defaults to "default". * Defaults to "default".
*/ */
type: ResponseTypes; type: ResponseType;
/** /**
* True if the response's status is within 200-299 * True if the response's status is within 200-299
*/ */

View File

@ -27,7 +27,7 @@ import {Response} from 'angular2/src/http/static_response';
import {Map} from 'angular2/src/facade/collection'; import {Map} from 'angular2/src/facade/collection';
import {RequestOptions, BaseRequestOptions} from 'angular2/src/http/base_request_options'; import {RequestOptions, BaseRequestOptions} from 'angular2/src/http/base_request_options';
import {BaseResponseOptions, ResponseOptions} from 'angular2/src/http/base_response_options'; import {BaseResponseOptions, ResponseOptions} from 'angular2/src/http/base_response_options';
import {ResponseTypes, ReadyStates, RequestMethods} from 'angular2/src/http/enums'; import {ResponseType, ReadyState, RequestMethod} from 'angular2/src/http/enums';
var addEventListenerSpy; var addEventListenerSpy;
var existingScripts = []; var existingScripts = [];
@ -94,9 +94,9 @@ export function main() {
it('should use the injected BaseResponseOptions to create the response', it('should use the injected BaseResponseOptions to create the response',
inject([AsyncTestCompleter], async => { inject([AsyncTestCompleter], async => {
let connection = new JSONPConnection_(sampleRequest, new MockBrowserJsonp(), let connection = new JSONPConnection_(sampleRequest, new MockBrowserJsonp(),
new ResponseOptions({type: ResponseTypes.Error})); new ResponseOptions({type: ResponseType.Error}));
connection.response.subscribe(res => { connection.response.subscribe(res => {
expect(res.type).toBe(ResponseTypes.Error); expect(res.type).toBe(ResponseType.Error);
async.done(); async.done();
}); });
connection.finished(); connection.finished();
@ -117,7 +117,7 @@ export function main() {
existingScripts[0].dispatchEvent('load'); existingScripts[0].dispatchEvent('load');
TimerWrapper.setTimeout(() => { TimerWrapper.setTimeout(() => {
expect(connection.readyState).toBe(ReadyStates.Cancelled); expect(connection.readyState).toBe(ReadyState.Cancelled);
expect(loadSpy).not.toHaveBeenCalled(); expect(loadSpy).not.toHaveBeenCalled();
expect(errorSpy).not.toHaveBeenCalled(); expect(errorSpy).not.toHaveBeenCalled();
expect(returnSpy).not.toHaveBeenCalled(); expect(returnSpy).not.toHaveBeenCalled();
@ -158,8 +158,8 @@ export function main() {
})); }));
it('should throw if request method is not GET', () => { it('should throw if request method is not GET', () => {
[RequestMethods.Post, RequestMethods.Put, RequestMethods.Delete, RequestMethods.Options, [RequestMethod.Post, RequestMethod.Put, RequestMethod.Delete, RequestMethod.Options,
RequestMethods.Head, RequestMethods.Patch] RequestMethod.Head, RequestMethod.Patch]
.forEach(method => { .forEach(method => {
let base = new BaseRequestOptions(); let base = new BaseRequestOptions();
let req = new Request( let req = new Request(

View File

@ -21,7 +21,7 @@ import {Headers} from 'angular2/src/http/headers';
import {Map} from 'angular2/src/facade/collection'; import {Map} from 'angular2/src/facade/collection';
import {RequestOptions, BaseRequestOptions} from 'angular2/src/http/base_request_options'; import {RequestOptions, BaseRequestOptions} from 'angular2/src/http/base_request_options';
import {BaseResponseOptions, ResponseOptions} from 'angular2/src/http/base_response_options'; import {BaseResponseOptions, ResponseOptions} from 'angular2/src/http/base_response_options';
import {ResponseTypes} from 'angular2/src/http/enums'; import {ResponseType} from 'angular2/src/http/enums';
export function main() { export function main() {
describe('MockBackend', () => { describe('MockBackend', () => {

View File

@ -21,7 +21,7 @@ import {Headers} from 'angular2/src/http/headers';
import {Map} from 'angular2/src/facade/collection'; import {Map} from 'angular2/src/facade/collection';
import {RequestOptions, BaseRequestOptions} from 'angular2/src/http/base_request_options'; import {RequestOptions, BaseRequestOptions} from 'angular2/src/http/base_request_options';
import {BaseResponseOptions, ResponseOptions} from 'angular2/src/http/base_response_options'; import {BaseResponseOptions, ResponseOptions} from 'angular2/src/http/base_response_options';
import {ResponseTypes} from 'angular2/src/http/enums'; import {ResponseType} from 'angular2/src/http/enums';
var abortSpy; var abortSpy;
var sendSpy; var sendSpy;
@ -104,9 +104,9 @@ export function main() {
it('should use the injected BaseResponseOptions to create the response', it('should use the injected BaseResponseOptions to create the response',
inject([AsyncTestCompleter], async => { inject([AsyncTestCompleter], async => {
var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(), var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(),
new ResponseOptions({type: ResponseTypes.Error})); new ResponseOptions({type: ResponseType.Error}));
connection.response.subscribe(res => { connection.response.subscribe(res => {
expect(res.type).toBe(ResponseTypes.Error); expect(res.type).toBe(ResponseType.Error);
async.done(); async.done();
}); });
existingXHRs[0].setStatusCode(200); existingXHRs[0].setStatusCode(200);
@ -115,8 +115,8 @@ export function main() {
it('should complete a request', inject([AsyncTestCompleter], async => { it('should complete a request', inject([AsyncTestCompleter], async => {
var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(), var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(),
new ResponseOptions({type: ResponseTypes.Error})); new ResponseOptions({type: ResponseType.Error}));
connection.response.subscribe(res => { expect(res.type).toBe(ResponseTypes.Error); }, connection.response.subscribe(res => { expect(res.type).toBe(ResponseType.Error); },
null, () => { async.done(); }); null, () => { async.done(); });
existingXHRs[0].setStatusCode(200); existingXHRs[0].setStatusCode(200);
existingXHRs[0].dispatchEvent('load'); existingXHRs[0].dispatchEvent('load');
@ -131,9 +131,9 @@ export function main() {
it('should create an error Response on error', inject([AsyncTestCompleter], async => { it('should create an error Response on error', inject([AsyncTestCompleter], async => {
var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(), var connection = new XHRConnection(sampleRequest, new MockBrowserXHR(),
new ResponseOptions({type: ResponseTypes.Error})); new ResponseOptions({type: ResponseType.Error}));
connection.response.subscribe(null, res => { connection.response.subscribe(null, res => {
expect(res.type).toBe(ResponseTypes.Error); expect(res.type).toBe(ResponseType.Error);
async.done(); async.done();
}); });
existingXHRs[0].dispatchEvent('error'); existingXHRs[0].dispatchEvent('error');

View File

@ -10,21 +10,21 @@ import {
xit xit
} from 'angular2/testing_internal'; } from 'angular2/testing_internal';
import {BaseRequestOptions, RequestOptions} from 'angular2/src/http/base_request_options'; import {BaseRequestOptions, RequestOptions} from 'angular2/src/http/base_request_options';
import {RequestMethods} from 'angular2/src/http/enums'; import {RequestMethod} from 'angular2/src/http/enums';
export function main() { export function main() {
describe('BaseRequestOptions', () => { describe('BaseRequestOptions', () => {
it('should create a new object when calling merge', () => { it('should create a new object when calling merge', () => {
var options1 = new BaseRequestOptions(); var options1 = new BaseRequestOptions();
var options2 = options1.merge(new RequestOptions({method: RequestMethods.Delete})); var options2 = options1.merge(new RequestOptions({method: RequestMethod.Delete}));
expect(options2).not.toBe(options1); expect(options2).not.toBe(options1);
expect(options2.method).toBe(RequestMethods.Delete); expect(options2.method).toBe(RequestMethod.Delete);
}); });
it('should retain previously merged values when merging again', () => { it('should retain previously merged values when merging again', () => {
var options1 = new BaseRequestOptions(); var options1 = new BaseRequestOptions();
var options2 = options1.merge(new RequestOptions({method: RequestMethods.Delete})); var options2 = options1.merge(new RequestOptions({method: RequestMethod.Delete}));
expect(options2.method).toBe(RequestMethods.Delete); expect(options2.method).toBe(RequestMethod.Delete);
}); });
}); });
} }

View File

@ -16,7 +16,7 @@ import {
BaseRequestOptions, BaseRequestOptions,
ConnectionBackend, ConnectionBackend,
Request, Request,
RequestMethods, RequestMethod,
RequestOptions, RequestOptions,
Response, Response,
ResponseOptions, ResponseOptions,
@ -143,12 +143,12 @@ export function main() {
inject([AsyncTestCompleter], (async) => { inject([AsyncTestCompleter], (async) => {
backend.connections.subscribe(c => { backend.connections.subscribe(c => {
expect(c.request.url).toBe('https://google.com'); expect(c.request.url).toBe('https://google.com');
expect(c.request.method).toBe(RequestMethods.Post); expect(c.request.method).toBe(RequestMethod.Post);
c.mockRespond(new Response(new ResponseOptions({body: 'Thank you'}))); c.mockRespond(new Response(new ResponseOptions({body: 'Thank you'})));
async.done(); async.done();
}); });
http.request(new Request(new RequestOptions( http.request(new Request(new RequestOptions(
{url: 'https://google.com', method: RequestMethods.Post}))) {url: 'https://google.com', method: RequestMethod.Post})))
.subscribe((res) => {}); .subscribe((res) => {});
})); }));
@ -166,10 +166,10 @@ export function main() {
it('should perform a post request for given url if options include a method', it('should perform a post request for given url if options include a method',
inject([AsyncTestCompleter], (async) => { inject([AsyncTestCompleter], (async) => {
backend.connections.subscribe(c => { backend.connections.subscribe(c => {
expect(c.request.method).toEqual(RequestMethods.Post); expect(c.request.method).toEqual(RequestMethod.Post);
c.mockRespond(baseResponse); c.mockRespond(baseResponse);
}); });
let requestOptions = new RequestOptions({method: RequestMethods.Post}); let requestOptions = new RequestOptions({method: RequestMethod.Post});
http.request('http://basic.connection', requestOptions) http.request('http://basic.connection', requestOptions)
.subscribe(res => { .subscribe(res => {
expect(res.text()).toBe('base response'); expect(res.text()).toBe('base response');
@ -180,10 +180,10 @@ export function main() {
it('should perform a post request for given url if options include a method', it('should perform a post request for given url if options include a method',
inject([AsyncTestCompleter], (async) => { inject([AsyncTestCompleter], (async) => {
backend.connections.subscribe(c => { backend.connections.subscribe(c => {
expect(c.request.method).toEqual(RequestMethods.Post); expect(c.request.method).toEqual(RequestMethod.Post);
c.mockRespond(baseResponse); c.mockRespond(baseResponse);
}); });
let requestOptions = {method: RequestMethods.Post}; let requestOptions = {method: RequestMethod.Post};
http.request('http://basic.connection', requestOptions) http.request('http://basic.connection', requestOptions)
.subscribe(res => { .subscribe(res => {
expect(res.text()).toBe('base response'); expect(res.text()).toBe('base response');
@ -221,7 +221,7 @@ export function main() {
describe('.get()', () => { describe('.get()', () => {
it('should perform a get request for given url', inject([AsyncTestCompleter], async => { it('should perform a get request for given url', inject([AsyncTestCompleter], async => {
backend.connections.subscribe(c => { backend.connections.subscribe(c => {
expect(c.request.method).toBe(RequestMethods.Get); expect(c.request.method).toBe(RequestMethod.Get);
backend.resolveAllConnections(); backend.resolveAllConnections();
async.done(); async.done();
}); });
@ -233,7 +233,7 @@ export function main() {
describe('.post()', () => { describe('.post()', () => {
it('should perform a post request for given url', inject([AsyncTestCompleter], async => { it('should perform a post request for given url', inject([AsyncTestCompleter], async => {
backend.connections.subscribe(c => { backend.connections.subscribe(c => {
expect(c.request.method).toBe(RequestMethods.Post); expect(c.request.method).toBe(RequestMethod.Post);
backend.resolveAllConnections(); backend.resolveAllConnections();
async.done(); async.done();
}); });
@ -256,7 +256,7 @@ export function main() {
describe('.put()', () => { describe('.put()', () => {
it('should perform a put request for given url', inject([AsyncTestCompleter], async => { it('should perform a put request for given url', inject([AsyncTestCompleter], async => {
backend.connections.subscribe(c => { backend.connections.subscribe(c => {
expect(c.request.method).toBe(RequestMethods.Put); expect(c.request.method).toBe(RequestMethod.Put);
backend.resolveAllConnections(); backend.resolveAllConnections();
async.done(); async.done();
}); });
@ -278,7 +278,7 @@ export function main() {
describe('.delete()', () => { describe('.delete()', () => {
it('should perform a delete request for given url', inject([AsyncTestCompleter], async => { it('should perform a delete request for given url', inject([AsyncTestCompleter], async => {
backend.connections.subscribe(c => { backend.connections.subscribe(c => {
expect(c.request.method).toBe(RequestMethods.Delete); expect(c.request.method).toBe(RequestMethod.Delete);
backend.resolveAllConnections(); backend.resolveAllConnections();
async.done(); async.done();
}); });
@ -290,7 +290,7 @@ export function main() {
describe('.patch()', () => { describe('.patch()', () => {
it('should perform a patch request for given url', inject([AsyncTestCompleter], async => { it('should perform a patch request for given url', inject([AsyncTestCompleter], async => {
backend.connections.subscribe(c => { backend.connections.subscribe(c => {
expect(c.request.method).toBe(RequestMethods.Patch); expect(c.request.method).toBe(RequestMethod.Patch);
backend.resolveAllConnections(); backend.resolveAllConnections();
async.done(); async.done();
}); });
@ -312,7 +312,7 @@ export function main() {
describe('.head()', () => { describe('.head()', () => {
it('should perform a head request for given url', inject([AsyncTestCompleter], async => { it('should perform a head request for given url', inject([AsyncTestCompleter], async => {
backend.connections.subscribe(c => { backend.connections.subscribe(c => {
expect(c.request.method).toBe(RequestMethods.Head); expect(c.request.method).toBe(RequestMethod.Head);
backend.resolveAllConnections(); backend.resolveAllConnections();
async.done(); async.done();
}); });
@ -363,7 +363,7 @@ export function main() {
inject([AsyncTestCompleter], (async) => { inject([AsyncTestCompleter], (async) => {
backend.connections.subscribe(c => { backend.connections.subscribe(c => {
expect(c.request.method) expect(c.request.method)
.toBe(RequestMethods.Post) .toBe(RequestMethod.Post)
c.mockRespond(new Response(new ResponseOptions({body: 'Thank you'}))); c.mockRespond(new Response(new ResponseOptions({body: 'Thank you'})));
async.done(); async.done();
}); });