import type { EventRecord, PhotoRecord, Preset } from '../types'; export interface Health { ok: boolean; app: string; version: string; needsSetup: boolean; registrationOpen: boolean; photos: number; bytes: number; raw: { libraw: boolean; sharp: boolean; formats: string[] }; } export interface SessionUser { id: string; email: string; name: string; role: string; } export interface UploadResult { raw: boolean; engine?: 'libraw' | 'embedded'; width?: number; height?: number; mime?: string; size?: number; previewUrl?: string; thumbUrl?: string; note?: string | null; } export class ApiError extends Error { status: number; constructor(message: string, status: number) { super(message); this.status = status; } } async function request(path: string, init?: RequestInit): Promise { let res: Response; try { res = await fetch(path, { credentials: 'same-origin', ...init }); } catch { throw new ApiError('Cannot reach the Lumenroom server', 0); } if (!res.ok) { let message = `Request failed (${res.status})`; try { const body = (await res.json()) as { error?: string }; if (body?.error) message = body.error; } catch { /* non-JSON error body */ } throw new ApiError(message, res.status); } if (res.status === 204) return undefined as T; return (await res.json()) as T; } function jsonBody(payload: unknown): RequestInit { return { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload), }; } /** Returns null when the app is running without a server (pure browser library). */ export async function getHealth(): Promise { try { const res = await fetch('/api/health', { credentials: 'same-origin' }); if (!res.ok) return null; const body = (await res.json()) as Health; return body?.ok ? body : null; } catch { return null; } } export async function register(input: { email: string; password: string; name?: string; invite?: string; }): Promise { const res = await request<{ user: SessionUser }>('/api/auth/register', jsonBody(input)); return res.user; } export async function login(email: string, password: string): Promise { const res = await request<{ user: SessionUser }>('/api/auth/login', jsonBody({ email, password })); return res.user; } export async function logout(): Promise { await request('/api/auth/logout', { method: 'POST' }); } export async function currentUser(): Promise { try { const res = await request<{ user: SessionUser }>('/api/auth/me'); return res.user; } catch (err) { if (err instanceof ApiError && err.status === 401) return null; return null; } } export interface SyncPayload { user: SessionUser; events: EventRecord[]; photos: PhotoRecord[]; presets: Preset[]; } export function pullAll(): Promise { return request('/api/sync'); } export function uploadOriginal(id: string, file: File | Blob, name: string): Promise { const query = new URLSearchParams({ name, mime: file.type || 'application/octet-stream' }); return request(`/api/photos/${encodeURIComponent(id)}/original?${query}`, { method: 'POST', headers: { 'Content-Type': 'application/octet-stream' }, body: file, }); } export function pushPhoto(photo: PhotoRecord): Promise { return request<{ photo: PhotoRecord }>(`/api/photos/${encodeURIComponent(photo.id)}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(photo), }).then((res) => res.photo); } export function deletePhotoRemote(id: string): Promise { return request(`/api/photos/${encodeURIComponent(id)}`, { method: 'DELETE' }); } export function pushEvent(event: EventRecord): Promise { return request<{ event: EventRecord }>(`/api/events/${encodeURIComponent(event.id)}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(event), }).then((res) => res.event); } export function deleteEventRemote(id: string): Promise { return request(`/api/events/${encodeURIComponent(id)}`, { method: 'DELETE' }); } export function pushPresets(presets: Preset[]): Promise { return request('/api/presets', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ presets }), }); }