feat: 使用 safeClient 封装 API 调用并处理错误提示
This commit is contained in:
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user