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}`);
|
|
|
|
|
}
|
2026-08-04 19:53:12 +00:00
|
|
|
const contentType = response.headers.get('content-type') || '';
|
|
|
|
|
if (!contentType.includes('application/json')) {
|
|
|
|
|
const text = await response.text();
|
|
|
|
|
throw new Error(`API returned non-JSON response: ${text.slice(0, 80)}`);
|
|
|
|
|
}
|
2026-08-04 19:25:39 +00:00
|
|
|
return response.json() as Promise<T>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function fetchInventoryTree(): Promise<InventoryTreeNode[]> {
|
2026-08-04 19:53:12 +00:00
|
|
|
const response = await fetch('http://127.0.0.1:8000/inventory/tree');
|
2026-08-04 19:25:39 +00:00
|
|
|
const data = await handleResponse<ListResponse<InventoryTreeNode>>(response);
|
|
|
|
|
return data.items;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function fetchNodeContext(id: string): Promise<NodeContext> {
|
2026-08-04 19:53:12 +00:00
|
|
|
const response = await fetch(`http://127.0.0.1:8000/inventory/nodes/${id}/context`);
|
2026-08-04 19:25:39 +00:00
|
|
|
const data = await handleResponse<EntityResponse<NodeContext>>(response);
|
|
|
|
|
return data.item;
|
|
|
|
|
}
|