metis-desk/modules/assets/src/web/shared/api/inventory-request.ts

23 lines
745 B
TypeScript
Raw Normal View History

2026-10-10 10:42:56 +00:00
import { csrfFetch } from "@metis/core/client";
import { z } from "zod/v4";
import { InventoryApiError } from "./inventory-api-error";
2026-10-10 10:42:56 +00:00
export async function inventoryRequest(path: string, body?: unknown) {
const response = await csrfFetch(`/api/assets/${path}`, {
method: body === undefined ? "GET" : "POST",
credentials: "same-origin",
2026-10-10 10:42:56 +00:00
headers: body === undefined ? {} : { "content-type": "application/json" },
...(body === undefined ? {} : { body: JSON.stringify(body) }),
});
const data: unknown = await response.json();
2026-10-10 10:01:58 +00:00
if (!response.ok) {
const error = z.object({ code: z.string() }).safeParse(data);
2026-10-10 10:01:58 +00:00
throw new InventoryApiError(error.success ? error.data.code : "INTERNAL_ERROR");
}
2026-10-10 10:01:58 +00:00
return data;
}