57 lines
2.6 KiB
JavaScript
57 lines
2.6 KiB
JavaScript
const API_BASE_URL = import.meta.env?.VITE_API_BASE_URL || "/api";
|
|
|
|
async function request(path, options = {}) {
|
|
const response = await fetch(`${API_BASE_URL}${path}`, {
|
|
credentials: "include",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
...(options.headers || {})
|
|
},
|
|
...options
|
|
});
|
|
|
|
if (response.status === 204) {
|
|
return null;
|
|
}
|
|
|
|
const payload = await response.json();
|
|
|
|
if (!response.ok) {
|
|
throw new Error(payload.error || "Request failed");
|
|
}
|
|
|
|
return payload;
|
|
}
|
|
|
|
export const api = {
|
|
me: () => request("/auth/me"),
|
|
login: (body) => request("/auth/login", { method: "POST", body: JSON.stringify(body) }),
|
|
logout: () => request("/auth/logout", { method: "POST" }),
|
|
tables: () => request("/db/tables"),
|
|
createTable: (body) => request("/db/tables", { method: "POST", body: JSON.stringify(body) }),
|
|
deleteTable: (tableName) => request(`/db/tables/${tableName}`, { method: "DELETE" }),
|
|
tableDetails: (tableName) => request(`/db/tables/${tableName}/details`),
|
|
rows: (tableName, query = {}) => {
|
|
const params = new URLSearchParams(query).toString();
|
|
return request(`/db/tables/${tableName}/rows${params ? `?${params}` : ""}`);
|
|
},
|
|
createRow: (tableName, body) => request(`/db/tables/${tableName}/rows`, { method: "POST", body: JSON.stringify(body) }),
|
|
updateRow: (tableName, id, body) =>
|
|
request(`/db/tables/${tableName}/rows/${id}`, { method: "PUT", body: JSON.stringify(body) }),
|
|
deleteRow: (tableName, id) => request(`/db/tables/${tableName}/rows/${id}`, { method: "DELETE" }),
|
|
addColumn: (tableName, body) =>
|
|
request(`/db/tables/${tableName}/columns`, { method: "POST", body: JSON.stringify(body) }),
|
|
alterColumn: (tableName, columnName, body) =>
|
|
request(`/db/tables/${tableName}/columns/${columnName}`, { method: "PATCH", body: JSON.stringify(body) }),
|
|
dropColumn: (tableName, columnName) => request(`/db/tables/${tableName}/columns/${columnName}`, { method: "DELETE" }),
|
|
createIndex: (tableName, body) =>
|
|
request(`/db/tables/${tableName}/indexes`, { method: "POST", body: JSON.stringify(body) }),
|
|
dropIndex: (tableName, indexName) => request(`/db/tables/${tableName}/indexes/${indexName}`, { method: "DELETE" }),
|
|
executeSql: (sql) => request("/sql/execute", { method: "POST", body: JSON.stringify({ sql }) }),
|
|
users: () => request("/admin/users"),
|
|
roles: () => request("/admin/roles"),
|
|
audit: (search = "") => request(`/admin/audit${search ? `?search=${encodeURIComponent(search)}` : ""}`),
|
|
postgresLogs: (search = "") =>
|
|
request(`/admin/postgres-logs${search ? `?search=${encodeURIComponent(search)}` : ""}`)
|
|
};
|