Skip to content

Commit d6c2f73

Browse files
Merge pull request #178 from 0xNafri/add-response-body-checking
Fix PDF Generation Issue with QuickBooks API by Checking Response Body When JSON is Null
2 parents 7ea0965 + 0fe35a5 commit d6c2f73

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/OAuthClient.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ OAuthClient.prototype.createToken = function createToken(uri) {
185185
const authResponse = res.json ? res : null;
186186
const json = (authResponse && authResponse.json) || res;
187187
this.token.setToken(json);
188-
this.log('info', 'Create Token response is : ', JSON.stringify(authResponse.json, null, 2));
188+
this.log('info', 'Create Token response is : ', JSON.stringify(authResponse && authResponse.json, null, 2));
189189
return authResponse;
190190
})
191191
.catch((e) => {
@@ -226,7 +226,7 @@ OAuthClient.prototype.refresh = function refresh() {
226226
const authResponse = res.json ? res : null;
227227
const json = (authResponse && authResponse.json) || res;
228228
this.token.setToken(json);
229-
this.log('info', 'Refresh Token () response is : ', JSON.stringify(authResponse.json, null, 2));
229+
this.log('info', 'Refresh Token () response is : ', JSON.stringify(authResponse && authResponse.json, null, 2));
230230
return authResponse;
231231
})
232232
.catch((e) => {
@@ -270,8 +270,7 @@ OAuthClient.prototype.refreshUsingToken = function refreshUsingToken(refresh_tok
270270
this.token.setToken(json);
271271
this.log(
272272
'info',
273-
'Refresh usingToken () response is : ',
274-
JSON.stringify(authResponse.json, null, 2),
273+
'Refresh usingToken () response is : ', JSON.stringify(authResponse && authResponse.json, null, 2),
275274
);
276275
return authResponse;
277276
})
@@ -318,7 +317,7 @@ OAuthClient.prototype.revoke = function revoke(params) {
318317
.then((res) => {
319318
const authResponse = res.json ? res : null;
320319
this.token.clearToken();
321-
this.log('info', 'Revoke Token () response is : ', JSON.stringify(authResponse.json, null, 2));
320+
this.log('info', 'Revoke Token () response is : ', JSON.stringify(authResponse && authResponse.json, null, 2));
322321
return authResponse;
323322
})
324323
.catch((e) => {
@@ -353,8 +352,7 @@ OAuthClient.prototype.getUserInfo = function getUserInfo() {
353352
const authResponse = res.json ? res : null;
354353
this.log(
355354
'info',
356-
'The Get User Info () response is : ',
357-
JSON.stringify(authResponse.json, null, 2),
355+
'The Get User Info () response is : ', JSON.stringify(authResponse && authResponse.json, null, 2),
358356
);
359357
return authResponse;
360358
})
@@ -403,8 +401,15 @@ OAuthClient.prototype.makeApiCall = function makeApiCall(params) {
403401
resolve(this.getTokenRequest(request));
404402
})
405403
.then((res) => {
406-
const authResponse = res.json ? res : null;
404+
const { body, ...authResponse } = res;
407405
this.log('info', 'The makeAPICall () response is : ', JSON.stringify(authResponse.json, null, 2));
406+
407+
if(authResponse.json === null && body) {
408+
return {
409+
...authResponse,
410+
body: body
411+
}
412+
}
408413
return authResponse;
409414
})
410415
.catch((e) => {
@@ -476,8 +481,7 @@ OAuthClient.prototype.getKeyFromJWKsURI = function getKeyFromJWKsURI(id_token, k
476481
.then((response) => {
477482
if (Number(response.status) !== 200) throw new Error('Could not reach JWK endpoint');
478483
// Find the key by KID
479-
const responseBody = JSON.parse(response.body);
480-
const key = responseBody.keys.find((el) => el.kid === kid);
484+
const key = response.data.keys.find((el) => el.kid === kid);
481485
const cert = this.getPublicKey(key.n, key.e);
482486

483487
return jwt.verify(id_token, cert);

0 commit comments

Comments
 (0)