@@ -36,7 +36,7 @@ export class JwtTokenManager {
36
36
protected tokenName : string ;
37
37
protected disableSslVerification : boolean ;
38
38
protected headers : OutgoingHttpHeaders ;
39
- protected requestWrapperInstance ;
39
+ protected requestWrapperInstance : RequestWrapper ;
40
40
private tokenInfo : any ;
41
41
private expireTime : number ;
42
42
@@ -70,33 +70,24 @@ export class JwtTokenManager {
70
70
}
71
71
72
72
/**
73
- * This function sends an access token back through a callback. The source of the token
74
- * is determined by the following logic:
73
+ * This function returns a Promise that resolves with an access token, if successful.
74
+ * The source of the token is determined by the following logic:
75
75
* 1. If user provides their own managed access token, assume it is valid and send it
76
76
* 2. a) If this class is managing tokens and does not yet have one, make a request for one
77
77
* b) If this class is managing tokens and the token has expired, request a new one
78
78
* 3. If this class is managing tokens and has a valid token stored, send it
79
79
*
80
- * @param {Function } cb - callback function that the token will be passed to
81
80
*/
82
- public getToken ( cb : Function ) {
81
+ public getToken ( ) : Promise < any > {
83
82
if ( ! this . tokenInfo [ this . tokenName ] || this . isTokenExpired ( ) ) {
84
83
// 1. request a new token
85
- this . requestToken ( ( err , tokenResponse ) => {
86
- if ( ! err ) {
87
- try {
88
- this . saveTokenInfo ( tokenResponse . result ) ;
89
- } catch ( e ) {
90
- // send lower level error through callback for user to handle
91
- err = e ;
92
- }
93
- }
94
- // return null for access_token if there is an error
95
- return cb ( err , this . tokenInfo [ this . tokenName ] || null ) ;
84
+ return this . requestToken ( ) . then ( tokenResponse => {
85
+ this . saveTokenInfo ( tokenResponse . result ) ;
86
+ return this . tokenInfo [ this . tokenName ] ;
96
87
} ) ;
97
88
} else {
98
89
// 2. use valid, managed token
99
- return cb ( null , this . tokenInfo [ this . tokenName ] ) ;
90
+ return Promise . resolve ( this . tokenInfo [ this . tokenName ] ) ;
100
91
}
101
92
}
102
93
@@ -129,12 +120,11 @@ export class JwtTokenManager {
129
120
/**
130
121
* Request a JWT using an API key.
131
122
*
132
- * @param {Function } cb - The callback that handles the response.
133
- * @returns {void }
123
+ * @returns {Promise }
134
124
*/
135
- protected requestToken ( cb : Function ) : void {
125
+ protected requestToken ( ) : Promise < any > {
136
126
const err = new Error ( '`requestToken` MUST be overridden by a subclass of JwtTokenManagerV1.' ) ;
137
- cb ( err , null ) ;
127
+ return Promise . reject ( err ) ;
138
128
}
139
129
140
130
/**
0 commit comments