Skip to content

Commit 93596df

Browse files
committed
feat(BaseRequestOptions): add merge method to make copies of options
1 parent ea27704 commit 93596df

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed
Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
1-
import {CONST_EXPR, CONST} from 'angular2/src/facade/lang';
1+
import {CONST_EXPR, CONST, isPresent} from 'angular2/src/facade/lang';
22
import {Headers} from './headers';
33
import {URLSearchParams} from './url_search_params';
44
import {RequestModesOpts, RequestMethods, RequestCacheOpts, RequestCredentialsOpts} from './enums';
55
import {RequestOptions} from './interfaces';
66
import {Injectable} from 'angular2/di';
7+
import {ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
78

8-
@Injectable()
9-
export class BaseRequestOptions implements RequestOptions {
10-
method: RequestMethods;
9+
export class RequestOptionsClass {
10+
method: RequestMethods = RequestMethods.GET;
1111
headers: Headers;
1212
body: URLSearchParams | FormData | string;
13-
mode: RequestModesOpts;
13+
mode: RequestModesOpts = RequestModesOpts.Cors;
1414
credentials: RequestCredentialsOpts;
1515
cache: RequestCacheOpts;
16+
constructor({method, headers, body, mode, credentials,
17+
cache}: RequestOptions = {method: RequestMethods.GET, mode: RequestModesOpts.Cors}) {
18+
this.method = method;
19+
this.headers = headers;
20+
this.body = body;
21+
this.mode = mode;
22+
this.credentials = credentials;
23+
this.cache = cache;
24+
}
1625

17-
constructor() {
18-
this.method = RequestMethods.GET;
19-
this.mode = RequestModesOpts.Cors;
26+
merge(opts: RequestOptions = {}): RequestOptionsClass {
27+
return new RequestOptionsClass(StringMapWrapper.merge(this, opts));
2028
}
2129
}
30+
31+
@Injectable()
32+
export class BaseRequestOptions extends RequestOptionsClass {
33+
constructor() { super(); }
34+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {
2+
AsyncTestCompleter,
3+
beforeEach,
4+
ddescribe,
5+
describe,
6+
expect,
7+
iit,
8+
inject,
9+
it,
10+
xit
11+
} from 'angular2/test_lib';
12+
import {BaseRequestOptions} from 'angular2/src/http/base_request_options';
13+
import {RequestMethods, RequestModesOpts} from 'angular2/src/http/enums';
14+
15+
export function main() {
16+
describe('BaseRequestOptions', () => {
17+
it('should create a new object when calling merge', () => {
18+
var options1 = new BaseRequestOptions();
19+
var options2 = options1.merge({method: RequestMethods.DELETE});
20+
expect(options2).not.toBe(options1);
21+
expect(options2.method).toBe(RequestMethods.DELETE);
22+
});
23+
24+
it('should retain previously merged values when merging again', () => {
25+
var options1 = new BaseRequestOptions();
26+
var options2 = options1.merge({method: RequestMethods.DELETE});
27+
var options3 = options2.merge({mode: RequestModesOpts.NoCors}) expect(options3.mode)
28+
.toBe(RequestModesOpts.NoCors);
29+
expect(options3.method).toBe(RequestMethods.DELETE);
30+
});
31+
});
32+
}

0 commit comments

Comments
 (0)