@@ -37,6 +37,8 @@ export interface OAuth2UserOptions {
37
37
scopes : OAuth2Scopes [ ] ;
38
38
/** Overwrite request options for all endpoints */
39
39
request_options ?: Partial < RequestOptions > ;
40
+ /** Set the auth token */
41
+ token ?: Token ;
40
42
}
41
43
42
44
export type GenerateAuthUrlOptions =
@@ -88,17 +90,17 @@ interface GetTokenResponse {
88
90
scope ?: string ;
89
91
}
90
92
91
- interface Token extends Omit < GetTokenResponse , ' expires_in' > {
93
+ interface Token extends Omit < GetTokenResponse , " expires_in" > {
92
94
/** Date that the access_token will expire at. */
93
- expires_at ?: Date ;
95
+ expires_at ?: number ;
94
96
}
95
97
96
98
function processTokenResponse ( token : GetTokenResponse ) : Token {
97
99
const { expires_in, ...rest } = token ;
98
100
return {
99
101
...rest ,
100
102
...( ! ! expires_in && {
101
- expires_at : new Date ( Date . now ( ) + expires_in * 1000 ) ,
103
+ expires_at : Date . now ( ) + expires_in * 1000 ,
102
104
} ) ,
103
105
} ;
104
106
}
@@ -112,7 +114,9 @@ export class OAuth2User implements AuthClient {
112
114
#code_verifier?: string ;
113
115
#code_challenge?: string ;
114
116
constructor ( options : OAuth2UserOptions ) {
115
- this . #options = options ;
117
+ const { token, ...defaultOptions } = options ;
118
+ this . #options = defaultOptions ;
119
+ this . token = token ;
116
120
}
117
121
118
122
/**
@@ -155,10 +159,8 @@ export class OAuth2User implements AuthClient {
155
159
isAccessTokenExpired ( ) : boolean {
156
160
const refresh_token = this . token ?. refresh_token ;
157
161
const expires_at = this . token ?. expires_at ;
158
- return (
159
- ! ! refresh_token &&
160
- ( ! expires_at || expires_at <= new Date ( Date . now ( ) + 1000 ) )
161
- ) ;
162
+ if ( ! expires_at ) return true ;
163
+ return ! ! refresh_token && expires_at <= Date . now ( ) + 1000 ;
162
164
}
163
165
164
166
/**
0 commit comments