const BASE = '/cashup/api' async function req(method: string, path: string, body?: unknown): Promise { const res = await fetch(BASE + path, { method, credentials: 'include', headers: body ? { 'Content-Type': 'application/json' } : undefined, body: body ? JSON.stringify(body) : undefined, }) if (!res.ok) { const err = await res.json().catch(() => ({ error: res.statusText })) throw new Error((err as { error?: string }).error || res.statusText) } return res.json() } export const api = { get: (path: string) => req('GET', path), post: (path: string, body: unknown) => req('POST', path, body), put: (path: string, body: unknown) => req('PUT', path, body), delete: (path: string) => req('DELETE', path), } export async function uploadAttachment( cashUpId: number, file: File, attachmentType: 'pdq_z_report' | 'receipt_error' | 'other' = 'other', label?: string, ) { const fd = new FormData() fd.append('attachment_type', attachmentType) if (label) fd.append('label', label) fd.append('file', file) const res = await fetch(`${BASE}/attachments/upload/${cashUpId}`, { method: 'POST', credentials: 'include', body: fd, }) if (!res.ok) { const err = await res.json().catch(() => ({ error: res.statusText })) throw new Error((err as { error?: string }).error || res.statusText) } return res.json() }