const API = "http://localhost:3001/categories"; export interface Category { id: string; name: string; slug: string; parentId?: string | null; children?: Category[]; } export async function getCategoriesTree(): Promise { const res = await fetch(`${API}/`); const data = await res.json(); return Array.isArray(data) ? data : data?.data ?? []; } export async function getCategoriesFlat(): Promise { const res = await fetch(API); const data = await res.json(); return Array.isArray(data) ? data : []; } export async function createCategory(payload: Partial) { return fetch(API, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }); } export async function updateCategory(id: string, payload: Partial) { return fetch(`${API}/${id}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }); } export async function deleteCategory(id: string) { return fetch(`${API}/${id}`, { method: "DELETE" }); } export async function getTree(): Promise { const res = await fetch(`${API}/`); const data = await res.json(); return Array.isArray(data) ? data : data?.data ?? []; } export async function getFlat(): Promise { const res = await fetch(API); const data = await res.json(); return Array.isArray(data) ? data : []; }