Skip to content

Commit 560083a

Browse files
authoredNov 9, 2024··
Merge pull request #89 from mattdavis90/tidy
chore: tidy privates and tests
2 parents ff065dc + b5b7f1e commit 560083a

File tree

4 files changed

+350
-590
lines changed

4 files changed

+350
-590
lines changed
 

‎package.json

+2
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@
3939
"@eslint/js": "^9.13.0",
4040
"@ianvs/prettier-plugin-sort-imports": "^4.2.1",
4141
"@types/chai": "^5.0.0",
42+
"@types/chai-as-promised": "^8.0.1",
4243
"@types/mocha": "^10.0.9",
4344
"@types/node": "^22.8.1",
4445
"@types/simple-oauth2": "^5.0.7",
4546
"@typescript-eslint/eslint-plugin": "^8.11.0",
4647
"@typescript-eslint/parser": "^8.11.0",
4748
"chai": "^5.1.2",
49+
"chai-as-promised": "^8.0.0",
4850
"eslint": "^9.13.0",
4951
"eslint-config-prettier": "^9.1.0",
5052
"eslint-plugin-tsdoc": "^0.3.0",

‎pnpm-lock.yaml

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/index.ts

+30-26
Original file line numberDiff line numberDiff line change
@@ -107,29 +107,29 @@ export class TadoError extends Error {}
107107
* ```
108108
*/
109109
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;
114114

115115
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 });
119119
}
120120

121-
private async _login(): Promise<void> {
122-
if (!this._username || !this._password) {
121+
async #login(): Promise<void> {
122+
if (!this.#username || !this.#password) {
123123
throw new Error("Please login before using Tado!");
124124
}
125125

126126
const tokenParams = {
127-
username: this._username,
128-
password: this._password,
127+
username: this.#username,
128+
password: this.#password,
129129
scope: "home.user",
130130
};
131131

132-
this._accessToken = await client.getToken(tokenParams);
132+
this.#accessToken = await client.getToken(tokenParams);
133133
}
134134

135135
/**
@@ -142,28 +142,32 @@ export class Tado {
142142
* @returns A promise that resolves when the token has been refreshed or re-obtained.
143143
* @throws {@link TadoError} if no access token is available after attempting to login.
144144
*/
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();
148148
}
149149

150-
if (!this._accessToken) {
150+
if (!this.#accessToken) {
151151
throw new TadoError(`No access token available, even after login in.`);
152152
}
153153

154154
// 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);
156156

157157
if (shouldRefresh) {
158158
try {
159-
this._accessToken = await this._accessToken.refresh();
159+
this.#accessToken = await this.#accessToken.refresh();
160160
} catch (_error) {
161-
this._accessToken = null;
162-
await this._login();
161+
this.#accessToken = undefined;
162+
await this.#login();
163163
}
164164
}
165165
}
166166

167+
get accessToken(): AccessToken | undefined {
168+
return this.#accessToken;
169+
}
170+
167171
/**
168172
* Authenticates a user using the provided public client credentials, username and password.
169173
* For more information see
@@ -174,9 +178,9 @@ export class Tado {
174178
* @returns A promise that resolves when the login process is complete.
175179
*/
176180
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();
180184
}
181185

182186
/**
@@ -190,7 +194,7 @@ export class Tado {
190194
* @returns A promise that resolves to the response data.
191195
*/
192196
async apiCall<R, T = unknown>(url: string, method: Method = "get", data?: T): Promise<R> {
193-
await this._refreshToken();
197+
await this.#refreshToken();
194198

195199
let callUrl = tado_url + url;
196200
if (url.includes("https")) {
@@ -201,9 +205,9 @@ export class Tado {
201205
method: method,
202206
data: data,
203207
headers: {
204-
Authorization: "Bearer " + this._accessToken?.token.access_token,
208+
Authorization: "Bearer " + this.#accessToken?.token.access_token,
205209
},
206-
httpsAgent: this._httpsAgent,
210+
httpsAgent: this.#httpsAgent,
207211
};
208212
if (method !== "get" && method !== "GET") {
209213
request.data = data;

‎test/index.ts

+290-564
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.