body.js
2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* @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
*/
import { stringToArrayBuffer } from './http_utils';
import { URLSearchParams } from './url_search_params';
/**
* HTTP request body used by both {\@link Request} and {\@link Response}
* https://fetch.spec.whatwg.org/#body
* @abstract
*/
export var Body = (function () {
function Body() {
}
/**
* Attempts to return body as parsed `JSON` object, or raises an exception.
* @return {?}
*/
Body.prototype.json = function () {
if (typeof this._body === 'string') {
return JSON.parse(/** @type {?} */ (this._body));
}
if (this._body instanceof ArrayBuffer) {
return JSON.parse(this.text());
}
return this._body;
};
/**
* Returns the body as a string, presuming `toString()` can be called on the response body.
* @return {?}
*/
Body.prototype.text = function () {
if (this._body instanceof URLSearchParams) {
return this._body.toString();
}
if (this._body instanceof ArrayBuffer) {
return String.fromCharCode.apply(null, new Uint16Array(/** @type {?} */ (this._body)));
}
if (this._body == null) {
return '';
}
if (typeof this._body === 'object') {
return JSON.stringify(this._body, null, 2);
}
return this._body.toString();
};
/**
* Return the body as an ArrayBuffer
* @return {?}
*/
Body.prototype.arrayBuffer = function () {
if (this._body instanceof ArrayBuffer) {
return (this._body);
}
return stringToArrayBuffer(this.text());
};
/**
* Returns the request's body as a Blob, assuming that body exists.
* @return {?}
*/
Body.prototype.blob = function () {
if (this._body instanceof Blob) {
return (this._body);
}
if (this._body instanceof ArrayBuffer) {
return new Blob([this._body]);
}
throw new Error('The request body isn\'t either a blob or an array buffer');
};
return Body;
}());
function Body_tsickle_Closure_declarations() {
/**
* \@internal
* @type {?}
*/
Body.prototype._body;
}
//# sourceMappingURL=body.js.map