Skip to content

Commit

Permalink
Merge pull request #59 from XeroAPI/master
Browse files Browse the repository at this point in the history
Backwards merge
  • Loading branch information
jordanwalsh23 authored Aug 22, 2017
2 parents 3b00a04 + aee84cd commit 342ce59
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 42 deletions.
8 changes: 5 additions & 3 deletions docs/Invoices.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
});
```
Expand Down Expand Up @@ -150,4 +152,4 @@ xeroClient.core.invoices.getInvoices({ modifiedAfter: modifiedDate })
console.log(invoice.Type); //ACCPAY
});
})
```
```
92 changes: 53 additions & 39 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -216,7 +226,6 @@ Object.assign(Application.prototype, {
return resolve();
}


var ret = { response: data, res: res };
resolve(ret);
callback && callback(null, data, res);
Expand Down Expand Up @@ -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 });

};
});
},
Expand Down

0 comments on commit 342ce59

Please sign in to comment.