Files
NetMapper2026/frontend/src/lib/inventoryApi.ts
T

29 lines
1.0 KiB
TypeScript
Raw Normal View History

import type { EntityResponse, InventoryTreeNode, ListResponse, NodeContext, Relation } from '../types/inventory';
async function handleResponse<T>(response: Response): Promise<T> {
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
return response.json() as Promise<T>;
}
async function fetchJson<T>(url: string): Promise<T> {
const response = await fetch(url, { cache: 'no-store' });
return handleResponse<T>(response);
}
export async function fetchInventoryTree(): Promise<InventoryTreeNode[]> {
const data = await fetchJson<ListResponse<InventoryTreeNode>>('/inventory/tree');
return data.items;
}
export async function fetchNodeContext(id: string): Promise<NodeContext> {
const data = await fetchJson<EntityResponse<NodeContext>>(`/inventory/nodes/${id}/context`);
return data.item;
}
export async function fetchNodeRelations(id: string): Promise<Relation[]> {
const data = await fetchJson<ListResponse<Relation>>(`/inventory/nodes/${id}/relations`);
return data.items;
}