@@ -51,39 +51,40 @@ export interface ApiClientOptions {
51
51
52
52
export class ApiClient {
53
53
private token ?: OAuthToken ;
54
- private saveToken ?: saveTokenFunction ;
55
- private client = createClient < paths > ( {
54
+ private readonly saveToken ?: saveTokenFunction ;
55
+ private readonly client = createClient < paths > ( {
56
56
baseUrl : config . apiBaseUrl ,
57
57
headers : {
58
58
"User-Agent" : config . userAgent ,
59
59
Accept : `application/vnd.atlas.${ config . atlasApiVersion } +json` ,
60
60
} ,
61
61
} ) ;
62
- private authMiddleware = ( apiClient : ApiClient ) : Middleware => ( {
63
- async onRequest ( { request, schemaPath } ) {
62
+
63
+ private readonly authMiddleware : Middleware = {
64
+ onRequest : async ( { request, schemaPath } ) => {
64
65
if ( schemaPath . startsWith ( "/api/private/unauth" ) || schemaPath . startsWith ( "/api/oauth" ) ) {
65
66
return undefined ;
66
67
}
67
- if ( await apiClient . validateToken ( ) ) {
68
- request . headers . set ( "Authorization" , `Bearer ${ apiClient . token ! . access_token } ` ) ;
68
+ if ( this . token && ( await this . validateToken ( ) ) ) {
69
+ request . headers . set ( "Authorization" , `Bearer ${ this . token . access_token } ` ) ;
69
70
return request ;
70
71
}
71
72
} ,
72
- } ) ;
73
- private errorMiddleware = ( ) : Middleware => ( {
73
+ } ;
74
+ private readonly errorMiddleware : Middleware = {
74
75
async onResponse ( { response } ) {
75
76
if ( ! response . ok ) {
76
77
throw await ApiClientError . fromResponse ( response ) ;
77
78
}
78
79
} ,
79
- } ) ;
80
+ } ;
80
81
81
82
constructor ( options : ApiClientOptions ) {
82
83
const { token, saveToken } = options ;
83
84
this . token = token ;
84
85
this . saveToken = saveToken ;
85
- this . client . use ( this . authMiddleware ( this ) ) ;
86
- this . client . use ( this . errorMiddleware ( ) ) ;
86
+ this . client . use ( this . authMiddleware ) ;
87
+ this . client . use ( this . errorMiddleware ) ;
87
88
}
88
89
89
90
static fromState ( state : State ) : ApiClient {
@@ -173,7 +174,7 @@ export class ApiClient {
173
174
}
174
175
}
175
176
176
- async refreshToken ( token ? : OAuthToken ) : Promise < OAuthToken | null > {
177
+ async refreshToken ( token : OAuthToken ) : Promise < OAuthToken > {
177
178
const endpoint = "api/private/unauth/account/device/token" ;
178
179
const url = new URL ( endpoint , config . apiBaseUrl ) ;
179
180
const response = await fetch ( url , {
@@ -184,7 +185,7 @@ export class ApiClient {
184
185
} ,
185
186
body : new URLSearchParams ( {
186
187
client_id : config . clientId ,
187
- refresh_token : ( token || this . token ) ?. refresh_token || "" ,
188
+ refresh_token : token . refresh_token ,
188
189
grant_type : "refresh_token" ,
189
190
scope : "openid profile offline_access" ,
190
191
} ) . toString ( ) ,
@@ -207,7 +208,7 @@ export class ApiClient {
207
208
return await this . storeToken ( tokenToStore ) ;
208
209
}
209
210
210
- async revokeToken ( token ? : OAuthToken ) : Promise < void > {
211
+ async revokeToken ( token : OAuthToken ) : Promise < void > {
211
212
const endpoint = "api/private/unauth/account/device/token" ;
212
213
const url = new URL ( endpoint , config . apiBaseUrl ) ;
213
214
const response = await fetch ( url , {
@@ -219,7 +220,7 @@ export class ApiClient {
219
220
} ,
220
221
body : new URLSearchParams ( {
221
222
client_id : config . clientId ,
222
- token : ( token || this . token ) ? .access_token || "" ,
223
+ token : token . access_token || "" ,
223
224
token_type_hint : "refresh_token" ,
224
225
} ) . toString ( ) ,
225
226
} ) ;
@@ -235,9 +236,8 @@ export class ApiClient {
235
236
return ;
236
237
}
237
238
238
- private checkTokenExpiry ( token ? : OAuthToken ) : boolean {
239
+ private checkTokenExpiry ( token : OAuthToken ) : boolean {
239
240
try {
240
- token = token || this . token ;
241
241
if ( ! token || ! token . access_token ) {
242
242
return false ;
243
243
}
@@ -252,21 +252,25 @@ export class ApiClient {
252
252
}
253
253
}
254
254
255
- async validateToken ( token ?: OAuthToken ) : Promise < boolean > {
256
- if ( this . checkTokenExpiry ( token ) ) {
255
+ async validateToken ( ) : Promise < boolean > {
256
+ if ( ! this . token ) {
257
+ return false ;
258
+ }
259
+
260
+ if ( this . checkTokenExpiry ( this . token ) ) {
257
261
return true ;
258
262
}
259
263
260
264
try {
261
- await this . refreshToken ( token ) ;
265
+ await this . refreshToken ( this . token ) ;
262
266
return true ;
263
267
} catch {
264
268
return false ;
265
269
}
266
270
}
267
271
268
272
async getIpInfo ( ) {
269
- if ( ! ( await this . validateToken ( ) ) ) {
273
+ if ( ! this . token || ! ( await this . validateToken ( ) ) ) {
270
274
throw new Error ( "Not Authenticated" ) ;
271
275
}
272
276
@@ -276,7 +280,7 @@ export class ApiClient {
276
280
method : "GET" ,
277
281
headers : {
278
282
Accept : "application/json" ,
279
- Authorization : `Bearer ${ this . token ! . access_token } ` ,
283
+ Authorization : `Bearer ${ this . token . access_token } ` ,
280
284
"User-Agent" : config . userAgent ,
281
285
} ,
282
286
} ) ;
0 commit comments