Files
famdone/frontend/src/lib/client/api.ts
T

44 lines
1.6 KiB
TypeScript

// Client-only. All /api calls go same-origin (vite proxy in dev, nginx in
// prod). Server-side (SSR) calls use PROXY_URL from $app/env/public instead.
const BASE_URL = '';
async function memberFetch<T = unknown>(
method: string,
path: string,
token: string,
famId: string,
body?: unknown,
): Promise<T> {
const headers: Record<string, string> = {
'x-device-token': token,
'x-device-famid': famId,
};
if (body !== undefined) headers['Content-Type'] = 'application/json';
const res = await fetch(`${BASE_URL}${path}`, {
method,
headers,
body: body !== undefined ? JSON.stringify(body) : undefined,
});
const data = await res.json();
if (!res.ok) throw new Error(data.error || `${method} ${path} failed`);
return data as T;
}
export const memberApi = {
async toggleCompletion(token: string, famId: string, assignedChoreId: string, date: string) {
return memberFetch('POST', '/api/completions/toggle', token, famId, { assignedChoreId, date });
},
async myChores(token: string, famId: string) {
return memberFetch('POST', '/api/members/my-chores', token, famId);
},
async claimReward(token: string, famId: string, rewardId: string) {
return memberFetch('POST', `/api/members/rewards/${rewardId}/claim`, token, famId);
},
async requestAllRewards(token: string, famId: string) {
return memberFetch<{ count: number }>('POST', '/api/members/rewards/request-all', token, famId);
},
async payday(token: string, famId: string) {
return memberFetch('POST', `/api/fam/${famId}/payday`, token, famId);
},
};