Skip to content

Commit 6acb5ce

Browse files
committed
ES6-ify
1 parent 982fc72 commit 6acb5ce

File tree

3 files changed

+25
-26
lines changed

3 files changed

+25
-26
lines changed

spec/HTTPRequest.spec.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,27 +160,24 @@ describe("httpRequest", () => {
160160
done();
161161
})
162162
});
163+
163164
it("should encode a JSON body by default", (done) => {
164-
165165
let options = {
166166
body: {"foo": "bar"},
167167
}
168-
169-
var result = httpRequest.encodeBody(options);
168+
let result = httpRequest.encodeBody(options);
170169
expect(result.body).toEqual('{"foo":"bar"}');
171170
expect(result.headers['Content-Type']).toEqual('application/json');
172171
done();
173172

174173
})
175174

176175
it("should encode a JSON body", (done) => {
177-
178176
let options = {
179177
body: {"foo": "bar"},
180178
headers: {'Content-Type': 'application/json'}
181179
}
182-
183-
var result = httpRequest.encodeBody(options);
180+
let result = httpRequest.encodeBody(options);
184181
expect(result.body).toEqual('{"foo":"bar"}');
185182
done();
186183

@@ -190,7 +187,7 @@ describe("httpRequest", () => {
190187
body: {"foo": "bar", "bar": "baz"},
191188
headers: {'cOntent-tYpe': 'application/x-www-form-urlencoded'}
192189
}
193-
var result = httpRequest.encodeBody(options);
190+
let result = httpRequest.encodeBody(options);
194191
expect(result.body).toEqual("foo=bar&bar=baz");
195192
done();
196193
});
@@ -199,7 +196,7 @@ describe("httpRequest", () => {
199196
body:{"foo": "bar", "bar": "baz"},
200197
headers: {'cOntent-tYpe': 'mime/jpeg'}
201198
}
202-
var result = httpRequest.encodeBody(options);
199+
let result = httpRequest.encodeBody(options);
203200
expect(result.body).toEqual({"foo": "bar", "bar": "baz"});
204201
done();
205202
});

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: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
var request = require("request"),
2-
querystring = require('querystring'),
3-
Parse = require('parse/node').Parse;
4-
HTTPResponse = require('./HTTPResponse').HTTPResponse;
1+
import request from 'request';
2+
import Parse from 'parse/node';
3+
import HTTPResponse from './HTTPResponse';
4+
import querystring from 'querystring';
55

6-
var encodeBody = function(options = {}) {
7-
let body = options.body;
8-
let headers = options.headers || {};
6+
var encodeBody = function({body, headers = {}}) {
97
if (typeof body !== 'object') {
10-
return options;
8+
return {body, headers};
119
}
1210
var contentTypeKeys = Object.keys(headers).filter((key) => {
1311
return key.match(/content-type/i) != null;
@@ -16,23 +14,27 @@ var encodeBody = function(options = {}) {
1614
if (contentTypeKeys.length == 0) {
1715
// no content type
1816
try {
19-
options.body = JSON.stringify(body);
20-
options.headers = options.headers || {};
21-
options.headers['Content-Type'] = 'application/json';
17+
body = JSON.stringify(body);
18+
headers['Content-Type'] = 'application/json';
2219
} catch(e) {
2320
// do nothing;
2421
}
25-
} else if (contentTypeKeys.length == 1) {
22+
} else {
23+
/* istanbul ignore next */
24+
if (contentTypeKeys.length > 1) {
25+
console.error('multiple content-type headers are set.');
26+
}
27+
// There maybe many, we'll just take the 1st one
2628
var contentType = contentTypeKeys[0];
2729
if (headers[contentType].match(/application\/json/i)) {
28-
options.body = JSON.stringify(body);
30+
body = JSON.stringify(body);
2931
} else if(headers[contentType].match(/application\/x-www-form-urlencoded/i)) {
30-
options.body = Object.keys(body).map(function(key){
32+
body = Object.keys(body).map(function(key){
3133
return `${key}=${encodeURIComponent(body[key])}`
3234
}).join("&");
3335
}
3436
}
35-
return options;
37+
return {body, headers};
3638
}
3739

3840
module.exports = function(options) {
@@ -44,7 +46,7 @@ module.exports = function(options) {
4446
delete options.success;
4547
delete options.error;
4648
delete options.uri; // not supported
47-
options = encodeBody(options);
49+
options = Object.assign(options, encodeBody(options));
4850
// set follow redirects to false by default
4951
options.followRedirect = options.followRedirects == true;
5052
// support params options

0 commit comments

Comments
 (0)