From 207edbd68c8399124123f6d4bb6da9f16881c133 Mon Sep 17 00:00:00 2001 From: Seth Samuel Date: Mon, 18 Jul 2016 16:00:09 -0400 Subject: [PATCH] Clean up promise and logic --- lib/index.js | 98 +++++++++++++++++++--------------------------------- 1 file changed, 35 insertions(+), 63 deletions(-) diff --git a/lib/index.js b/lib/index.js index 7dc308e..c4b8eb2 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,45 +1,60 @@ var generateQueryString = require('./utils').generateQueryString, request = require('request'), parseXML = require('xml2js').parseString; - -if (!global.Promise) { - var Promise = require('es6-promise').Promise; -} +if (typeof Promise === 'undefined') { + Promise = require('es6-promise').Promise; +} + var runQuery = function (credentials, method) { return function (query, cb) { var url = generateQueryString(query, method, credentials); var req = query.request || request; - if (typeof cb === 'function') { - req(url, function (err, response, body) { + var p = new Promise(function(resolve, reject) { + var success = function(results) { + if (typeof cb === 'function') { + cb.apply(null, [null].concat(Array.prototype.slice.call(arguments))); + }else{ + resolve(results); + } + }; + var failure = function(err) { + if (typeof cb === 'function') { + cb.call(null, err); + } else { + reject(err); + } + }; + + + req(url, function (err, response, body) { if (err) { - cb(err); + failure(err); } else if (!response) { - cb("No response (check internet connection)"); + failure("No response (check internet connection)"); } else if (response.statusCode !== 200) { parseXML(body, function (err, resp) { if (err) { - cb(err); + failure(err); } else { - cb(resp[method + 'ErrorResponse']); + failure(resp[method + 'ErrorResponse']); } }); } else { parseXML(body, function (err, resp) { if (err) { - cb(err); + failure(err); } else { var respObj = resp[method + 'Response']; if (respObj.Items && respObj.Items.length > 0) { // Request Error if (respObj.Items[0].Request && respObj.Items[0].Request.length > 0 && respObj.Items[0].Request[0].Errors) { - cb(respObj.Items[0].Request[0].Errors); + failure(respObj.Items[0].Request[0].Errors); } else if (respObj.Items[0].Item) { - cb( - null, + success( respObj.Items[0].Item, respObj.Items[0] ); @@ -47,10 +62,9 @@ var runQuery = function (credentials, method) { } else if (respObj.BrowseNodes && respObj.BrowseNodes.length > 0) { // Request Error if (respObj.BrowseNodes[0].Request && respObj.BrowseNodes[0].Request.length > 0 && respObj.BrowseNodes[0].Request[0].Errors) { - cb(respObj.BrowseNodes[0].Request[0].Errors); + failure(respObj.BrowseNodes[0].Request[0].Errors); } else if (respObj.BrowseNodes[0].BrowseNode) { - cb( - null, + success( respObj.BrowseNodes[0].BrowseNode, respObj.BrowseNodes[0] ); @@ -60,53 +74,11 @@ var runQuery = function (credentials, method) { }); } }); - return; - } - - var promise = new Promise(function (resolve, reject) { - - req(url, function (err, response, body) { - - if (err) { - reject(err); - } else if (!response) { - reject("No response (check internet connection)"); - } else if (response.statusCode !== 200) { - parseXML(body, function (err, resp) { - if (err) { - reject(err); - } else { - reject(resp[method + 'ErrorResponse']); - } - }); - } else { - parseXML(body, function (err, resp) { - if (err) { - reject(err); - } else { - var respObj = resp[method + 'Response']; - if (respObj.Items && respObj.Items.length > 0) { - // Request Error - if (respObj.Items[0].Request && respObj.Items[0].Request.length > 0 && respObj.Items[0].Request[0].Errors) { - reject(respObj.Items[0].Request[0].Errors); - } else if (respObj.Items[0].Item) { - resolve(respObj.Items[0].Item); - } - } else if (respObj.BrowseNodes && respObj.BrowseNodes.length > 0) { - // Request Error - if (respObj.BrowseNodes[0].Request && respObj.BrowseNodes[0].Request.length > 0 && respObj.BrowseNodes[0].Request[0].Errors) { - reject(respObj.BrowseNodes[0].Request[0].Errors); - } else if (respObj.BrowseNodes[0].BrowseNode) { - resolve(respObj.BrowseNodes[0].BrowseNode); - } - } - } - }); - } - }); }); - - return promise; + + if(typeof cb !== 'function') { + return p; + } }; };