2016-03-07 23:53:23 -05:00
|
|
|
|
|
|
|
|
export default class HTTPResponse {
|
2016-06-25 13:56:02 -04:00
|
|
|
constructor(response, body) {
|
|
|
|
|
let _text, _data;
|
2016-03-07 23:53:23 -05:00
|
|
|
this.status = response.statusCode;
|
2016-06-25 13:56:02 -04:00
|
|
|
this.headers = response.headers || {};
|
|
|
|
|
this.cookies = this.headers["set-cookie"];
|
|
|
|
|
|
|
|
|
|
if (typeof body == 'string') {
|
|
|
|
|
_text = body;
|
|
|
|
|
} else if (Buffer.isBuffer(body)) {
|
|
|
|
|
this.buffer = body;
|
|
|
|
|
} else if (typeof body == 'object') {
|
|
|
|
|
_data = body;
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-24 15:47:41 -05:00
|
|
|
let getText = () => {
|
2016-06-25 13:56:02 -04:00
|
|
|
if (!_text && this.buffer) {
|
|
|
|
|
_text = this.buffer.toString('utf-8');
|
|
|
|
|
} else if (!_text && _data) {
|
|
|
|
|
_text = JSON.stringify(_data);
|
|
|
|
|
}
|
|
|
|
|
return _text;
|
2016-03-07 23:53:23 -05:00
|
|
|
}
|
2016-06-25 13:56:02 -04:00
|
|
|
|
|
|
|
|
let getData = () => {
|
|
|
|
|
if (!_data) {
|
|
|
|
|
try {
|
2016-11-24 15:47:41 -05:00
|
|
|
_data = JSON.parse(getText());
|
|
|
|
|
} catch (e) { /* */ }
|
2016-06-25 13:56:02 -04:00
|
|
|
}
|
|
|
|
|
return _data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'body', {
|
|
|
|
|
get: () => { return body }
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'text', {
|
|
|
|
|
enumerable: true,
|
|
|
|
|
get: getText
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(this, 'data', {
|
|
|
|
|
enumerable: true,
|
|
|
|
|
get: getData
|
|
|
|
|
});
|
2016-03-07 23:53:23 -05:00
|
|
|
}
|
2016-03-08 00:15:17 -05:00
|
|
|
}
|