Skip to content

Commit

Permalink
Clean up promise and logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Seth Samuel committed Jul 18, 2016
1 parent 0ab31fc commit 207edbd
Showing 1 changed file with 35 additions and 63 deletions.
98 changes: 35 additions & 63 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,70 @@
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]
);
}
} 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]
);
Expand All @@ -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;
}
};
};

Expand Down

0 comments on commit 207edbd

Please sign in to comment.