From 578e53646a0d3da1aff92d9a35e71587e97c6d7d Mon Sep 17 00:00:00 2001 From: Julien Sol Date: Sat, 3 Jun 2017 17:57:15 +0200 Subject: [PATCH 1/5] Allow post data method modification --- lib/twitter.js | 11 ++++------- test/twitter.js | 1 + 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/twitter.js b/lib/twitter.js index 0754de20..d94ca4af 100644 --- a/lib/twitter.js +++ b/lib/twitter.js @@ -29,6 +29,7 @@ function Twitter(options) { user_stream_base: 'https://userstream.twitter.com/1.1', site_stream_base: 'https://sitestream.twitter.com/1.1', media_base: 'https://upload.twitter.com/1.1', + post_method: null, request_options: { headers: { Accept: '*/*', @@ -129,14 +130,10 @@ Twitter.prototype.__request = function(method, path, params, callback) { options.qs = params; } - // Pass form data if post + // Pass data if post if (method === 'post') { - var formKey = 'form'; - - if (typeof params.media !== 'undefined') { - formKey = 'formData'; - } - options[formKey] = params; + var post_method = this.options.post_method ? this.options.post_method : (params.media ? 'formData' : 'form'); + options[post_method] = params; } // Promisified version diff --git a/test/twitter.js b/test/twitter.js index 7dbe9a3d..f1dbb518 100644 --- a/test/twitter.js +++ b/test/twitter.js @@ -21,6 +21,7 @@ describe('Twitter', function() { user_stream_base: 'https://userstream.twitter.com/1.1', site_stream_base: 'https://sitestream.twitter.com/1.1', media_base: 'https://upload.twitter.com/1.1', + post_method: null, request_options: { headers: { 'Accept': '*/*', From feed6da121272153a91082fd7ebde15c9186c7e2 Mon Sep 17 00:00:00 2001 From: Julien Sol Date: Sun, 4 Jun 2017 22:06:10 +0200 Subject: [PATCH 2/5] Fix the parsing of JSON responses --- lib/twitter.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/twitter.js b/lib/twitter.js index d94ca4af..7356d4d7 100644 --- a/lib/twitter.js +++ b/lib/twitter.js @@ -146,13 +146,15 @@ Twitter.prototype.__request = function(method, path, params, callback) { return reject(error); } + var contentType = response.headers['content-type']; + // JSON parse error or empty strings try { // An empty string is a valid response if (data === '') { data = {}; } - else { + else if (!contentType || !contentType.match(/application\/json/)) { data = JSON.parse(data); } } @@ -184,14 +186,16 @@ Twitter.prototype.__request = function(method, path, params, callback) { return callback(error, data, response); } + var contentType = response.headers['content-type']; + // JSON parse error or empty strings try { // An empty string is a valid response if (data === '') { - data = {}; + data = {}; } - else { - data = JSON.parse(data); + else if (!contentType || !contentType.match(/application\/json/)) { + data = JSON.parse(data); } } catch(parseError) { From 841339af7fd6363705f109a6e29ae8c29992a12f Mon Sep 17 00:00:00 2001 From: Juju Date: Mon, 5 Jun 2017 01:31:17 +0200 Subject: [PATCH 3/5] Fix indentation --- lib/twitter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/twitter.js b/lib/twitter.js index 7356d4d7..b57c9dc6 100644 --- a/lib/twitter.js +++ b/lib/twitter.js @@ -192,10 +192,10 @@ Twitter.prototype.__request = function(method, path, params, callback) { try { // An empty string is a valid response if (data === '') { - data = {}; + data = {}; } else if (!contentType || !contentType.match(/application\/json/)) { - data = JSON.parse(data); + data = JSON.parse(data); } } catch(parseError) { From cdc39057569b18e2008084cc94a1d9c397e736b9 Mon Sep 17 00:00:00 2001 From: Julien Sol Date: Mon, 5 Jun 2017 02:28:31 +0200 Subject: [PATCH 4/5] Revert "Fix indentation" This reverts commit 841339af7fd6363705f109a6e29ae8c29992a12f. --- lib/twitter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/twitter.js b/lib/twitter.js index b57c9dc6..7356d4d7 100644 --- a/lib/twitter.js +++ b/lib/twitter.js @@ -192,10 +192,10 @@ Twitter.prototype.__request = function(method, path, params, callback) { try { // An empty string is a valid response if (data === '') { - data = {}; + data = {}; } else if (!contentType || !contentType.match(/application\/json/)) { - data = JSON.parse(data); + data = JSON.parse(data); } } catch(parseError) { From 8f824881a273bad452a95a1cb67b3c6cfb38da28 Mon Sep 17 00:00:00 2001 From: Julien Sol Date: Mon, 5 Jun 2017 02:29:03 +0200 Subject: [PATCH 5/5] Revert "Fix the parsing of JSON responses" This reverts commit feed6da121272153a91082fd7ebde15c9186c7e2. --- lib/twitter.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/twitter.js b/lib/twitter.js index 7356d4d7..d94ca4af 100644 --- a/lib/twitter.js +++ b/lib/twitter.js @@ -146,15 +146,13 @@ Twitter.prototype.__request = function(method, path, params, callback) { return reject(error); } - var contentType = response.headers['content-type']; - // JSON parse error or empty strings try { // An empty string is a valid response if (data === '') { data = {}; } - else if (!contentType || !contentType.match(/application\/json/)) { + else { data = JSON.parse(data); } } @@ -186,16 +184,14 @@ Twitter.prototype.__request = function(method, path, params, callback) { return callback(error, data, response); } - var contentType = response.headers['content-type']; - // JSON parse error or empty strings try { // An empty string is a valid response if (data === '') { - data = {}; + data = {}; } - else if (!contentType || !contentType.match(/application\/json/)) { - data = JSON.parse(data); + else { + data = JSON.parse(data); } } catch(parseError) {