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