BREAKING_CHANGE Ts2Dart issue: https://github.com/angular/ts2dart/issues/270 TypeScript convention: https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines DartConvertion: https://www.dartlang.org/articles/style-guide/ Rename: - NumberFormatStyle.DECIMAL => NumberFormatStyle.Decimal - NumberFormatStyle.PERCENT => NumberFormatStyle.Percent - NumberFormatStyle.CURRENCY => NumberFormatStyle.Currency - RequestMethods.GET => RequestMethods.Get - RequestMethods.POST => RequestMethods.Post - RequestMethods.PUT => RequestMethods.Put - RequestMethods.DELETE => RequestMethods.Delete - RequestMethods.HEAD => RequestMethods.Head - RequestMethods.PATCH => RequestMethods.Patch - ReadyStates.UNSENT => ReadyStates.Unsent - ReadyStates.OPEN => ReadyStates.Open - ReadyStates.HEADERS_RECEIVED => ReadyStates.HeadersReceived - ReadyStates.LOADING => ReadyStates.Loading - ReadyStates.DONE => ReadyStates.Done - ReadyStates.CANCELLED => ReadyStates.Canceled
98 lines
3.8 KiB
TypeScript
98 lines
3.8 KiB
TypeScript
import {CONST_EXPR, CONST, isPresent, isString} from 'angular2/src/core/facade/lang';
|
|
import {Headers} from './headers';
|
|
import {RequestModesOpts, RequestMethods, RequestCacheOpts, RequestCredentialsOpts} from './enums';
|
|
import {RequestOptionsArgs} from './interfaces';
|
|
import {Injectable} from 'angular2/di';
|
|
import {URLSearchParams} from './url_search_params';
|
|
|
|
/**
|
|
* Creates a request options object similar to the `RequestInit` description
|
|
* in the [Fetch
|
|
* Spec](https://fetch.spec.whatwg.org/#requestinit) to be optionally provided when instantiating a
|
|
* {@link Request}.
|
|
*
|
|
* All values are null by default.
|
|
*/
|
|
export class RequestOptions {
|
|
/**
|
|
* Http method with which to execute the request.
|
|
*
|
|
* Defaults to "GET".
|
|
*/
|
|
method: RequestMethods;
|
|
/**
|
|
* Headers object based on the `Headers` class in the [Fetch
|
|
* Spec](https://fetch.spec.whatwg.org/#headers-class).
|
|
*/
|
|
headers: Headers;
|
|
/**
|
|
* Body to be used when creating the request.
|
|
*/
|
|
// TODO: support FormData, Blob, URLSearchParams
|
|
body: string;
|
|
mode: RequestModesOpts;
|
|
credentials: RequestCredentialsOpts;
|
|
cache: RequestCacheOpts;
|
|
url: string;
|
|
search: URLSearchParams;
|
|
constructor({method, headers, body, mode, credentials, cache, url, search}:
|
|
RequestOptionsArgs = {}) {
|
|
this.method = isPresent(method) ? method : null;
|
|
this.headers = isPresent(headers) ? headers : null;
|
|
this.body = isPresent(body) ? body : null;
|
|
this.mode = isPresent(mode) ? mode : null;
|
|
this.credentials = isPresent(credentials) ? credentials : null;
|
|
this.cache = isPresent(cache) ? cache : null;
|
|
this.url = isPresent(url) ? url : null;
|
|
this.search = isPresent(search) ? (isString(search) ? new URLSearchParams(<string>(search)) :
|
|
<URLSearchParams>(search)) :
|
|
null;
|
|
}
|
|
|
|
/**
|
|
* Creates a copy of the `RequestOptions` instance, using the optional input as values to override
|
|
* existing values.
|
|
*/
|
|
merge(options?: RequestOptionsArgs): RequestOptions {
|
|
return new RequestOptions({
|
|
method: isPresent(options) && isPresent(options.method) ? options.method : this.method,
|
|
headers: isPresent(options) && isPresent(options.headers) ? options.headers : this.headers,
|
|
body: isPresent(options) && isPresent(options.body) ? options.body : this.body,
|
|
mode: isPresent(options) && isPresent(options.mode) ? options.mode : this.mode,
|
|
credentials: isPresent(options) && isPresent(options.credentials) ? options.credentials :
|
|
this.credentials,
|
|
cache: isPresent(options) && isPresent(options.cache) ? options.cache : this.cache,
|
|
url: isPresent(options) && isPresent(options.url) ? options.url : this.url,
|
|
search: isPresent(options) && isPresent(options.search) ?
|
|
(isString(options.search) ? new URLSearchParams(<string>(options.search)) :
|
|
(<URLSearchParams>(options.search)).clone()) :
|
|
this.search
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Injectable version of {@link RequestOptions}, with overridable default values.
|
|
*
|
|
* #Example
|
|
*
|
|
* ```
|
|
* import {Http, BaseRequestOptions, Request} from 'angular2/http';
|
|
* ...
|
|
* class MyComponent {
|
|
* constructor(baseRequestOptions:BaseRequestOptions, http:Http) {
|
|
* var options = baseRequestOptions.merge({body: 'foobar', url: 'https://foo'});
|
|
* var request = new Request(options);
|
|
* http.request(request).subscribe(res => this.bars = res.json());
|
|
* }
|
|
* }
|
|
*
|
|
* ```
|
|
*/
|
|
@Injectable()
|
|
export class BaseRequestOptions extends RequestOptions {
|
|
constructor() {
|
|
super({method: RequestMethods.Get, headers: new Headers(), mode: RequestModesOpts.Cors});
|
|
}
|
|
}
|