diff --git a/.eslintrc.json b/.eslintrc.json index 0ec9a70..e62016e 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -28,6 +28,12 @@ "argsIgnorePattern": "^_" } ], + "@typescript-eslint/no-misused-promises": [ + "error", + { + "checksVoidReturn": false + } + ], "@typescript-eslint/require-await": ["off"], "@typescript-eslint/no-empty-function": ["off"], "@typescript-eslint/ban-ts-comment": ["off"], diff --git a/src/pages/KeyValue/KeyValueNew.ts b/src/pages/KeyValue/KeyValueNew.ts index f27048c..2a1b67a 100644 --- a/src/pages/KeyValue/KeyValueNew.ts +++ b/src/pages/KeyValue/KeyValueNew.ts @@ -53,9 +53,9 @@ export class KeyValueNewPage extends Page { }) as HTMLFormElement; setPageContent(this.addKVNewForm); - this.addKVNewForm.addEventListener("submit", (e: Event) => { + this.addKVNewForm.addEventListener("submit", async (e: Event) => { e.preventDefault(); - void this.newKVSecretHandleForm(); + await this.newKVSecretHandleForm(); }); } diff --git a/src/pages/Login.ts b/src/pages/Login.ts index 5a6b4fa..6698487 100644 --- a/src/pages/Login.ts +++ b/src/pages/Login.ts @@ -132,37 +132,42 @@ export class LoginPage extends Page { }), ); - tokenLoginForm.addEventListener("submit", function (e) { + tokenLoginForm.addEventListener("submit", async function (e) { e.preventDefault(); const formData = new FormData(tokenLoginForm); const token = formData.get("token"); pageState.token = token as string; - lookupSelf() - .then((_) => { - void changePage("HOME"); - }) - .catch((e: Error) => { - document.getElementById("tokenInput").classList.add("uk-form-danger"); - if (e.message == "permission denied") { - setErrorText(i18next.t("token_login_error")); - } else { - setErrorText(e.message); - } - }); + + try { + await lookupSelf(); + await changePage("HOME"); + } catch (e: unknown) { + const error = e as Error; + document.getElementById("tokenInput").classList.add("uk-form-danger"); + if (error.message == "permission denied") { + setErrorText(i18next.t("token_login_error")); + } else { + setErrorText(error.message); + } + } }); - usernameLoginForm.addEventListener("submit", function (e) { + usernameLoginForm.addEventListener("submit", async function (e) { e.preventDefault(); const formData = new FormData(usernameLoginForm); - usernameLogin(formData.get("username") as string, formData.get("password") as string) - .then((res) => { - pageState.token = res; - void changePage("HOME"); - }) - .catch((e: Error) => { - document.getElementById("usernameInput").classList.add("uk-form-danger"); - document.getElementById("passwordInput").classList.add("uk-form-danger"); - setErrorText(e.message); - }); + + try { + const res = await usernameLogin( + formData.get("username") as string, + formData.get("password") as string, + ); + pageState.token = res; + await changePage("HOME"); + } catch (e: unknown) { + const error = e as Error; + document.getElementById("usernameInput").classList.add("uk-form-danger"); + document.getElementById("passwordInput").classList.add("uk-form-danger"); + setErrorText(error.message); + } }); } diff --git a/src/pages/SetLanguage.ts b/src/pages/SetLanguage.ts index 2686584..d2eea59 100644 --- a/src/pages/SetLanguage.ts +++ b/src/pages/SetLanguage.ts @@ -52,17 +52,17 @@ export class SetLanguagePage extends Page { ], }) as HTMLFormElement; setPageContent(setLanguageForm); - setLanguageForm.addEventListener("submit", function (e) { + setLanguageForm.addEventListener("submit", async function (e) { e.preventDefault(); const formData = new FormData(setLanguageForm); + const language = formData.get("language") as string; pageState.language = language; - console.log(pageState.language); - void i18next.changeLanguage(language).then((t) => { - pageState.pageDirection = t("language_direction"); - reloadNavBar(); - void changePage("HOME"); - }); + + const t = await i18next.changeLanguage(language); + pageState.pageDirection = t("language_direction"); + reloadNavBar(); + await changePage("HOME"); }); } get name(): string { diff --git a/src/pages/SetVaultURL.ts b/src/pages/SetVaultURL.ts index 14dbaac..e7d35f2 100644 --- a/src/pages/SetVaultURL.ts +++ b/src/pages/SetVaultURL.ts @@ -43,11 +43,11 @@ export class SetVaultURLPage extends Page { ], }), ); - document.getElementById("setVaultURLForm").addEventListener("submit", function (e) { + document.getElementById("setVaultURLForm").addEventListener("submit", async function (e) { e.preventDefault(); const formData = new FormData(document.querySelector("#setVaultURLForm")); pageState.apiURL = formData.get("vaultURL") as string; - void changePage("HOME"); + await changePage("HOME"); }); } get name(): string { diff --git a/src/pages/TOTP/NewTOTP.ts b/src/pages/TOTP/NewTOTP.ts index 2d8deb7..e61f59f 100644 --- a/src/pages/TOTP/NewTOTP.ts +++ b/src/pages/TOTP/NewTOTP.ts @@ -86,22 +86,25 @@ export class NewTOTPPage extends Page { }) as HTMLFormElement; setPageContent(totpForm); - totpForm.addEventListener("submit", function (e) { + totpForm.addEventListener("submit", async function (e) { e.preventDefault(); + const formData = new FormData(totpForm); + const parms = { url: formData.get("uri") as string, key: removeDashSpaces(formData.get("key") as string).toUpperCase(), name: formData.get("name") as string, generate: false, }; - addNewTOTP(pageState.currentBaseMount, parms) - .then((_) => { - void changePage("TOTP"); - }) - .catch((e: Error) => { - setErrorText(`API Error: ${e.message}`); - }); + + try { + await addNewTOTP(pageState.currentBaseMount, parms); + await changePage("TOTP"); + } catch (e: unknown) { + const error = e as Error; + setErrorText(`API Error: ${error.message}`); + } }); } diff --git a/src/pages/TOTP/TOTPView.ts b/src/pages/TOTP/TOTPView.ts index 17b5a12..7ff324f 100644 --- a/src/pages/TOTP/TOTPView.ts +++ b/src/pages/TOTP/TOTPView.ts @@ -52,7 +52,6 @@ export class TOTPViewPage extends Page { try { const res = await getTOTPKeys(pageState.currentBaseMount); for (const totpKeyName of res) { - console.log(totpKeyName); const totpListElement = this.makeTOTPListElement(totpKeyName); totpList.appendChild(totpListElement); this.totpListElements[totpKeyName] = totpListElement; diff --git a/src/pages/Transit/TransitDecrypt.ts b/src/pages/Transit/TransitDecrypt.ts index 37abc1b..5349a21 100644 --- a/src/pages/Transit/TransitDecrypt.ts +++ b/src/pages/Transit/TransitDecrypt.ts @@ -75,9 +75,9 @@ export class TransitDecryptPage extends Page { ], }) as HTMLFormElement; setPageContent(this.transitDecryptForm); - this.transitDecryptForm.addEventListener("submit", (e: Event) => { + this.transitDecryptForm.addEventListener("submit", async (e: Event) => { e.preventDefault(); - void this.transitDecryptFormHandler(); + await this.transitDecryptFormHandler(); }); } diff --git a/src/pages/Transit/TransitEncrypt.ts b/src/pages/Transit/TransitEncrypt.ts index 574464f..d638b9c 100644 --- a/src/pages/Transit/TransitEncrypt.ts +++ b/src/pages/Transit/TransitEncrypt.ts @@ -76,9 +76,9 @@ export class TransitEncryptPage extends Page { }) as HTMLFormElement; setPageContent(this.transitEncryptForm); - this.transitEncryptForm.addEventListener("submit", (e: Event) => { + this.transitEncryptForm.addEventListener("submit", async (e: Event) => { e.preventDefault(); - void this.transitEncryptFormHandler(); + await this.transitEncryptFormHandler(); }); } diff --git a/src/pages/Transit/TransitRewrap.ts b/src/pages/Transit/TransitRewrap.ts index 17f8a08..0998a82 100644 --- a/src/pages/Transit/TransitRewrap.ts +++ b/src/pages/Transit/TransitRewrap.ts @@ -84,9 +84,9 @@ export class TransitRewrapPage extends Page { }) as HTMLFormElement; setPageContent(this.transitRewrapForm); - this.transitRewrapForm.addEventListener("submit", (e: Event) => { + this.transitRewrapForm.addEventListener("submit", async (e: Event) => { e.preventDefault(); - void this.transitRewrapFormHandler(); + await this.transitRewrapFormHandler(); }); } diff --git a/src/pages/Unseal.ts b/src/pages/Unseal.ts index 6ea4dbc..a53e182 100644 --- a/src/pages/Unseal.ts +++ b/src/pages/Unseal.ts @@ -49,7 +49,7 @@ export class UnsealPage extends Page { async doRefresh(): Promise { const status = await getSealStatus(); - this.updateSealProgress(status); + await this.updateSealProgress(status); } async render(): Promise { @@ -84,7 +84,7 @@ export class UnsealPage extends Page { }), ); this.switchInputMode(this.mode); - this.updateSealProgress(await getSealStatus()); + await this.updateSealProgress(await getSealStatus()); this.makeRefresher(); } @@ -150,13 +150,13 @@ export class UnsealPage extends Page { } async makeQRInput(): Promise { - this.qrScanner = await QRScanner((code: string) => { - this.submitKey(code); + this.qrScanner = await QRScanner(async (code: string) => { + await this.submitKey(code); }); this.unsealInputContent.appendChild(this.qrScanner); } - updateSealProgress(data: SealStatusType): void { + async updateSealProgress(data: SealStatusType): Promise { const progress = data.progress; const keysNeeded = data.t; const text = this.unsealProgressText; @@ -169,26 +169,24 @@ export class UnsealPage extends Page { progressBar.max = keysNeeded; if (!data.sealed) { progressBar.value = keysNeeded; - void changePage("HOME"); + await changePage("HOME"); } } - submitKey(key: string): void { - submitUnsealKey(key) - .then((_) => { - void getSealStatus().then((data) => { - void this.updateSealProgress(data); - }); - }) - .catch((e: Error) => { - setErrorText(e.message); - }); + async submitKey(key: string): Promise { + try { + await submitUnsealKey(key); + await this.updateSealProgress(await getSealStatus()); + } catch (e: unknown) { + const error = e as Error; + setErrorText(error.message); + } } - handleKeySubmit(): void { + async handleKeySubmit(): Promise { const formData = new FormData(this.unsealKeyForm); - this.submitKey(formData.get("key") as string); + await this.submitKey(formData.get("key") as string); } get name(): string { return i18next.t("unseal_vault_text");