diff --git a/index.js b/index.js index 013ea7c..e6deb3b 100644 --- a/index.js +++ b/index.js @@ -60,9 +60,9 @@ var formatPath = function (path, path_params) { Mailchimp.prototype.post = function (options, body, done) { - console.log(JSON.stringify({ + console.log(JSON.stringify({ post_options: options, - post_body: body + post_body: body })); options = _.clone(options) || {}; @@ -91,9 +91,9 @@ Mailchimp.prototype.post = function (options, body, done) { } Mailchimp.prototype.patch = function (options, body, done) { - console.log(JSON.stringify({ + console.log(JSON.stringify({ patch_options: options, - patch_body: body + patch_body: body })); options = _.clone(options) || {}; @@ -123,7 +123,7 @@ Mailchimp.prototype.patch = function (options, body, done) { Mailchimp.prototype.request = function (options, done) { console.log(JSON.stringify({ request_options: options })); - + var mailchimp = this; var promise = new Promise(function(resolve, reject) { if (!options) { @@ -161,18 +161,6 @@ Mailchimp.prototype.request = function (options, done) { } let axios_req = { - method : method, - url : mailchimp.__base_url + path, - data : body, - params : query, - headers : headers, - responseType: 'json', - } - - // omitted auth in log - console.log({ axios_req }); - - axios({ method : method, url : mailchimp.__base_url + path, auth : { @@ -183,32 +171,39 @@ Mailchimp.prototype.request = function (options, done) { params : query, headers : headers, responseType: 'json', - }, function (err, response) { - - if (err) { - var error = new Error(err); - console.log('Mailchimp Error', error.response); - error.response = response; - error.status = response ? response.status : undefined; - reject(error) - return; - } - - console.log('Mailchimp Response: ', response) - - if (response.status < 200 || response.status > 299) { - var error = Object.assign(new Error(response.data ? response.data : response.status), response.data || response) - error.response = response; - error.status = response.status; - reject(error); - return; - } + } - var result = response.data || {}; - result.statusCode = response.status; + // omitted auth in log + console.log({ axios_req }); - resolve(result) - }) + axios(axios_req) + .then((response) => { + + console.log('Mailchimp Response: ', response) + + if (response.status < 200 || response.status > 299) { + var error = Object.assign(new Error(response.data ? response.data : response.status), response.data || response) + error.response = response; + error.status = response.status; + reject(error); + return; + } + + var result = response.data || {}; + result.statusCode = response.status; + resolve(result) + }) + .catch((err) => { + console.log('Mailchimp Error: ', err) + if (err) { + var error = new Error(err); + console.log('Mailchimp Error', error.response); + error.response = response; + error.status = response ? response.status : undefined; + reject(error) + return; + } + }); })