feat(BaseRequestOptions): add merge method to make copies of options
This commit is contained in:
parent
ea27704ea9
commit
93596dff3f
|
@ -1,21 +1,34 @@
|
||||||
import {CONST_EXPR, CONST} from 'angular2/src/facade/lang';
|
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 {RequestOptions} from './interfaces';
|
||||||
import {Injectable} from 'angular2/di';
|
import {Injectable} from 'angular2/di';
|
||||||
|
import {ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
|
||||||
|
|
||||||
@Injectable()
|
export class RequestOptionsClass {
|
||||||
export class BaseRequestOptions implements RequestOptions {
|
method: RequestMethods = RequestMethods.GET;
|
||||||
method: RequestMethods;
|
|
||||||
headers: Headers;
|
headers: Headers;
|
||||||
body: URLSearchParams | FormData | string;
|
body: URLSearchParams | FormData | string;
|
||||||
mode: RequestModesOpts;
|
mode: RequestModesOpts = RequestModesOpts.Cors;
|
||||||
credentials: RequestCredentialsOpts;
|
credentials: RequestCredentialsOpts;
|
||||||
cache: RequestCacheOpts;
|
cache: RequestCacheOpts;
|
||||||
|
constructor({method, headers, body, mode, credentials,
|
||||||
|
cache}: RequestOptions = {method: RequestMethods.GET, mode: RequestModesOpts.Cors}) {
|
||||||
|
this.method = method;
|
||||||
|
this.headers = headers;
|
||||||
|
this.body = body;
|
||||||
|
this.mode = mode;
|
||||||
|
this.credentials = credentials;
|
||||||
|
this.cache = cache;
|
||||||
|
}
|
||||||
|
|
||||||
constructor() {
|
merge(opts: RequestOptions = {}): RequestOptionsClass {
|
||||||
this.method = RequestMethods.GET;
|
return new RequestOptionsClass(StringMapWrapper.merge(this, opts));
|
||||||
this.mode = RequestModesOpts.Cors;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class BaseRequestOptions extends RequestOptionsClass {
|
||||||
|
constructor() { super(); }
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
import {
|
||||||
|
AsyncTestCompleter,
|
||||||
|
beforeEach,
|
||||||
|
ddescribe,
|
||||||
|
describe,
|
||||||
|
expect,
|
||||||
|
iit,
|
||||||
|
inject,
|
||||||
|
it,
|
||||||
|
xit
|
||||||
|
} from 'angular2/test_lib';
|
||||||
|
import {BaseRequestOptions} from 'angular2/src/http/base_request_options';
|
||||||
|
import {RequestMethods, RequestModesOpts} from 'angular2/src/http/enums';
|
||||||
|
|
||||||
|
export function main() {
|
||||||
|
describe('BaseRequestOptions', () => {
|
||||||
|
it('should create a new object when calling merge', () => {
|
||||||
|
var options1 = new BaseRequestOptions();
|
||||||
|
var options2 = options1.merge({method: RequestMethods.DELETE});
|
||||||
|
expect(options2).not.toBe(options1);
|
||||||
|
expect(options2.method).toBe(RequestMethods.DELETE);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should retain previously merged values when merging again', () => {
|
||||||
|
var options1 = new BaseRequestOptions();
|
||||||
|
var options2 = options1.merge({method: RequestMethods.DELETE});
|
||||||
|
var options3 = options2.merge({mode: RequestModesOpts.NoCors}) expect(options3.mode)
|
||||||
|
.toBe(RequestModesOpts.NoCors);
|
||||||
|
expect(options3.method).toBe(RequestMethods.DELETE);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in New Issue