Files
tvone/lib/categories.api.ts
T
2026-04-18 11:28:23 +01:00

54 lines
1.4 KiB
TypeScript

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<Category[]> {
const res = await fetch(`${API}/`);
const data = await res.json();
return Array.isArray(data) ? data : data?.data ?? [];
}
export async function getCategoriesFlat(): Promise<Category[]> {
const res = await fetch(API);
const data = await res.json();
return Array.isArray(data) ? data : [];
}
export async function createCategory(payload: Partial<Category>) {
return fetch(API, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
}
export async function updateCategory(id: string, payload: Partial<Category>) {
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<Category[]> {
const res = await fetch(`${API}/`);
const data = await res.json();
return Array.isArray(data) ? data : data?.data ?? [];
}
export async function getFlat(): Promise<Category[]> {
const res = await fetch(API);
const data = await res.json();
return Array.isArray(data) ? data : [];
}