Initial project scaffold: FastAPI backend + Svelte 5 frontend
Backend (uv): FastAPI app with routers (foods, log, targets, OFF proxy), SQLAlchemy models, Pydantic schemas, migration runner + 0001 initial schema, example pytest suite (7 tests). Frontend (npm): Vite 7 + Svelte 5 runes, lib/ (api, stores, scanner, format) per spec §7, placeholder components, example vitest suite (4 tests). SPEC.md §1: registered uvicorn and Vitest (rule §8.3.3).
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* All HTTP goes through this module (spec §8.2 rule 1).
|
||||
* No raw fetch() in components. Shapes mirror backend schemas.py (spec §8.3 rule 1).
|
||||
*/
|
||||
|
||||
const BASE_URL = '' // same origin; Vite dev server proxies /api to the backend
|
||||
|
||||
async function request(path, options = {}) {
|
||||
const resp = await fetch(`${BASE_URL}${path}`, {
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
...options,
|
||||
})
|
||||
if (!resp.ok) {
|
||||
throw new Error(`API ${options.method ?? 'GET'} ${path} failed: ${resp.status}`)
|
||||
}
|
||||
return resp.json()
|
||||
}
|
||||
|
||||
export const api = {
|
||||
health: () => request('/api/health'),
|
||||
// Foods (spec §3.1)
|
||||
searchFoods: (q) => request(`/api/foods?q=${encodeURIComponent(q)}`),
|
||||
recentFoods: (limit = 10) => request(`/api/foods/recent?limit=${limit}`),
|
||||
getFood: (id) => request(`/api/foods/${id}`),
|
||||
createFood: (food) => request('/api/foods', { method: 'POST', body: JSON.stringify(food) }),
|
||||
// Daily log (spec §3.3) — dates are YYYY-MM-DD strings end-to-end (spec §8.3 rule 4)
|
||||
getLog: (date) => request(`/api/log?date=${date}`),
|
||||
addLogEntry: (entry) => request('/api/log', { method: 'POST', body: JSON.stringify(entry) }),
|
||||
deleteLogEntry: (id) => request(`/api/log/${id}`, { method: 'DELETE' }),
|
||||
getSummary: (date) => request(`/api/log/summary?date=${date}`),
|
||||
// Targets (spec §3.4)
|
||||
getCurrentTarget: () => request('/api/targets/current'),
|
||||
// OFF proxy (spec §3.5) — the frontend never calls OFF directly
|
||||
offProduct: (barcode) => request(`/api/off/product/${barcode}`),
|
||||
offSearch: (q) => request(`/api/off/search?q=${encodeURIComponent(q)}`),
|
||||
}
|
||||
Reference in New Issue
Block a user