diff --git a/src/api/transit/transitDecrypt.ts b/src/api/transit/transitDecrypt.ts index 1a7ff5e..0ea48f8 100644 --- a/src/api/transit/transitDecrypt.ts +++ b/src/api/transit/transitDecrypt.ts @@ -5,10 +5,14 @@ type DecryptionResult = { plaintext: string; } +type DecryptionPayload = { + ciphertext: string; +} + export async function transitDecrypt( baseMount: string, name: string, - data: string + payload: DecryptionPayload ): Promise { const request = new Request(appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/decrypt/${name}`)), { method: 'POST', @@ -16,7 +20,7 @@ export async function transitDecrypt( 'Content-Type': 'application/json', ...getHeaders(), }, - body: JSON.stringify({ ciphertext: data }) + body: JSON.stringify(payload) }); const response = await fetch(request); if (!response.ok) { diff --git a/src/api/transit/transitEncrypt.ts b/src/api/transit/transitEncrypt.ts index 0d7d271..82f9c17 100644 --- a/src/api/transit/transitEncrypt.ts +++ b/src/api/transit/transitEncrypt.ts @@ -5,10 +5,14 @@ type EncryptionResult = { ciphertext: string; } +type EncryptionPayload = { + plaintext: string; +} + export async function transitEncrypt( baseMount: string, name: string, - data: string + payload: EncryptionPayload ): Promise { const request = new Request(appendAPIURL(removeDoubleSlash(`/v1/${baseMount}/encrypt/${name}`)), { method: 'POST', @@ -16,7 +20,7 @@ export async function transitEncrypt( 'Content-Type': 'application/json', ...getHeaders(), }, - body: JSON.stringify({ plaintext: data }) + body: JSON.stringify(payload) }); const response = await fetch(request); if (!response.ok) { diff --git a/src/pages/Transit/TransitDecrypt.ts b/src/pages/Transit/TransitDecrypt.ts index 363e29f..f5e9313 100644 --- a/src/pages/Transit/TransitDecrypt.ts +++ b/src/pages/Transit/TransitDecrypt.ts @@ -80,7 +80,7 @@ export class TransitDecryptPage extends Page { async transitDecryptFormHandler(): Promise { const formData = new FormData(this.transitDecryptForm); - + const decodeBase64 = formData.get("decodeBase64Checkbox") as string; let ciphertext = formData.get("ciphertext") as string; @@ -90,7 +90,12 @@ export class TransitDecryptPage extends Page { ciphertext = atob((await fileToBase64(ciphertext_file) as string).replace("data:text/plain;base64,", "")); } - transitDecrypt(pageState.currentBaseMount, pageState.currentSecret, ciphertext).then(res => { + try { + let res = await transitDecrypt( + pageState.currentBaseMount, + pageState.currentSecret, + { ciphertext: ciphertext }, + ); let plaintext = res.plaintext; if (decodeBase64 == "on") { plaintext = atob(plaintext); @@ -98,9 +103,9 @@ export class TransitDecryptPage extends Page { const modal = CopyableModal(i18next.t("transit_decrypt_decryption_result_modal_title"), plaintext); document.body.querySelector("#pageContent").appendChild(modal); UIkit.modal(modal).show(); - }).catch(e => { + } catch (e) { setErrorText(`API Error: ${e.message}`); - }); + } } get titleSuffix(): string { diff --git a/src/pages/Transit/TransitEncrypt.ts b/src/pages/Transit/TransitEncrypt.ts index b14844f..2ba028e 100644 --- a/src/pages/Transit/TransitEncrypt.ts +++ b/src/pages/Transit/TransitEncrypt.ts @@ -81,7 +81,7 @@ export class TransitEncryptPage extends Page { }.bind(this)); } - async transitEncryptFormHandler(): Promise { + async transitEncryptFormHandler(): Promise { const formData = new FormData(this.transitEncryptForm); const base64Checkbox = formData.get("base64Checkbox") as string; @@ -96,14 +96,18 @@ export class TransitEncryptPage extends Page { plaintext = base64Checkbox == "on" ? plaintext : btoa(plaintext); } - - transitEncrypt(pageState.currentBaseMount, pageState.currentSecret, plaintext).then(res => { + try { + let res = await transitEncrypt( + pageState.currentBaseMount, + pageState.currentSecret, + { plaintext: plaintext } + ); const modal = CopyableModal(i18next.t("transit_encrypt_encryption_result_modal_title"), res.ciphertext); document.body.querySelector("#pageContent").appendChild(modal); UIkit.modal(modal).show(); - }).catch(e => { + } catch (e) { setErrorText(`API Error: ${e.message}`); - }); + } } get titleSuffix(): string {