Skip to content

Commit

Permalink
ES6-ify
Browse files Browse the repository at this point in the history
  • Loading branch information
flovilmart committed Mar 8, 2016
1 parent 95e5f02 commit 66fefa5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
13 changes: 5 additions & 8 deletions spec/HTTPRequest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,27 +156,24 @@ describe("httpRequest", () => {
done();
})
});

it("should encode a JSON body by default", (done) => {

let options = {
body: {"foo": "bar"},
}

var result = httpRequest.encodeBody(options);
let result = httpRequest.encodeBody(options);
expect(result.body).toEqual('{"foo":"bar"}');
expect(result.headers['Content-Type']).toEqual('application/json');
done();

})

it("should encode a JSON body", (done) => {

let options = {
body: {"foo": "bar"},
headers: {'Content-Type': 'application/json'}
}

var result = httpRequest.encodeBody(options);
let result = httpRequest.encodeBody(options);
expect(result.body).toEqual('{"foo":"bar"}');
done();

Expand All @@ -186,7 +183,7 @@ describe("httpRequest", () => {
body: {"foo": "bar", "bar": "baz"},
headers: {'cOntent-tYpe': 'application/x-www-form-urlencoded'}
}
var result = httpRequest.encodeBody(options);
let result = httpRequest.encodeBody(options);
expect(result.body).toEqual("foo=bar&bar=baz");
done();
});
Expand All @@ -195,7 +192,7 @@ describe("httpRequest", () => {
body:{"foo": "bar", "bar": "baz"},
headers: {'cOntent-tYpe': 'mime/jpeg'}
}
var result = httpRequest.encodeBody(options);
let result = httpRequest.encodeBody(options);
expect(result.body).toEqual({"foo": "bar", "bar": "baz"});
done();
});
Expand Down
4 changes: 2 additions & 2 deletions src/cloud-code/HTTPResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export default class HTTPResponse {
get data() {
if (!this._data) {
try {
this._data = JSON.parse(this.text);
this._data = JSON.parse(this.text);
} catch (e) {}
}
return this._data;
}
}
}
32 changes: 17 additions & 15 deletions src/cloud-code/httpRequest.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
var request = require("request"),
Parse = require('parse/node').Parse,
HTTPResponse = require('./HTTPResponse').HTTPResponse;
import request from 'request';
import Parse from 'parse/node';
import HTTPResponse from './HTTPResponse';

var encodeBody = function(options = {}) {
let body = options.body;
let headers = options.headers || {};
var encodeBody = function({body, headers = {}}) {
if (typeof body !== 'object') {
return options;
return {body, headers};
}
var contentTypeKeys = Object.keys(headers).filter((key) => {
return key.match(/content-type/i) != null;
Expand All @@ -15,23 +13,27 @@ var encodeBody = function(options = {}) {
if (contentTypeKeys.length == 0) {
// no content type
try {
options.body = JSON.stringify(body);
options.headers = options.headers || {};
options.headers['Content-Type'] = 'application/json';
body = JSON.stringify(body);
headers['Content-Type'] = 'application/json';
} catch(e) {
// do nothing;
}
} else if (contentTypeKeys.length == 1) {
} else {
/* istanbul ignore next */
if (contentTypeKeys.length > 1) {
console.error('multiple content-type headers are set.');
}
// There maybe many, we'll just take the 1st one
var contentType = contentTypeKeys[0];
if (headers[contentType].match(/application\/json/i)) {
options.body = JSON.stringify(body);
body = JSON.stringify(body);
} else if(headers[contentType].match(/application\/x-www-form-urlencoded/i)) {
options.body = Object.keys(body).map(function(key){
body = Object.keys(body).map(function(key){
return `${key}=${encodeURIComponent(body[key])}`
}).join("&");
}
}
return options;
return {body, headers};
}

module.exports = function(options) {
Expand All @@ -43,7 +45,7 @@ module.exports = function(options) {
delete options.success;
delete options.error;
delete options.uri; // not supported
options = encodeBody(options);
options = Object.assign(options, encodeBody(options));
// set follow redirects to false by default
options.followRedirect = options.followRedirects == true;
// force the response as a buffer
Expand Down

0 comments on commit 66fefa5

Please sign in to comment.