import api from "@/lib/api-client"; import { saveAs } from "file-saver"; import { IBase, IBaseProperty, IBaseRow, IBaseView, CreateBaseInput, UpdateBaseInput, CreatePropertyInput, UpdatePropertyInput, DeletePropertyInput, ReorderPropertyInput, CreateRowInput, UpdateRowInput, DeleteRowInput, DeleteRowsInput, ReorderRowInput, CreateViewInput, UpdateViewInput, DeleteViewInput, UpdatePropertyResult, FilterNode, SearchSpec, ViewSortConfig, RowReferences, } from "@/ee/base/types/base.types"; import { IPagination } from "@/lib/types"; export type IBaseRowsPage = IPagination & { references?: RowReferences; }; export async function createBase(data: CreateBaseInput): Promise { const req = await api.post("/bases/create", data); return req.data; } export async function getBaseInfo(pageId: string): Promise { const req = await api.post("/bases/info", { pageId }); return req.data; } export async function updateBase(data: UpdateBaseInput): Promise { const req = await api.post("/bases/update", data); return req.data; } export async function deleteBase(pageId: string): Promise { await api.post("/bases/delete", { pageId }); } export async function convertPageToBase( pageId: string, template?: "kanban", ): Promise { const req = await api.post("/bases/convert", { pageId, template }); return req.data; } export async function exportBaseToCsv(pageId: string): Promise { const req = await api.post( "/bases/export-csv", { pageId }, { responseType: "blob" }, ); const header = (req?.headers?.["content-disposition"] as string) ?? ""; const utf8Match = header.match(/filename\*=UTF-8''([^;]+)/i); const plainMatch = header.match(/filename="?([^";]+)"?/i); let fileName = utf8Match?.[1] ?? plainMatch?.[1] ?? "base.csv"; try { fileName = decodeURIComponent(fileName); } catch { // fallback to raw filename } saveAs(req.data, fileName); } export async function listBases( spaceId: string, params?: { cursor?: string; limit?: number }, ): Promise> { const req = await api.post("/bases", { spaceId, ...params }); return req.data; } export async function createProperty( data: CreatePropertyInput, ): Promise { const req = await api.post("/bases/properties/create", data); return req.data; } export async function updateProperty( data: UpdatePropertyInput, ): Promise { const req = await api.post( "/bases/properties/update", data, ); return req.data; } export async function deleteProperty(data: DeletePropertyInput): Promise { await api.post("/bases/properties/delete", data); } export async function reorderProperty( data: ReorderPropertyInput, ): Promise { await api.post("/bases/properties/reorder", data); } export async function createRow(data: CreateRowInput): Promise { const req = await api.post("/bases/rows/create", data); return req.data; } export async function getRowInfo( rowId: string, pageId: string, ): Promise { const req = await api.post("/bases/rows/info", { rowId, pageId }); return req.data; } export async function updateRow(data: UpdateRowInput): Promise { const req = await api.post("/bases/rows/update", data); return req.data; } export async function deleteRow(data: DeleteRowInput): Promise { await api.post("/bases/rows/delete", data); } export async function deleteRows(data: DeleteRowsInput): Promise { await api.post("/bases/rows/delete-many", data); } export async function listRows( pageId: string, params?: { cursor?: string; limit?: number; filter?: FilterNode; sorts?: ViewSortConfig[]; search?: SearchSpec; }, ): Promise { const req = await api.post("/bases/rows", { pageId, ...params }); return req.data; } export async function reorderRow(data: ReorderRowInput): Promise { await api.post("/bases/rows/reorder", data); } // --- Views --- export async function createView(data: CreateViewInput): Promise { const req = await api.post("/bases/views/create", data); return req.data; } export async function updateView(data: UpdateViewInput): Promise { const req = await api.post("/bases/views/update", data); return req.data; } export async function deleteView(data: DeleteViewInput): Promise { await api.post("/bases/views/delete", data); } export async function listViews(pageId: string): Promise { const req = await api.post("/bases/views", { pageId }); return req.data; }