Branchwell

AI Resource Pro / website · airesourcepro.com / src / lib / account-client.ts

Raw 722 B on 5069836c1e4c4e369ebe86b13dc7d299c5bed8c0
1type Section = "saved" | "profile" | "progress" | "view" | "survey";
2
3export async function accountRequest<T>(section: Section, body?: object): Promise<
4 { data: T; error: null } | { data: null; error: string }
5> {
6 try {
7 const result = await fetch(`/api/account/${section}`, body === undefined ? undefined : {
8 method: "POST",
9 headers: { "Content-Type": "application/json" },
10 body: JSON.stringify(body),
11 });
12 const payload = await result.json();
13 if (!result.ok) return { data: null, error: payload.error ?? "Could not update your account." };
14 return { data: payload.data, error: null };
15 } catch {
16 return { data: null, error: "Could not reach your account. Try again." };
17 }
18}