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-09-27 20:12:25 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
|
2015-06-24 03:27:07 -04:00
|
|
|
import {ResponseOptions} from './base_response_options';
|
2016-05-26 18:47:20 -04:00
|
|
|
import {Body} from './body';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {ResponseType} from './enums';
|
|
|
|
import {Headers} from './headers';
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
2015-06-24 03:27:07 -04:00
|
|
|
* Creates `Response` instances from provided values.
|
2015-06-09 18:18:57 -04:00
|
|
|
*
|
|
|
|
* Though this object isn't
|
|
|
|
* usually instantiated by end-users, it is the primary object interacted with when it comes time to
|
|
|
|
* add data to a view.
|
|
|
|
*
|
2015-10-19 10:37:32 -04:00
|
|
|
* ### Example
|
2015-06-09 18:18:57 -04:00
|
|
|
*
|
|
|
|
* ```
|
2015-10-07 06:41:27 -04:00
|
|
|
* http.request('my-friends.txt').subscribe(response => this.friends = response.text());
|
2015-06-09 18:18:57 -04:00
|
|
|
* ```
|
|
|
|
*
|
2015-06-24 03:27:07 -04:00
|
|
|
* The Response's interface is inspired by the Response constructor defined in the [Fetch
|
2015-06-09 18:18:57 -04:00
|
|
|
* Spec](https://fetch.spec.whatwg.org/#response-class), but is considered a static value whose body
|
|
|
|
* can be accessed many times. There are other differences in the implementation, but this is the
|
|
|
|
* most significant.
|
2016-06-27 15:27:23 -04:00
|
|
|
*
|
2018-06-01 16:09:01 -04:00
|
|
|
* @deprecated see https://angular.io/guide/http
|
2015-06-09 18:18:57 -04:00
|
|
|
*/
|
2016-05-26 18:47:20 -04:00
|
|
|
export class Response extends Body {
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
2017-01-12 12:55:49 -05:00
|
|
|
* One of "basic", "cors", "default", "error", or "opaque".
|
2015-06-09 18:18:57 -04:00
|
|
|
*
|
|
|
|
* Defaults to "default".
|
|
|
|
*/
|
2015-12-03 16:44:14 -05:00
|
|
|
type: ResponseType;
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* True if the response's status is within 200-299
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
ok: boolean;
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* URL of response.
|
|
|
|
*
|
|
|
|
* Defaults to empty string.
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
url: string;
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Status code returned by server.
|
|
|
|
*
|
|
|
|
* Defaults to 200.
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
status: number;
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Text representing the corresponding reason phrase to the `status`, as defined in [ietf rfc 2616
|
|
|
|
* section 6.1.1](https://tools.ietf.org/html/rfc2616#section-6.1.1)
|
|
|
|
*
|
|
|
|
* Defaults to "OK"
|
|
|
|
*/
|
2017-04-17 14:12:53 -04:00
|
|
|
statusText: string|null;
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Non-standard property
|
|
|
|
*
|
|
|
|
* Denotes how many of the response body's bytes have been loaded, for example if the response is
|
|
|
|
* the result of a progress event.
|
|
|
|
*/
|
2018-06-18 19:38:33 -04:00
|
|
|
// TODO(issue/24571): remove '!'.
|
|
|
|
bytesLoaded !: number;
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Non-standard property
|
|
|
|
*
|
|
|
|
* Denotes how many bytes are expected in the final response body.
|
|
|
|
*/
|
2018-06-18 19:38:33 -04:00
|
|
|
// TODO(issue/24571): remove '!'.
|
|
|
|
totalBytes !: number;
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Headers object based on the `Headers` class in the [Fetch
|
|
|
|
* Spec](https://fetch.spec.whatwg.org/#headers-class).
|
|
|
|
*/
|
2017-04-17 14:12:53 -04:00
|
|
|
headers: Headers|null;
|
2016-02-24 10:37:18 -05:00
|
|
|
|
2015-06-24 03:27:07 -04:00
|
|
|
constructor(responseOptions: ResponseOptions) {
|
2016-05-26 18:47:20 -04:00
|
|
|
super();
|
2015-06-24 03:27:07 -04:00
|
|
|
this._body = responseOptions.body;
|
2017-04-17 14:12:53 -04:00
|
|
|
this.status = responseOptions.status !;
|
2016-04-13 19:07:20 -04:00
|
|
|
this.ok = (this.status >= 200 && this.status <= 299);
|
2015-06-24 03:27:07 -04:00
|
|
|
this.statusText = responseOptions.statusText;
|
|
|
|
this.headers = responseOptions.headers;
|
2017-04-17 14:12:53 -04:00
|
|
|
this.type = responseOptions.type !;
|
|
|
|
this.url = responseOptions.url !;
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
|
|
|
|
2016-03-09 08:35:49 -05:00
|
|
|
toString(): string {
|
|
|
|
return `Response with status: ${this.status} ${this.statusText} for URL: ${this.url}`;
|
|
|
|
}
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|