From 8d0f5d2bdc35710569e4646b6f3f21f06293ae18 Mon Sep 17 00:00:00 2001 From: Glynn Bird Date: Sat, 8 Oct 2016 19:18:02 +0100 Subject: [PATCH 1/3] ensure JSON is returned wherever possible --- plugins/promises.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plugins/promises.js b/plugins/promises.js index 8f7b737c..1562016c 100644 --- a/plugins/promises.js +++ b/plugins/promises.js @@ -29,10 +29,16 @@ module.exports = function(options) { return new Promise(function(resolve, reject) { request(req, function(err, h, b) { var statusCode = h && h.statusCode || 500; + if (b) { + try { b = JSON.parse(b); } catch (err) { } + } if (statusCode >= 200 && statusCode < 400) { callback(null, h, b); return resolve(b); } + if (b) { + b.statusCode = statusCode; + } reject(err || b); callback(err, h, b); }) From 3f6298395a21c4e3307b9422547a2f7680baa968 Mon Sep 17 00:00:00 2001 From: Glynn Bird Date: Sun, 9 Oct 2016 07:49:28 +0100 Subject: [PATCH 2/3] do not delete requestDefaults before passing to plugin - as per issue #151 --- cloudant.js | 1 - tests/plugin.js | 23 ++++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/cloudant.js b/cloudant.js index b814c279..5d2f7cf9 100644 --- a/cloudant.js +++ b/cloudant.js @@ -43,7 +43,6 @@ function Cloudant(options, callback) { if (typeof options == "object") { if (options.requestDefaults) { requestDefaults = options.requestDefaults; - delete options.requestDefaults; } theurl = reconfigure(options); } else { diff --git a/tests/plugin.js b/tests/plugin.js index e100cd54..74778daa 100644 --- a/tests/plugin.js +++ b/tests/plugin.js @@ -53,15 +53,32 @@ describe('promise plugin', function() { it('should return a promise', function(done) { var mocks = nock(SERVER) - .get('/' + MYDB).reply(200, {}); + .get('/' + MYDB).reply(200, { ok: true}); var cloudant = Cloudant({plugin:'promises', account:ME, password: PASSWORD}); var db = cloudant.db.use(MYDB); var p = db.info().then(function(data) { - data.should.be.an.object; + data.should.be.an.Object; done(); }); assert.equal(p instanceof Promise, true); - }) + }); + + it('should return an error status code', function(done) { + var mocks = nock(SERVER) + .get('/' + MYDB).reply(404, { ok: false}); + var cloudant = Cloudant({plugin:'promises', account:ME, password: PASSWORD}); + var db = cloudant.db.use(MYDB); + var p = db.info().then(function(data) { + assert(false); + }).catch(function(e) { + e.should.be.an.Object; + e.should.have.property.statusCode; + e.statusCode.should.be.a.Number; + e.statusCode.should.equal(404); + done(); + }); + assert.equal(p instanceof Promise, true); + }); }); From 2a1e82d598aaf480493c04fb8fea367d604f5fca Mon Sep 17 00:00:00 2001 From: Glynn Bird Date: Sun, 9 Oct 2016 07:54:01 +0100 Subject: [PATCH 3/3] ensure agent and user-agent are passed to plugins. Version bump --- cloudant.js | 1 + package.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/cloudant.js b/cloudant.js index 5d2f7cf9..8bf8cb2b 100644 --- a/cloudant.js +++ b/cloudant.js @@ -59,6 +59,7 @@ function Cloudant(options, callback) { // plugin a request library var plugin = null; if (options.plugin) { + options.requestDefaults = requestDefaults; if(typeof options.plugin === 'string') { var plugintype = options.plugin || 'default'; debug('Using the "' + plugintype + '" plugin'); diff --git a/package.json b/package.json index 4f0ce37d..1cea3fd7 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "license": "Apache-2.0", "homepage": "http://github.com/cloudant/nodejs-cloudant", "repository": "git://github.com/cloudant/nodejs-cloudant", - "version": "1.5.0", + "version": "1.5.1", "author": "Jason Smith ", "contributors": [ "Glynn Bird "