26 lines
930 B
JavaScript
26 lines
930 B
JavaScript
/* ============================================================
|
|
api.js — minimal HTTP client for the FastAPI backend
|
|
============================================================ */
|
|
window.Api = {
|
|
base: 'http://localhost:8000',
|
|
|
|
async get(path, params) {
|
|
const url = new URL(path.replace(/^\//, ''), this.base.endsWith('/') ? this.base : this.base + '/');
|
|
if (params) {
|
|
Object.entries(params).forEach(([key, value]) => {
|
|
if (value !== undefined && value !== null && value !== '') {
|
|
url.searchParams.set(key, value);
|
|
}
|
|
});
|
|
}
|
|
const res = await fetch(url.toString());
|
|
let body = null;
|
|
try { body = await res.json(); } catch (_) { body = null; }
|
|
if (!res.ok) {
|
|
const detail = body && body.detail != null ? body.detail : res.statusText;
|
|
throw new Error(typeof detail === 'string' ? detail : JSON.stringify(detail));
|
|
}
|
|
return body;
|
|
}
|
|
};
|