40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import { SERVER_IP, PROXY_PORT } from '../../../../config.ts';
|
|
|
|
const BASE_URL = typeof window === 'undefined'
|
|
? `http://${SERVER_IP}:${PROXY_PORT}`
|
|
: '';
|
|
|
|
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);
|
|
},
|
|
};
|