diff --git a/src/api/index.ts b/src/api/index.ts index 16c0cd7..923ac73 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,5 +1,6 @@ import type { App } from "@riwa/api-types"; import { treaty } from "@elysiajs/eden"; +import { toastController } from "@ionic/vue"; const client = treaty(window.location.origin, { fetch: { @@ -7,4 +8,27 @@ const client = treaty(window.location.origin, { }, }); +export async function safeClient( + requestPromise: Promise<{ data: T; error: E | null }>, + options: { silent?: boolean } = {}, +) { + const { data, error } = await requestPromise; + + if (error) { + if (!options.silent) { + const toast = await toastController.create({ + message: typeof error === "string" ? error : "Request failed. Please try again.", + duration: 3000, + position: "bottom", + color: "danger", + }); + await toast.present(); + } + + throw error; + } + + return { data, error }; +} + export { client }; diff --git a/src/views/withdraw/index.vue b/src/views/withdraw/index.vue index 677d122..7d292d4 100644 --- a/src/views/withdraw/index.vue +++ b/src/views/withdraw/index.vue @@ -1,7 +1,7 @@