@@ -64,39 +64,38 @@ export abstract class HttpClient {
6464 config . headers [ 'Accept-Charset' ] = `utf-8` ;
6565 config . headers [ 'Accept' ] = `application/json` ;
6666 config . headers [ 'Content-Type' ] = `application/json` ;
67+ //config.headers['content-type'] = `application/json`;
6768 }
6869
6970 if ( config . url !== '/v2/oauth/token' ) {
7071 // check token validity
71- const access_token = await checkTokenValidity ( ) ;
72-
72+ let access_token = await checkTokenValidity ( ) ;
73+
7374 if ( access_token ) {
74- config . headers [ 'Authorization' ] = `Bearer ${ access_token } ` ;
75+ config . headers [ 'Authorization' ] = `Bearer ${ access_token } ` ;
7576 } else {
7677 // retrieve auth credentials
7778 const authCredentials = await this . auth . getCredentials ( ) ;
7879
79- // request new token
80- const { access_token } = await this . renewToken ( authCredentials ) ;
81-
80+ // request new token
81+ let renewed_token = await this . renewToken ( authCredentials ) ;
8282 // set token if valid
83- if ( access_token ) {
83+ if ( renewed_token ) {
8484 // set authorization headers
85- config . headers [ 'Authorization' ] = `Bearer ${ access_token } ` ;
85+ config . headers [ 'Authorization' ] = `Bearer ${ renewed_token } ` ;
8686 // set authorization token in storage
8787 const futureTimeStamp = addMinutes (
8888 new Date ( ) ,
8989 Constants . TOKEN_EXPIRE_IN_50_MINS
9090 ) ;
9191 await setAuthToken (
92- access_token ,
92+ renewed_token ,
9393 futureTimeStamp . toISOString ( )
9494 ) ;
9595 }
9696 }
9797 }
9898
99-
10099 return config ;
101100 } ;
102101
@@ -132,16 +131,16 @@ export abstract class HttpClient {
132131 const authCredentials = await this . auth . getCredentials ( ) ;
133132
134133 // request new token
135- const { access_token } = await this . renewToken ( authCredentials ) ;
134+ let renewed_token = await this . renewToken ( authCredentials ) ;
136135
137136 // set token if valid
138- if ( access_token ) {
137+ if ( renewed_token ) {
139138 // set authorization token in storage
140139 const futureTimeStamp = addMinutes (
141140 new Date ( ) ,
142141 Constants . TOKEN_EXPIRE_IN_50_MINS
143142 ) ;
144- await setAuthToken ( access_token , futureTimeStamp . toISOString ( ) ) ;
143+ await setAuthToken ( renewed_token , futureTimeStamp . toISOString ( ) ) ;
145144
146145 return this . instance (
147146 originalRequest as InternalAxiosRequestConfig
@@ -169,9 +168,7 @@ export abstract class HttpClient {
169168 return Promise . reject ( error ) ;
170169 } ;
171170
172- private async renewToken (
173- authCredentials : AuthCredentials
174- ) : Promise < { access_token : string } > {
171+ private async renewToken ( authCredentials : AuthCredentials ) : Promise < string | null > {
175172 const params = new URLSearchParams ( ) ;
176173 params . append ( 'client_id' , `${ authCredentials . client_id } ` ) ;
177174 params . append ( 'client_secret' , `${ authCredentials . client_secret } ` ) ;
@@ -180,10 +177,17 @@ export abstract class HttpClient {
180177 'scope' ,
181178 'free-trial-numbers:read free-trial-numbers:write messages:read messages:write virtual-numbers:read virtual-numbers:write reports:read reports:write'
182179 ) ;
183-
184- const auth = await this . instance . post ( `/v2/oauth/token` , params ) ;
185- if ( ! auth ) return auth ;
186- const { access_token } = auth ;
187- return access_token ;
180+
181+ try {
182+ const auth = await this . instance . post ( `/v2/oauth/token` , params ) ;
183+
184+ if ( auth && auth . access_token ) {
185+ return auth . access_token ;
186+ } else {
187+ return null ;
188+ }
189+ } catch ( error ) {
190+ return null ;
191+ }
188192 }
189193}
0 commit comments