// Thin wrapper over the API container. Same origin in production (nginx proxies // /api), the Vite dev server proxies it too — so no base URL, no CORS. import type { Recipe } from '../shared/types'; export interface User { id: number; email: string; } export interface SavedRecipe { id: number; name: string; recipe: unknown; createdAt: string; updatedAt: string; } async function call(path: string, init?: RequestInit): Promise { const res = await fetch(`/api${path}`, { credentials: 'same-origin', headers: init?.body ? { 'content-type': 'application/json' } : undefined, ...init, }); if (res.status === 204) return undefined as T; const text = await res.text(); const body = text ? JSON.parse(text) : {}; if (!res.ok) throw new Error(body.error ?? `HTTP ${res.status}`); return body as T; } export const api = { me: () => call<{ user: User }>('/auth/me'), signup: (email: string, password: string) => call<{ user: User }>('/auth/signup', { method: 'POST', body: JSON.stringify({ email, password }) }), login: (email: string, password: string) => call<{ user: User }>('/auth/login', { method: 'POST', body: JSON.stringify({ email, password }) }), logout: () => call('/auth/logout', { method: 'POST' }), listRecipes: () => call<{ recipes: SavedRecipe[] }>('/recipes'), createRecipe: (name: string, recipe: Recipe) => call<{ recipe: SavedRecipe }>('/recipes', { method: 'POST', body: JSON.stringify({ name, recipe }) }), updateRecipe: (id: number, name: string, recipe: Recipe) => call<{ recipe: SavedRecipe }>(`/recipes/${id}`, { method: 'PUT', body: JSON.stringify({ name, recipe }) }), deleteRecipe: (id: number) => call(`/recipes/${id}`, { method: 'DELETE' }), };