ES6-ify
This commit is contained in:
@@ -160,13 +160,12 @@ 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();
|
||||
@@ -174,13 +173,11 @@ describe("httpRequest", () => {
|
||||
})
|
||||
|
||||
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();
|
||||
|
||||
@@ -190,7 +187,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();
|
||||
});
|
||||
@@ -199,7 +196,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();
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
var request = require("request"),
|
||||
querystring = require('querystring'),
|
||||
Parse = require('parse/node').Parse;
|
||||
HTTPResponse = require('./HTTPResponse').HTTPResponse;
|
||||
import request from 'request';
|
||||
import Parse from 'parse/node';
|
||||
import HTTPResponse from './HTTPResponse';
|
||||
import querystring from 'querystring';
|
||||
|
||||
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;
|
||||
@@ -16,23 +14,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) {
|
||||
@@ -44,7 +46,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;
|
||||
// support params options
|
||||
|
||||
Reference in New Issue
Block a user