From bba67a9ebcbd6a56b6d1463afa8511566cb51ecf Mon Sep 17 00:00:00 2001 From: Gerard Banasig Date: Wed, 26 Jul 2017 17:13:14 +0800 Subject: [PATCH 1/3] Proposal intuitive syntax to get entities[0] Using the array destructuring, our code on the docs will look more intuitive, eliminating the entities[0] portion and using proper name. --- docs/Invoices.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/Invoices.md b/docs/Invoices.md index 6a62c547..43e56ec7 100644 --- a/docs/Invoices.md +++ b/docs/Invoices.md @@ -47,7 +47,8 @@ var invoiceObj = xeroClient.core.invoices.newInvoice(sampleInvoice); invoiceObj.save() .then(function(invoices) { //Invoice has been created - var myInvoice = invoices.entities[0]; + var [ myInvoice ] = invoices.entities; + console.log(myInvoice) }) .catch(function(err) { //Some error occurred @@ -77,7 +78,8 @@ currentApp.core.invoices.getInvoice(invoiceID) invoice.save() .then(function(savedInvoice) { - console.log(savedInvoice.entities[0].Status); //'AUTHORISED' + var [ mySavedInvoice ] = savedInvoice.entities + console.log(mySavedInvoice.Status); //'AUTHORISED' }); ``` @@ -150,4 +152,4 @@ xeroClient.core.invoices.getInvoices({ modifiedAfter: modifiedDate }) console.log(invoice.Type); //ACCPAY }); }) -``` \ No newline at end of file +``` From 5dffe3fc7f9ea4295c17aaa4143704e280e098a8 Mon Sep 17 00:00:00 2001 From: Jordan Walsh Date: Fri, 18 Aug 2017 09:46:43 +1000 Subject: [PATCH 2/3] added qs parsing for when oauth errors occur --- lib/application.js | 94 +++++++++++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 39 deletions(-) diff --git a/lib/application.js b/lib/application.js index 5228831e..aa58c35d 100644 --- a/lib/application.js +++ b/lib/application.js @@ -134,7 +134,15 @@ Object.assign(Application.prototype, { var contentType = options.contentType || 'application/json'; self.oa[method](url, self.options.accessToken, self.options.accessSecret, payload, contentType, function(err, data, res) { - data = JSON.parse(data); + try { + data = JSON.parse(data); + } catch (ex) { + // data could not be parsed as JSON, it's likely because of an oauth error + data = qs.parse(data); + } finally { + + } + if (err && data && data['ErrorNumber'] >= 0) { var errObj = new Error(method.toUpperCase() + ' call failed with: ' + err.statusCode); errObj.data = data; @@ -189,8 +197,12 @@ Object.assign(Application.prototype, { self.checkExpiry() .then(function() { self.oa.delete(url, self.options.accessToken, self.options.accessSecret, function(err, data, res) { - if (data) - data = JSON.parse(data); + try { + data = JSON.parse(data); + } catch (ex) { + // data could not be parsed as JSON, it's likely because of an oauth error + data = qs.parse(data); + } if (options.stream && !err) { // Already done @@ -216,7 +228,6 @@ Object.assign(Application.prototype, { return resolve(); } - var ret = { response: data, res: res }; resolve(ret); callback && callback(null, data, res); @@ -301,48 +312,53 @@ Object.assign(Application.prototype, { } self.oa.get(url, self.options.accessToken, self.options.accessSecret, function(err, data, res) { - data = JSON.parse(data) - if (options.stream && !err) { - // Already done - return resolve(); - } - if (err && data) { - var errObj = new Error('GET call failed with: ' + err.statusCode); - errObj.data = data; - reject(errObj); - callback && callback(errObj); - return; - } + try { + data = JSON.parse(data); + } catch (ex) { + // data could not be parsed as JSON, it's likely because of an oauth error + data = qs.parse(data); + } + if (options.stream && !err) { + // Already done + return resolve(); + } - var ret = { response: data, res: res }; - if (err) { - var errObj = new Error('GET call failed with: ' + err.statusCode + ' and exception: ' + JSON.stringify(data.ApiException, null, 2)); - reject(errObj); - callback && callback(errObj); - return; - } + if (err && data) { + var errObj = new Error('GET call failed with: ' + err.statusCode); + errObj.data = data; + reject(errObj); + callback && callback(errObj); + return; + } - if (options.pager && options.pager.callback) { - options.pager.callback(err, ret, function(err, result) { - result = _.defaults({}, result, { recordCount: 0, stop: false }); - if (!result.stop) - getResource(result.nextOffset || ++offset); - else - done(); - }) - return; - } + var ret = { response: data, res: res }; + if (err) { + var errObj = new Error('GET call failed with: ' + err.statusCode + ' and exception: ' + JSON.stringify(data.ApiException, null, 2)); + reject(errObj); + callback && callback(errObj); + return; + } + + if (options.pager && options.pager.callback) { + options.pager.callback(err, ret, function(err, result) { + result = _.defaults({}, result, { recordCount: 0, stop: false }); + if (!result.stop) + getResource(result.nextOffset || ++offset); + else + done(); + }) + return; + } - done(); + done(); - function done() { - resolve(ret); - callback && callback(null, data, res); - } + function done() { + resolve(ret); + callback && callback(null, data, res); + } }, { stream: options.stream }); - }; }); }, From 9f6aa69bebd8110982f054c238d69e68c4e7ccc4 Mon Sep 17 00:00:00 2001 From: Jordan Walsh Date: Fri, 18 Aug 2017 09:48:55 +1000 Subject: [PATCH 3/3] removed unused finally block --- lib/application.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/application.js b/lib/application.js index aa58c35d..c4494222 100644 --- a/lib/application.js +++ b/lib/application.js @@ -139,8 +139,6 @@ Object.assign(Application.prototype, { } catch (ex) { // data could not be parsed as JSON, it's likely because of an oauth error data = qs.parse(data); - } finally { - } if (err && data && data['ErrorNumber'] >= 0) {