Skip to content

Commit 2c30e38

Browse files
committed
ES6-ify
1 parent 95e5f02 commit 2c30e38

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

spec/HTTPRequest.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ describe("httpRequest", () => {
156156
done();
157157
})
158158
});
159+
159160
it("should encode a JSON body by default", (done) => {
160-
161161
let options = {
162162
body: {"foo": "bar"},
163163
}
@@ -195,7 +195,7 @@ describe("httpRequest", () => {
195195
body:{"foo": "bar", "bar": "baz"},
196196
headers: {'cOntent-tYpe': 'mime/jpeg'}
197197
}
198-
var result = httpRequest.encodeBody(options);
198+
let result = httpRequest.encodeBody(options);
199199
expect(result.body).toEqual({"foo": "bar", "bar": "baz"});
200200
done();
201201
});

src/cloud-code/HTTPResponse.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ export default class HTTPResponse {
1313
get data() {
1414
if (!this._data) {
1515
try {
16-
this._data = JSON.parse(this.text);
16+
this._data = JSON.parse(this.text);
1717
} catch (e) {}
1818
}
1919
return this._data;
2020
}
21-
}
21+
}

src/cloud-code/httpRequest.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
var request = require("request"),
2-
Parse = require('parse/node').Parse,
3-
HTTPResponse = require('./HTTPResponse').HTTPResponse;
1+
import request from 'request';
2+
import Parse from 'parse/node';
3+
import HTTPResponse from './HTTPResponse';
44

5-
var encodeBody = function(options = {}) {
6-
let body = options.body;
7-
let headers = options.headers || {};
5+
var encodeBody = function({body, headers = {}}) {
86
if (typeof body !== 'object') {
9-
return options;
7+
return {body, headers};
108
}
119
var contentTypeKeys = Object.keys(headers).filter((key) => {
1210
return key.match(/content-type/i) != null;
@@ -15,23 +13,27 @@ var encodeBody = function(options = {}) {
1513
if (contentTypeKeys.length == 0) {
1614
// no content type
1715
try {
18-
options.body = JSON.stringify(body);
19-
options.headers = options.headers || {};
20-
options.headers['Content-Type'] = 'application/json';
16+
body = JSON.stringify(body);
17+
headers['Content-Type'] = 'application/json';
2118
} catch(e) {
2219
// do nothing;
2320
}
24-
} else if (contentTypeKeys.length == 1) {
21+
} else {
22+
/* istanbul ignore next */
23+
if (contentTypeKeys.length > 1) {
24+
console.error('multiple content-type headers are set.');
25+
}
26+
// There maybe many, we'll just take the 1st one
2527
var contentType = contentTypeKeys[0];
2628
if (headers[contentType].match(/application\/json/i)) {
27-
options.body = JSON.stringify(body);
29+
body = JSON.stringify(body);
2830
} else if(headers[contentType].match(/application\/x-www-form-urlencoded/i)) {
29-
options.body = Object.keys(body).map(function(key){
31+
body = Object.keys(body).map(function(key){
3032
return `${key}=${encodeURIComponent(body[key])}`
3133
}).join("&");
3234
}
3335
}
34-
return options;
36+
return {body, headers};
3537
}
3638

3739
module.exports = function(options) {
@@ -43,7 +45,7 @@ module.exports = function(options) {
4345
delete options.success;
4446
delete options.error;
4547
delete options.uri; // not supported
46-
options = encodeBody(options);
48+
options = Object.assign(options, encodeBody(options));
4749
// set follow redirects to false by default
4850
options.followRedirect = options.followRedirects == true;
4951
// force the response as a buffer

0 commit comments

Comments
 (0)