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

View File

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

View File

@ -11,7 +11,7 @@ import {
import {Headers} from './headers'; import {Headers} from './headers';
import {URLSearchParams} from './url_search_params'; import {URLSearchParams} from './url_search_params';
export interface RequestOptions { export interface IRequestOptions {
method?: RequestMethods; method?: RequestMethods;
headers?: Headers; headers?: Headers;
body?: URLSearchParams | FormData | Blob | string; 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 // Prefixed as IHttp because used in conjunction with Http class, but interface is callable
// constructor(@Inject(Http) http:IHttp) // 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 {RequestMethods, RequestModesOpts, RequestCredentialsOpts} from './enums';
import {URLSearchParams} from './url_search_params'; 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 {Headers} from './headers';
import {BaseException, RegExpWrapper} from 'angular2/src/facade/lang'; 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, constructor(public url: string, {body, method = RequestMethods.GET, mode = RequestModesOpts.Cors,
credentials = RequestCredentialsOpts.Omit, credentials = RequestCredentialsOpts.Omit,
headers = new Headers()}: RequestOptions = {}) { headers = new Headers()}: IRequestOptions = {}) {
this.body = body; this.body = body;
// Defaults to 'GET', consistent with browser // Defaults to 'GET', consistent with browser
this.method = method; this.method = method;

View File

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