AI Resource Pro / website · airesourcepro.com / src / lib / account-client.ts
| 1 | type Section = "saved" | "profile" | "progress" | "view" | "survey"; |
| 2 | |
| 3 | export 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 | } |