2015-04-29 02:07:55 -04:00
|
|
|
import {ResponseTypes} from './enums';
|
2015-09-10 18:25:36 -04:00
|
|
|
import {CONST_EXPR, isString, isPresent, Json} from 'angular2/src/core/facade/lang';
|
|
|
|
import {BaseException, WrappedException} from 'angular2/src/core/facade/exceptions';
|
2015-04-29 02:07:55 -04:00
|
|
|
import {Headers} from './headers';
|
2015-06-24 03:27:07 -04:00
|
|
|
import {ResponseOptions} from './base_response_options';
|
2015-07-14 20:53:04 -04:00
|
|
|
import {isJsObject} from './http_utils';
|
2015-04-29 02:07:55 -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.
|
|
|
|
*/
|
2015-06-24 03:27:07 -04:00
|
|
|
export class Response {
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* One of "basic", "cors", "default", "error, or "opaque".
|
|
|
|
*
|
|
|
|
* Defaults to "default".
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
type: ResponseTypes;
|
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"
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
statusText: string;
|
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.
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
bytesLoaded: number;
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Non-standard property
|
|
|
|
*
|
|
|
|
* Denotes how many bytes are expected in the final response body.
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
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).
|
|
|
|
*/
|
2015-04-29 02:07:55 -04:00
|
|
|
headers: Headers;
|
2015-06-19 15:14:12 -04:00
|
|
|
// TODO: Support ArrayBuffer, JSON, FormData, Blob
|
|
|
|
private _body: string | Object;
|
2015-06-24 03:27:07 -04:00
|
|
|
constructor(responseOptions: ResponseOptions) {
|
|
|
|
this._body = responseOptions.body;
|
|
|
|
this.status = responseOptions.status;
|
|
|
|
this.statusText = responseOptions.statusText;
|
|
|
|
this.headers = responseOptions.headers;
|
|
|
|
this.type = responseOptions.type;
|
|
|
|
this.url = responseOptions.url;
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Not yet implemented
|
|
|
|
*/
|
2015-06-19 15:14:12 -04:00
|
|
|
// TODO: Blob return type
|
|
|
|
blob(): any { throw new BaseException('"blob()" method not implemented on Response superclass'); }
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Attempts to return body as parsed `JSON` object, or raises an exception.
|
|
|
|
*/
|
2015-06-19 15:14:12 -04:00
|
|
|
json(): Object {
|
|
|
|
var jsonResponse;
|
2015-06-09 18:18:57 -04:00
|
|
|
if (isJsObject(this._body)) {
|
2015-06-19 15:14:12 -04:00
|
|
|
jsonResponse = this._body;
|
2015-06-09 18:18:57 -04:00
|
|
|
} else if (isString(this._body)) {
|
2015-06-19 15:14:12 -04:00
|
|
|
jsonResponse = Json.parse(<string>this._body);
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
2015-06-19 15:14:12 -04:00
|
|
|
return jsonResponse;
|
2015-04-29 02:07:55 -04:00
|
|
|
}
|
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Returns the body as a string, presuming `toString()` can be called on the response body.
|
|
|
|
*/
|
|
|
|
text(): string { return this._body.toString(); }
|
2015-04-29 02:07:55 -04:00
|
|
|
|
2015-06-09 18:18:57 -04:00
|
|
|
/**
|
|
|
|
* Not yet implemented
|
|
|
|
*/
|
2015-06-19 15:14:12 -04:00
|
|
|
// TODO: ArrayBuffer return type
|
|
|
|
arrayBuffer(): any {
|
2015-04-29 02:07:55 -04:00
|
|
|
throw new BaseException('"arrayBuffer()" method not implemented on Response superclass');
|
|
|
|
}
|
|
|
|
}
|