Skip to content

chore: tidy privates and tests #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@
"@eslint/js": "^9.13.0",
"@ianvs/prettier-plugin-sort-imports": "^4.2.1",
"@types/chai": "^5.0.0",
"@types/chai-as-promised": "^8.0.1",
"@types/mocha": "^10.0.9",
"@types/node": "^22.8.1",
"@types/simple-oauth2": "^5.0.7",
"@typescript-eslint/eslint-plugin": "^8.11.0",
"@typescript-eslint/parser": "^8.11.0",
"chai": "^5.1.2",
"chai-as-promised": "^8.0.0",
"eslint": "^9.13.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-tsdoc": "^0.3.0",
Expand Down
28 changes: 28 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 30 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,29 +107,29 @@ export class TadoError extends Error {}
* ```
*/
export class Tado {
private _httpsAgent: Agent;
private _accessToken?: AccessToken | null;
private _username?: string;
private _password?: string;
#httpsAgent: Agent;
#accessToken?: AccessToken | undefined;
#username?: string;
#password?: string;

constructor(username?: string, password?: string) {
this._username = username;
this._password = password;
this._httpsAgent = new Agent({ keepAlive: true });
this.#username = username;
this.#password = password;
this.#httpsAgent = new Agent({ keepAlive: true });
}

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

const tokenParams = {
username: this._username,
password: this._password,
username: this.#username,
password: this.#password,
scope: "home.user",
};

this._accessToken = await client.getToken(tokenParams);
this.#accessToken = await client.getToken(tokenParams);
}

/**
Expand All @@ -142,28 +142,32 @@ export class Tado {
* @returns A promise that resolves when the token has been refreshed or re-obtained.
* @throws {@link TadoError} if no access token is available after attempting to login.
*/
private async _refreshToken(): Promise<void> {
if (!this._accessToken) {
await this._login();
async #refreshToken(): Promise<void> {
if (!this.#accessToken) {
await this.#login();
}

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

// If the start of the window has passed, refresh the token
const shouldRefresh = this._accessToken.expired(EXPIRATION_WINDOW_IN_SECONDS);
const shouldRefresh = this.#accessToken.expired(EXPIRATION_WINDOW_IN_SECONDS);

if (shouldRefresh) {
try {
this._accessToken = await this._accessToken.refresh();
this.#accessToken = await this.#accessToken.refresh();
} catch (_error) {
this._accessToken = null;
await this._login();
this.#accessToken = undefined;
await this.#login();
}
}
}

get accessToken(): AccessToken | undefined {
return this.#accessToken;
}

/**
* Authenticates a user using the provided public client credentials, username and password.
* For more information see
Expand All @@ -174,9 +178,9 @@ export class Tado {
* @returns A promise that resolves when the login process is complete.
*/
async login(username: string, password: string): Promise<void> {
this._username = username;
this._password = password;
await this._login();
this.#username = username;
this.#password = password;
await this.#login();
}

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

let callUrl = tado_url + url;
if (url.includes("https")) {
Expand All @@ -201,9 +205,9 @@ export class Tado {
method: method,
data: data,
headers: {
Authorization: "Bearer " + this._accessToken?.token.access_token,
Authorization: "Bearer " + this.#accessToken?.token.access_token,
},
httpsAgent: this._httpsAgent,
httpsAgent: this.#httpsAgent,
};
if (method !== "get" && method !== "GET") {
request.data = data;
Expand Down
Loading