Skip to content

Commit 95e5f02

Browse files
committed
Adds HTTPResponse object and lazy loading text and JSON
1 parent f23f39c commit 95e5f02

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

src/cloud-code/HTTPResponse.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
export default class HTTPResponse {
3+
constructor(response) {
4+
this.status = response.statusCode;
5+
this.headers = response.headers;
6+
this.buffer = response.body;
7+
this.cookies = response.headers["set-cookie"];
8+
}
9+
10+
get text() {
11+
return this.buffer.toString('utf-8');
12+
}
13+
get data() {
14+
if (!this._data) {
15+
try {
16+
this._data = JSON.parse(this.text);
17+
} catch (e) {}
18+
}
19+
return this._data;
20+
}
21+
}

src/cloud-code/httpRequest.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var request = require("request"),
2-
Parse = require('parse/node').Parse;
2+
Parse = require('parse/node').Parse,
3+
HTTPResponse = require('./HTTPResponse').HTTPResponse;
34

45
var encodeBody = function(options = {}) {
56
let body = options.body;
@@ -54,15 +55,8 @@ module.exports = function(options) {
5455
}
5556
return promise.reject(error);
5657
}
57-
var httpResponse = {};
58-
httpResponse.status = response.statusCode;
59-
httpResponse.headers = response.headers;
60-
httpResponse.buffer = response.body;
61-
httpResponse.cookies = response.headers["set-cookie"];
62-
httpResponse.text = response.body.toString('utf-8');
63-
try {
64-
httpResponse.data = JSON.parse(httpResponse.text);
65-
} catch (e) {}
58+
let httpResponse = new HTTPResponse(response);
59+
6660
// Consider <200 && >= 400 as errors
6761
if (httpResponse.status < 200 || httpResponse.status >= 400) {
6862
if (callbacks.error) {

0 commit comments

Comments
 (0)