Files
electronics-inventory/public/lib/api.mjs
mikael-lovqvists-claude-agent ef2e53ea18 Initial electronics inventory webapp
KV-store backed Express 5 app for tracking electronic components,
their arbitrary fields, and inventory locations (physical, BOM, digital).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-20 19:11:13 +00:00

30 lines
1.3 KiB
JavaScript

async function req(method, path, body) {
const opts = { method, headers: {} };
if (body !== undefined) {
opts.headers['Content-Type'] = 'application/json';
opts.body = JSON.stringify(body);
}
const res = await fetch(path, opts);
const data = await res.json();
if (!data.ok) throw new Error(data.error ?? 'Request failed');
return data;
}
// Fields
export const get_fields = () => req('GET', '/api/fields');
export const create_field = (body) => req('POST', '/api/fields', body);
export const update_field = (id, body) => req('PUT', `/api/fields/${id}`, body);
export const delete_field = (id) => req('DELETE', `/api/fields/${id}`);
// Components
export const get_components = () => req('GET', '/api/components');
export const create_component = (body) => req('POST', '/api/components', body);
export const update_component = (id, body) => req('PUT', `/api/components/${id}`, body);
export const delete_component = (id) => req('DELETE', `/api/components/${id}`);
// Inventory
export const get_inventory = () => req('GET', '/api/inventory');
export const create_inventory = (body) => req('POST', '/api/inventory', body);
export const update_inventory = (id, body) => req('PUT', `/api/inventory/${id}`, body);
export const delete_inventory = (id) => req('DELETE', `/api/inventory/${id}`);