feat: 使用 safeClient 封装 API 调用并处理错误提示

This commit is contained in:
2025-12-14 20:40:17 +07:00
parent 9d6bceb6cd
commit f112371efa
2 changed files with 26 additions and 13 deletions

View File

@@ -1,5 +1,6 @@
import type { App } from "@riwa/api-types";
import { treaty } from "@elysiajs/eden";
import { toastController } from "@ionic/vue";
const client = treaty<App>(window.location.origin, {
fetch: {
@@ -7,4 +8,27 @@ const client = treaty<App>(window.location.origin, {
},
});
export async function safeClient<T, E>(
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 };