28 lines
1.0 KiB
TypeScript
28 lines
1.0 KiB
TypeScript
import { apiFetch } from './api';
|
|
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>(path: string): Promise<T> {
|
|
const response = await apiFetch(path, { 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;
|
|
}
|