@@ -107,29 +107,29 @@ export class TadoError extends Error {}
107
107
* ```
108
108
*/
109
109
export class Tado {
110
- private _httpsAgent : Agent ;
111
- private _accessToken ?: AccessToken | null ;
112
- private _username ?: string ;
113
- private _password ?: string ;
110
+ #httpsAgent : Agent ;
111
+ #accessToken ?: AccessToken | undefined ;
112
+ #username ?: string ;
113
+ #password ?: string ;
114
114
115
115
constructor ( username ?: string , password ?: string ) {
116
- this . _username = username ;
117
- this . _password = password ;
118
- this . _httpsAgent = new Agent ( { keepAlive : true } ) ;
116
+ this . #username = username ;
117
+ this . #password = password ;
118
+ this . #httpsAgent = new Agent ( { keepAlive : true } ) ;
119
119
}
120
120
121
- private async _login ( ) : Promise < void > {
122
- if ( ! this . _username || ! this . _password ) {
121
+ async #login ( ) : Promise < void > {
122
+ if ( ! this . #username || ! this . #password ) {
123
123
throw new Error ( "Please login before using Tado!" ) ;
124
124
}
125
125
126
126
const tokenParams = {
127
- username : this . _username ,
128
- password : this . _password ,
127
+ username : this . #username ,
128
+ password : this . #password ,
129
129
scope : "home.user" ,
130
130
} ;
131
131
132
- this . _accessToken = await client . getToken ( tokenParams ) ;
132
+ this . #accessToken = await client . getToken ( tokenParams ) ;
133
133
}
134
134
135
135
/**
@@ -142,28 +142,32 @@ export class Tado {
142
142
* @returns A promise that resolves when the token has been refreshed or re-obtained.
143
143
* @throws {@link TadoError } if no access token is available after attempting to login.
144
144
*/
145
- private async _refreshToken ( ) : Promise < void > {
146
- if ( ! this . _accessToken ) {
147
- await this . _login ( ) ;
145
+ async #refreshToken ( ) : Promise < void > {
146
+ if ( ! this . #accessToken ) {
147
+ await this . #login ( ) ;
148
148
}
149
149
150
- if ( ! this . _accessToken ) {
150
+ if ( ! this . #accessToken ) {
151
151
throw new TadoError ( `No access token available, even after login in.` ) ;
152
152
}
153
153
154
154
// If the start of the window has passed, refresh the token
155
- const shouldRefresh = this . _accessToken . expired ( EXPIRATION_WINDOW_IN_SECONDS ) ;
155
+ const shouldRefresh = this . #accessToken . expired ( EXPIRATION_WINDOW_IN_SECONDS ) ;
156
156
157
157
if ( shouldRefresh ) {
158
158
try {
159
- this . _accessToken = await this . _accessToken . refresh ( ) ;
159
+ this . #accessToken = await this . #accessToken . refresh ( ) ;
160
160
} catch ( _error ) {
161
- this . _accessToken = null ;
162
- await this . _login ( ) ;
161
+ this . #accessToken = undefined ;
162
+ await this . #login ( ) ;
163
163
}
164
164
}
165
165
}
166
166
167
+ get accessToken ( ) : AccessToken | undefined {
168
+ return this . #accessToken;
169
+ }
170
+
167
171
/**
168
172
* Authenticates a user using the provided public client credentials, username and password.
169
173
* For more information see
@@ -174,9 +178,9 @@ export class Tado {
174
178
* @returns A promise that resolves when the login process is complete.
175
179
*/
176
180
async login ( username : string , password : string ) : Promise < void > {
177
- this . _username = username ;
178
- this . _password = password ;
179
- await this . _login ( ) ;
181
+ this . #username = username ;
182
+ this . #password = password ;
183
+ await this . #login ( ) ;
180
184
}
181
185
182
186
/**
@@ -190,7 +194,7 @@ export class Tado {
190
194
* @returns A promise that resolves to the response data.
191
195
*/
192
196
async apiCall < R , T = unknown > ( url : string , method : Method = "get" , data ?: T ) : Promise < R > {
193
- await this . _refreshToken ( ) ;
197
+ await this . #refreshToken ( ) ;
194
198
195
199
let callUrl = tado_url + url ;
196
200
if ( url . includes ( "https" ) ) {
@@ -201,9 +205,9 @@ export class Tado {
201
205
method : method ,
202
206
data : data ,
203
207
headers : {
204
- Authorization : "Bearer " + this . _accessToken ?. token . access_token ,
208
+ Authorization : "Bearer " + this . #accessToken ?. token . access_token ,
205
209
} ,
206
- httpsAgent : this . _httpsAgent ,
210
+ httpsAgent : this . #httpsAgent ,
207
211
} ;
208
212
if ( method !== "get" && method !== "GET" ) {
209
213
request . data = data ;
0 commit comments