refactor(Http): rename request options interface

This commit is contained in:
Jeff Cross 2015-06-13 19:48:40 -07:00
parent 70ffd267f8
commit e68e69e7e5
5 changed files with 24 additions and 24 deletions

View File

@ -2,19 +2,21 @@ import {CONST_EXPR, CONST, isPresent} from 'angular2/src/facade/lang';
import {Headers} from './headers';
import {URLSearchParams} from './url_search_params';
import {RequestModesOpts, RequestMethods, RequestCacheOpts, RequestCredentialsOpts} from './enums';
import {RequestOptions} from './interfaces';
import {IRequestOptions} from './interfaces';
import {Injectable} from 'angular2/di';
import {ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
export class RequestOptionsClass implements RequestOptions {
export class RequestOptions implements IRequestOptions {
method: RequestMethods = RequestMethods.GET;
headers: Headers;
body: URLSearchParams | FormData | Blob | string;
mode: RequestModesOpts = RequestModesOpts.Cors;
credentials: RequestCredentialsOpts;
cache: RequestCacheOpts;
constructor({method, headers, body, mode, credentials,
cache}: RequestOptions = {method: RequestMethods.GET, mode: RequestModesOpts.Cors}) {
constructor({method, headers, body, mode, credentials, cache}: IRequestOptions = {
method: RequestMethods.GET,
mode: RequestModesOpts.Cors
}) {
this.method = method;
this.headers = headers;
this.body = body;
@ -23,12 +25,12 @@ export class RequestOptionsClass implements RequestOptions {
this.cache = cache;
}
merge(opts: RequestOptions = {}): RequestOptionsClass {
return new RequestOptionsClass(StringMapWrapper.merge(this, opts));
merge(opts: IRequestOptions = {}): RequestOptions {
return new RequestOptions(StringMapWrapper.merge(this, opts));
}
}
@Injectable()
export class BaseRequestOptions extends RequestOptionsClass {
export class BaseRequestOptions extends RequestOptions {
constructor() { super(); }
}

View File

@ -1,7 +1,7 @@
/// <reference path="../../typings/rx/rx.all.d.ts" />
import {Injectable} from 'angular2/src/di/decorators';
import {RequestOptions, Connection} from './interfaces';
import {IRequestOptions, Connection} from './interfaces';
import {Request} from './static_request';
import {Response} from './static_response';
import {XHRBackend} from './backends/xhr_backend';
@ -61,7 +61,7 @@ function httpRequest(backend: XHRBackend, request: Request) {
export class Http {
constructor(private backend: XHRBackend, private defaultOptions: BaseRequestOptions) {}
request(url: string|Request, options?: RequestOptions): Rx.Observable<Response> {
request(url: string | Request, options?: IRequestOptions): Rx.Observable<Response> {
if (typeof url === 'string') {
return httpRequest(this.backend, new Request(url, this.defaultOptions.merge(options)));
} else if (url instanceof Request) {
@ -69,36 +69,36 @@ export class Http {
}
}
get(url: string, options?: RequestOptions) {
get(url: string, options?: IRequestOptions) {
return httpRequest(this.backend, new Request(url, this.defaultOptions.merge(options)
.merge({method: RequestMethods.GET})));
}
post(url: string, body: URLSearchParams | FormData | Blob | string, options?: RequestOptions) {
post(url: string, body: URLSearchParams | FormData | Blob | string, options?: IRequestOptions) {
return httpRequest(this.backend,
new Request(url, this.defaultOptions.merge(options)
.merge({body: body, method: RequestMethods.POST})));
}
put(url: string, body: URLSearchParams | FormData | Blob | string, options?: RequestOptions) {
put(url: string, body: URLSearchParams | FormData | Blob | string, options?: IRequestOptions) {
return httpRequest(this.backend,
new Request(url, this.defaultOptions.merge(options)
.merge({body: body, method: RequestMethods.PUT})));
}
delete (url: string, options?: RequestOptions) {
delete (url: string, options?: IRequestOptions) {
return httpRequest(this.backend, new Request(url, this.defaultOptions.merge(options)
.merge({method: RequestMethods.DELETE})));
}
patch(url: string, body: URLSearchParams | FormData | Blob | string, options?: RequestOptions) {
patch(url: string, body: URLSearchParams | FormData | Blob | string, options?: IRequestOptions) {
return httpRequest(this.backend,
new Request(url, this.defaultOptions.merge(options)
.merge({body: body, method: RequestMethods.PATCH})));
}
head(url: string, options?: RequestOptions) {
head(url: string, options?: IRequestOptions) {
return httpRequest(this.backend, new Request(url, this.defaultOptions.merge(options)
.merge({method: RequestMethods.HEAD})));
}
@ -111,7 +111,7 @@ if (Rx.hasOwnProperty('default')) {
Observable = Rx.Observable;
}
export function HttpFactory(backend: XHRBackend, defaultOptions: BaseRequestOptions) {
return function(url: string | Request, options?: RequestOptions) {
return function(url: string | Request, options?: IRequestOptions) {
if (typeof url === 'string') {
return httpRequest(backend, new Request(url, defaultOptions.merge(options)));
} else if (url instanceof Request) {

View File

@ -11,7 +11,7 @@ import {
import {Headers} from './headers';
import {URLSearchParams} from './url_search_params';
export interface RequestOptions {
export interface IRequestOptions {
method?: RequestMethods;
headers?: Headers;
body?: URLSearchParams | FormData | Blob | string;
@ -60,4 +60,4 @@ export interface Connection {
// 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?: RequestOptions): Rx.Observable<Response> }
export interface IHttp { (url: string, options?: IRequestOptions): Rx.Observable<Response> }

View File

@ -1,6 +1,6 @@
import {RequestMethods, RequestModesOpts, RequestCredentialsOpts} from './enums';
import {URLSearchParams} from './url_search_params';
import {RequestOptions, Request as IRequest} from './interfaces';
import {IRequestOptions, Request as IRequest} from './interfaces';
import {Headers} from './headers';
import {BaseException, RegExpWrapper} from 'angular2/src/facade/lang';
@ -21,7 +21,7 @@ export class Request implements IRequest {
constructor(public url: string, {body, method = RequestMethods.GET, mode = RequestModesOpts.Cors,
credentials = RequestCredentialsOpts.Omit,
headers = new Headers()}: RequestOptions = {}) {
headers = new Headers()}: IRequestOptions = {}) {
this.body = body;
// Defaults to 'GET', consistent with browser
this.method = method;

View File

@ -124,10 +124,8 @@ export function main() {
it('should accept a fully-qualified request as its only parameter', () => {
var req = new Request('https://google.com');
backend.connections.subscribe(c => {
expect(c.request.url).toBe('https://google.com');
});
http.request(req).subscribe(() =>{});
backend.connections.subscribe(c => { expect(c.request.url).toBe('https://google.com'); });
http.request(req).subscribe(() => {});
});
});