Skip to content

Commit 1e71150

Browse files
Merge pull request #150 from benflap/dependency-compile-issue
Continue Axios Migration
2 parents 606f826 + f123f5b commit 1e71150

File tree

2 files changed

+35
-45
lines changed

2 files changed

+35
-45
lines changed

src/OAuthClient.js

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ OAuthClient.prototype.createToken = function createToken(uri) {
169169

170170
const request = {
171171
url: OAuthClient.tokenEndpoint,
172-
body,
172+
data: body,
173173
method: 'POST',
174174
headers: {
175175
Authorization: `Basic ${this.authHeader()}`,
@@ -182,8 +182,8 @@ OAuthClient.prototype.createToken = function createToken(uri) {
182182
resolve(this.getTokenRequest(request));
183183
})
184184
.then((res) => {
185-
const authResponse = res.json ? res : null;
186-
const json = (authResponse && authResponse.getJson()) || res;
185+
const { response, ...authResponse } = res.json ? res : null;
186+
const json = (authResponse && authResponse.json) || res;
187187
this.token.setToken(json);
188188
this.log('info', 'Create Token response is : ', JSON.stringify(authResponse, null, 2));
189189
return authResponse;
@@ -210,7 +210,7 @@ OAuthClient.prototype.refresh = function refresh() {
210210

211211
const request = {
212212
url: OAuthClient.tokenEndpoint,
213-
body,
213+
data: body,
214214
method: 'POST',
215215
headers: {
216216
Authorization: `Basic ${this.authHeader()}`,
@@ -223,7 +223,7 @@ OAuthClient.prototype.refresh = function refresh() {
223223
resolve(this.getTokenRequest(request));
224224
})
225225
.then((res) => {
226-
const authResponse = res.json ? res : null;
226+
const { request, ...authResponse } = res.json ? res : null;
227227
const json = (authResponse && authResponse.getJson()) || res;
228228
this.token.setToken(json);
229229
this.log('info', 'Refresh Token () response is : ', JSON.stringify(authResponse, null, 2));
@@ -252,7 +252,7 @@ OAuthClient.prototype.refreshUsingToken = function refreshUsingToken(refresh_tok
252252

253253
const request = {
254254
url: OAuthClient.tokenEndpoint,
255-
body,
255+
data: body,
256256
method: 'POST',
257257
headers: {
258258
Authorization: `Basic ${this.authHeader()}`,
@@ -265,7 +265,7 @@ OAuthClient.prototype.refreshUsingToken = function refreshUsingToken(refresh_tok
265265
resolve(this.getTokenRequest(request));
266266
})
267267
.then((res) => {
268-
const authResponse = res.json ? res : null;
268+
const { request, ...authResponse } = res.json ? res : null;
269269
const json = (authResponse && authResponse.getJson()) || res;
270270
this.token.setToken(json);
271271
this.log(
@@ -303,7 +303,7 @@ OAuthClient.prototype.revoke = function revoke(params) {
303303

304304
const request = {
305305
url: OAuthClient.revokeEndpoint,
306-
body,
306+
data: body,
307307
method: 'POST',
308308
headers: {
309309
Authorization: `Basic ${this.authHeader()}`,
@@ -315,7 +315,7 @@ OAuthClient.prototype.revoke = function revoke(params) {
315315

316316
resolve(this.getTokenRequest(request));
317317
})
318-
.then((authResponse) => {
318+
.then(({ request, ...authResponse }) => {
319319
this.token.clearToken();
320320
this.log('info', 'Revoke Token () response is : ', JSON.stringify(authResponse, null, 2));
321321
return authResponse;
@@ -349,7 +349,7 @@ OAuthClient.prototype.getUserInfo = function getUserInfo() {
349349
resolve(this.getTokenRequest(request));
350350
})
351351
.then((res) => {
352-
const authResponse = res.json ? res : null;
352+
const { request, ...authResponse } = res.json ? res : null;
353353
this.log(
354354
'info',
355355
'The Get User Info () response is : ',
@@ -365,47 +365,43 @@ OAuthClient.prototype.getUserInfo = function getUserInfo() {
365365

366366
/**
367367
* Make API call. Pass the url,method,headers using `params` object
368-
* *
369-
* @param {Object} params
368+
*
369+
* @param {params} params
370+
* @param {string} params.url
371+
* @param {string} params.method (optional) default is GET
372+
* @param {Object} params.headers (optional)
373+
* @param {Object} params.body (optional)
374+
* @param {string} params.responseType (optional) default is json - options are json, text, stream, arraybuffer
370375
* @returns {Promise}
371376
*/
372377
OAuthClient.prototype.makeApiCall = function makeApiCall(params) {
373378
return new Promise((resolve) => {
374379
params = params || {};
375-
const transport = params.transport ? params.transport : { responseType: 'text' };
380+
const responseType = params.responseType ? params.responseType : 'json';
381+
382+
const baseHeaders = {
383+
Authorization: `Bearer ${this.getToken().access_token}`,
384+
Accept: AuthResponse._jsonContentType,
385+
'User-Agent': OAuthClient.user_agent,
386+
};
376387

377388
const headers =
378389
params.headers && typeof params.headers === 'object'
379-
? Object.assign(
380-
{},
381-
{
382-
Authorization: `Bearer ${this.getToken().access_token}`,
383-
Accept: AuthResponse._jsonContentType,
384-
'User-Agent': OAuthClient.user_agent,
385-
},
386-
params.headers
387-
)
388-
: Object.assign(
389-
{},
390-
{
391-
Authorization: `Bearer ${this.getToken().access_token}`,
392-
Accept: AuthResponse._jsonContentType,
393-
'User-Agent': OAuthClient.user_agent,
394-
}
395-
);
390+
? Object.assign({}, baseHeaders, params.headers)
391+
: Object.assign({}, baseHeaders);
396392

397393
const request = {
398394
url: params.url,
399395
method: params.method || 'GET',
400396
headers,
401-
transport,
397+
responseType,
402398
};
403399

404-
params.body && (request.body = params.body);
400+
params.body && (request.data = params.body);
405401

406402
resolve(this.getTokenRequest(request));
407403
})
408-
.then((authResponse) => {
404+
.then(({ request, ...authResponse }) => {
409405
this.log('info', 'The makeAPICall () response is : ', JSON.stringify(authResponse, null, 2));
410406
return authResponse;
411407
})
@@ -552,7 +548,7 @@ OAuthClient.prototype.validateToken = function validateToken() {
552548
* @returns response
553549
*/
554550
OAuthClient.prototype.loadResponse = function loadResponse(request) {
555-
return axios.get(request).then((response) => response);
551+
return axios(request).then((response) => response);
556552
};
557553

558554
/**

src/response/AuthResponse.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ function AuthResponse(params) {
4444
*/
4545
AuthResponse.prototype.processResponse = function processResponse(response) {
4646
this.response = response || '';
47-
this.body = (response && response.body) || '';
48-
this.json = this.body && this.isJson() ? JSON.parse(this.body) : null;
47+
this.body = (response && response.data) || '';
48+
this.json = this.body && this.isJson() ? this.body : null;
4949
this.intuit_tid = (response && response.headers && response.headers.intuit_tid) || '';
5050
};
5151

@@ -58,7 +58,6 @@ AuthResponse.prototype.getToken = function getToken() {
5858
return this.token.getToken();
5959
};
6060

61-
6261
/**
6362
* Get Token
6463
* *
@@ -92,11 +91,9 @@ AuthResponse.prototype.headers = function headers() {
9291
* @returns {*|boolean}
9392
*/
9493
AuthResponse.prototype.valid = function valid() {
95-
return (this.response && Number(this.response.status) >= 200
96-
&& Number(this.response.status) < 300);
94+
return this.response && Number(this.response.status) >= 200 && Number(this.response.status) < 300;
9795
};
9896

99-
10097
/**
10198
* Get Json () { returns token as JSON }
10299
* *
@@ -119,7 +116,6 @@ AuthResponse.prototype.get_intuit_tid = function get_intuit_tid() {
119116
return this.intuit_tid;
120117
};
121118

122-
123119
/**
124120
* isContentType
125121
* *
@@ -135,7 +131,7 @@ AuthResponse.prototype.isContentType = function isContentType(contentType) {
135131
* @returns {string} getContentType
136132
*/
137133
AuthResponse.prototype.getContentType = function getContentType() {
138-
return this.response.get(AuthResponse._contentType) || '';
134+
return this.response.headers[AuthResponse._contentType] || '';
139135
};
140136

141137
/**
@@ -147,10 +143,8 @@ AuthResponse.prototype.isJson = function isJson() {
147143
return this.isContentType('application/json');
148144
};
149145

150-
151-
AuthResponse._contentType = 'Content-Type';
146+
AuthResponse._contentType = 'content-type';
152147
AuthResponse._jsonContentType = 'application/json';
153148
AuthResponse._urlencodedContentType = 'application/x-www-form-urlencoded';
154149

155-
156150
module.exports = AuthResponse;

0 commit comments

Comments
 (0)