2015-06-24 03:27:07 -04:00
|
|
|
import {Injectable} from 'angular2/di';
|
2015-08-20 17:28:25 -04:00
|
|
|
import {isPresent, isJsObject} from 'angular2/src/core/facade/lang';
|
2015-04-29 02:07:55 -04:00
|
|
|
import {Headers} from './headers';
|
|
|
|
import {ResponseTypes} from './enums';
|
2015-08-11 18:01:29 -04:00
|
|
|
import {ResponseOptionsArgs} from './interfaces';
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-24 03:27:07 -04:00
|
|
|
/**
|
|
|
|
* Creates a response options object similar to the
|
|
|
|
* [ResponseInit](https://fetch.spec.whatwg.org/#responseinit) description
|
|
|
|
* in the Fetch
|
|
|
|
* Spec to be optionally provided when instantiating a
|
|
|
|
* {@link Response}.
|
|
|
|
*
|
|
|
|
* All values are null by default.
|
|
|
|
*/
|
2015-08-11 18:01:29 -04:00
|
|
|
export class ResponseOptions {
|
2015-06-24 03:27:07 -04:00
|
|
|
// TODO: ArrayBuffer | FormData | Blob
|
|
|
|
body: string | Object;
|
|
|
|
status: number;
|
|
|
|
headers: Headers;
|
|
|
|
statusText: string;
|
|
|
|
type: ResponseTypes;
|
|
|
|
url: string;
|
2015-08-11 18:01:29 -04:00
|
|
|
constructor({body, status, headers, statusText, type, url}: ResponseOptionsArgs = {}) {
|
2015-06-24 03:27:07 -04:00
|
|
|
this.body = isPresent(body) ? body : null;
|
|
|
|
this.status = isPresent(status) ? status : null;
|
|
|
|
this.headers = isPresent(headers) ? headers : null;
|
|
|
|
this.statusText = isPresent(statusText) ? statusText : null;
|
|
|
|
this.type = isPresent(type) ? type : null;
|
|
|
|
this.url = isPresent(url) ? url : null;
|
|
|
|
}
|
|
|
|
|
2015-08-11 18:01:29 -04:00
|
|
|
merge(options?: ResponseOptionsArgs): ResponseOptions {
|
2015-06-24 03:27:07 -04:00
|
|
|
return new ResponseOptions({
|
|
|
|
body: isPresent(options) && isPresent(options.body) ? options.body : this.body,
|
|
|
|
status: isPresent(options) && isPresent(options.status) ? options.status : this.status,
|
|
|
|
headers: isPresent(options) && isPresent(options.headers) ? options.headers : this.headers,
|
|
|
|
statusText: isPresent(options) && isPresent(options.statusText) ? options.statusText :
|
|
|
|
this.statusText,
|
|
|
|
type: isPresent(options) && isPresent(options.type) ? options.type : this.type,
|
|
|
|
url: isPresent(options) && isPresent(options.url) ? options.url : this.url,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Injectable version of {@link ResponseOptions}, with overridable default values.
|
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class BaseResponseOptions extends ResponseOptions {
|
|
|
|
// TODO: Object | ArrayBuffer | JSON | FormData | Blob
|
2015-06-19 15:14:12 -04:00
|
|
|
body: string | Object | ArrayBuffer | JSON | FormData | Blob;
|
2015-04-29 02:07:55 -04:00
|
|
|
status: number;
|
2015-06-19 15:14:12 -04:00
|
|
|
headers: Headers;
|
2015-04-29 02:07:55 -04:00
|
|
|
statusText: string;
|
|
|
|
type: ResponseTypes;
|
|
|
|
url: string;
|
|
|
|
|
2015-06-19 15:14:12 -04:00
|
|
|
constructor() {
|
2015-06-24 03:27:07 -04:00
|
|
|
super({status: 200, statusText: 'Ok', type: ResponseTypes.Default, headers: new Headers()});
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
|
|
|
}
|