2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
import {Injectable} from '@angular/core';
|
2016-06-08 19:38:52 -04:00
|
|
|
|
2016-09-27 20:12:25 -04:00
|
|
|
import {isPresent} from '../src/facade/lang';
|
2016-06-08 19:38:52 -04:00
|
|
|
|
2015-12-03 16:44:14 -05:00
|
|
|
import {ResponseType} from './enums';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {Headers} from './headers';
|
2015-08-11 18:01:29 -04:00
|
|
|
import {ResponseOptionsArgs} from './interfaces';
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
|
2015-06-24 03:27:07 -04:00
|
|
|
/**
|
2015-09-22 18:50:49 -04:00
|
|
|
* Creates a response options object to be optionally provided when instantiating a
|
2015-06-24 03:27:07 -04:00
|
|
|
* {@link Response}.
|
|
|
|
*
|
2015-09-22 18:50:49 -04:00
|
|
|
* This class is based on the `ResponseInit` description in the [Fetch
|
|
|
|
* Spec](https://fetch.spec.whatwg.org/#responseinit).
|
|
|
|
*
|
|
|
|
* All values are null by default. Typical defaults can be found in the
|
|
|
|
* {@link BaseResponseOptions} class, which sub-classes `ResponseOptions`.
|
|
|
|
*
|
|
|
|
* This class may be used in tests to build {@link Response Responses} for
|
|
|
|
* mock responses (see {@link MockBackend}).
|
|
|
|
*
|
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/P9Jkk8e8cz6NVzbcxEsD?p=preview))
|
|
|
|
*
|
|
|
|
* ```typescript
|
2016-04-28 20:50:03 -04:00
|
|
|
* import {ResponseOptions, Response} from '@angular/http';
|
2015-09-22 18:50:49 -04:00
|
|
|
*
|
|
|
|
* var options = new ResponseOptions({
|
|
|
|
* body: '{"name":"Jeff"}'
|
|
|
|
* });
|
|
|
|
* var res = new Response(options);
|
|
|
|
*
|
|
|
|
* console.log('res.json():', res.json()); // Object {name: "Jeff"}
|
|
|
|
* ```
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
|
|
|
* @experimental
|
2015-06-24 03:27:07 -04:00
|
|
|
*/
|
2015-08-11 18:01:29 -04:00
|
|
|
export class ResponseOptions {
|
2016-02-24 10:37:18 -05:00
|
|
|
// TODO: FormData | Blob
|
2015-09-22 18:50:49 -04:00
|
|
|
/**
|
2016-07-21 16:44:38 -04:00
|
|
|
* String, Object, ArrayBuffer or Blob representing the body of the {@link Response}.
|
2015-09-22 18:50:49 -04:00
|
|
|
*/
|
2016-07-21 16:44:38 -04:00
|
|
|
body: string|Object|ArrayBuffer|Blob;
|
2015-09-22 18:50:49 -04:00
|
|
|
/**
|
|
|
|
* Http {@link http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html status code}
|
|
|
|
* associated with the response.
|
|
|
|
*/
|
2015-06-24 03:27:07 -04:00
|
|
|
status: number;
|
2015-09-22 18:50:49 -04:00
|
|
|
/**
|
|
|
|
* Response {@link Headers headers}
|
|
|
|
*/
|
2015-06-24 03:27:07 -04:00
|
|
|
headers: Headers;
|
2015-09-22 18:50:49 -04:00
|
|
|
/**
|
2015-10-02 16:32:48 -04:00
|
|
|
* @internal
|
2015-09-22 18:50:49 -04:00
|
|
|
*/
|
2015-06-24 03:27:07 -04:00
|
|
|
statusText: string;
|
2015-09-22 18:50:49 -04:00
|
|
|
/**
|
2015-10-02 16:32:48 -04:00
|
|
|
* @internal
|
2015-09-22 18:50:49 -04:00
|
|
|
*/
|
2015-12-03 16:44:14 -05:00
|
|
|
type: ResponseType;
|
2015-06-24 03:27:07 -04:00
|
|
|
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-09-22 18:50:49 -04:00
|
|
|
/**
|
|
|
|
* Creates a copy of the `ResponseOptions` instance, using the optional input as values to
|
|
|
|
* override
|
|
|
|
* existing values. This method will not change the values of the instance on which it is being
|
|
|
|
* called.
|
|
|
|
*
|
|
|
|
* This may be useful when sharing a base `ResponseOptions` object inside tests,
|
|
|
|
* where certain properties may change from test to test.
|
|
|
|
*
|
2015-10-19 10:37:32 -04:00
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/1lXquqFfgduTFBWjNoRE?p=preview))
|
2015-09-22 18:50:49 -04:00
|
|
|
*
|
|
|
|
* ```typescript
|
2016-04-28 20:50:03 -04:00
|
|
|
* import {ResponseOptions, Response} from '@angular/http';
|
2015-09-22 18:50:49 -04:00
|
|
|
*
|
|
|
|
* var options = new ResponseOptions({
|
|
|
|
* body: {name: 'Jeff'}
|
|
|
|
* });
|
|
|
|
* var res = new Response(options.merge({
|
|
|
|
* url: 'https://google.com'
|
|
|
|
* }));
|
|
|
|
* console.log('options.url:', options.url); // null
|
|
|
|
* console.log('res.json():', res.json()); // Object {name: "Jeff"}
|
|
|
|
* console.log('res.url:', res.url); // https://google.com
|
|
|
|
* ```
|
|
|
|
*/
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-22 19:22:49 -04:00
|
|
|
* Subclass of {@link ResponseOptions}, with default values.
|
|
|
|
*
|
|
|
|
* Default values:
|
|
|
|
* * status: 200
|
|
|
|
* * headers: empty {@link Headers} object
|
|
|
|
*
|
|
|
|
* This class could be extended and bound to the {@link ResponseOptions} class
|
|
|
|
* when configuring an {@link Injector}, in order to override the default options
|
|
|
|
* used by {@link Http} to create {@link Response Responses}.
|
|
|
|
*
|
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/qv8DLT?p=preview))
|
|
|
|
*
|
2015-09-24 04:32:43 -04:00
|
|
|
* ```typescript
|
2016-04-28 20:50:03 -04:00
|
|
|
* import {provide} from '@angular/core';
|
|
|
|
* import {bootstrap} from '@angular/platform-browser/browser';
|
2015-10-11 01:11:13 -04:00
|
|
|
* import {HTTP_PROVIDERS, Headers, Http, BaseResponseOptions, ResponseOptions} from
|
2016-04-28 20:50:03 -04:00
|
|
|
* '@angular/http';
|
2015-09-22 19:22:49 -04:00
|
|
|
* import {App} from './myapp';
|
|
|
|
*
|
|
|
|
* class MyOptions extends BaseResponseOptions {
|
|
|
|
* headers:Headers = new Headers({network: 'github'});
|
|
|
|
* }
|
|
|
|
*
|
2016-06-02 20:30:40 -04:00
|
|
|
* bootstrap(App, [HTTP_PROVIDERS, {provide: ResponseOptions, useClass: MyOptions}]);
|
2015-09-22 19:22:49 -04:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* The options could also be extended when manually creating a {@link Response}
|
|
|
|
* object.
|
|
|
|
*
|
|
|
|
* ### Example ([live demo](http://plnkr.co/edit/VngosOWiaExEtbstDoix?p=preview))
|
|
|
|
*
|
|
|
|
* ```
|
2016-04-28 20:50:03 -04:00
|
|
|
* import {BaseResponseOptions, Response} from '@angular/http';
|
2015-09-22 19:22:49 -04:00
|
|
|
*
|
|
|
|
* var options = new BaseResponseOptions();
|
|
|
|
* var res = new Response(options.merge({
|
2016-04-28 20:50:03 -04:00
|
|
|
* body: 'Angular',
|
2015-09-22 19:22:49 -04:00
|
|
|
* headers: new Headers({framework: 'angular'})
|
|
|
|
* }));
|
|
|
|
* console.log('res.headers.get("framework"):', res.headers.get('framework')); // angular
|
2016-04-28 20:50:03 -04:00
|
|
|
* console.log('res.text():', res.text()); // Angular;
|
2015-09-22 19:22:49 -04:00
|
|
|
* ```
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
|
|
|
* @experimental
|
2015-06-24 03:27:07 -04:00
|
|
|
*/
|
|
|
|
@Injectable()
|
|
|
|
export class BaseResponseOptions extends ResponseOptions {
|
2015-06-19 15:14:12 -04:00
|
|
|
constructor() {
|
2015-12-03 16:44:14 -05:00
|
|
|
super({status: 200, statusText: 'Ok', type: ResponseType.Default, headers: new Headers()});
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
|
|
|
}
|