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 +``` diff --git a/lib/application.js b/lib/application.js index 5228831e..c4494222 100644 --- a/lib/application.js +++ b/lib/application.js @@ -134,7 +134,13 @@ 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); + } + if (err && data && data['ErrorNumber'] >= 0) { var errObj = new Error(method.toUpperCase() + ' call failed with: ' + err.statusCode); errObj.data = data; @@ -189,8 +195,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 +226,6 @@ Object.assign(Application.prototype, { return resolve(); } - var ret = { response: data, res: res }; resolve(ret); callback && callback(null, data, res); @@ -301,48 +310,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 }); - }; }); },