2026-08-04 19:25:39 +00:00
|
|
|
import type { EntityResponse, InventoryTreeNode, ListResponse, NodeContext } 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>;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-04 19:54:48 +00:00
|
|
|
async function fetchJson<T>(url: string): Promise<T> {
|
|
|
|
|
const response = await fetch(url, { cache: 'no-store' });
|
|
|
|
|
return handleResponse<T>(response);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-04 19:25:39 +00:00
|
|
|
export async function fetchInventoryTree(): Promise<InventoryTreeNode[]> {
|
2026-08-04 19:54:48 +00:00
|
|
|
const data = await fetchJson<ListResponse<InventoryTreeNode>>('/inventory/tree');
|
2026-08-04 19:25:39 +00:00
|
|
|
return data.items;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function fetchNodeContext(id: string): Promise<NodeContext> {
|
2026-08-04 19:54:48 +00:00
|
|
|
const data = await fetchJson<EntityResponse<NodeContext>>(`/inventory/nodes/${id}/context`);
|
2026-08-04 19:25:39 +00:00
|
|
|
return data.item;
|
|
|
|
|
}
|