feat: implement core backend API structure and frontend service layer for case management and authentication
This commit is contained in:
@@ -1,39 +1,101 @@
|
||||
// frontend/js/api.js
|
||||
// Base URL for backend API (adjust if using different host/port)
|
||||
const API_BASE = "http://localhost:5000/api";
|
||||
|
||||
function getHeaders() {
|
||||
const token = localStorage.getItem('token');
|
||||
const headers = { "Content-Type": "application/json" };
|
||||
if (token) headers["Authorization"] = `Bearer ${token}`;
|
||||
return headers;
|
||||
}
|
||||
|
||||
async function handleResponse(res) {
|
||||
if (res.status === 401) {
|
||||
localStorage.removeItem('token');
|
||||
window.location.reload();
|
||||
}
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error || data.message || 'API Error');
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function login(username, password) {
|
||||
const res = await fetch(`${API_BASE}/auth/login`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ username, password }),
|
||||
});
|
||||
return await handleResponse(res);
|
||||
}
|
||||
|
||||
export async function register(username, password) {
|
||||
const res = await fetch(`${API_BASE}/auth/register`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ username, password }),
|
||||
});
|
||||
return await handleResponse(res);
|
||||
}
|
||||
|
||||
export async function getMe() {
|
||||
const res = await fetch(`${API_BASE}/auth/me`, { headers: getHeaders() });
|
||||
return await handleResponse(res);
|
||||
}
|
||||
|
||||
export async function upgradePlan() {
|
||||
const res = await fetch(`${API_BASE}/auth/upgrade`, {
|
||||
method: "POST",
|
||||
headers: getHeaders()
|
||||
});
|
||||
return await handleResponse(res);
|
||||
}
|
||||
|
||||
export async function getDashboardStats() {
|
||||
const res = await fetch(`${API_BASE}/dashboard/stats`, { headers: getHeaders() });
|
||||
return await handleResponse(res);
|
||||
}
|
||||
|
||||
export function getCaseReportPdfUrl(caseId) {
|
||||
const token = localStorage.getItem('token');
|
||||
return `${API_BASE}/reports/case/${caseId}/pdf?token=${token}`;
|
||||
}
|
||||
|
||||
export function getExportCsvUrl() {
|
||||
const token = localStorage.getItem('token');
|
||||
return `${API_BASE}/reports/cases/csv?token=${token}`;
|
||||
}
|
||||
|
||||
export async function fetchCases(params = {}) {
|
||||
const query = new URLSearchParams(params).toString();
|
||||
const res = await fetch(`${API_BASE}/cases${query ? `?${query}` : ''}`);
|
||||
return await res.json();
|
||||
const res = await fetch(`${API_BASE}/cases${query ? `?${query}` : ''}`, { headers: getHeaders() });
|
||||
return await handleResponse(res);
|
||||
}
|
||||
|
||||
export async function createCase(data) {
|
||||
const res = await fetch(`${API_BASE}/cases`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
headers: getHeaders(),
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return await res.json();
|
||||
return await handleResponse(res);
|
||||
}
|
||||
|
||||
export async function getCase(id) {
|
||||
const res = await fetch(`${API_BASE}/cases/${id}`);
|
||||
return await res.json();
|
||||
const res = await fetch(`${API_BASE}/cases/${id}`, { headers: getHeaders() });
|
||||
return await handleResponse(res);
|
||||
}
|
||||
|
||||
export async function updateCase(id, data) {
|
||||
const res = await fetch(`${API_BASE}/cases/${id}`, {
|
||||
method: "PUT",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
headers: getHeaders(),
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return await res.json();
|
||||
return await handleResponse(res);
|
||||
}
|
||||
|
||||
export async function deleteCase(id) {
|
||||
const res = await fetch(`${API_BASE}/cases/${id}`, { method: "DELETE" });
|
||||
return await res.json();
|
||||
const res = await fetch(`${API_BASE}/cases/${id}`, { method: "DELETE", headers: getHeaders() });
|
||||
return await handleResponse(res);
|
||||
}
|
||||
|
||||
export async function uploadFiles(caseId, fileList) {
|
||||
@@ -41,29 +103,32 @@ export async function uploadFiles(caseId, fileList) {
|
||||
for (const f of fileList) {
|
||||
form.append("files", f);
|
||||
}
|
||||
const headers = getHeaders();
|
||||
delete headers["Content-Type"]; // let browser set boundary
|
||||
const res = await fetch(`${API_BASE}/cases/${caseId}/files`, {
|
||||
method: "POST",
|
||||
headers: headers,
|
||||
body: form,
|
||||
});
|
||||
return await res.json();
|
||||
return await handleResponse(res);
|
||||
}
|
||||
|
||||
export async function listFiles(caseId) {
|
||||
const res = await fetch(`${API_BASE}/cases/${caseId}/files`);
|
||||
return await res.json();
|
||||
const res = await fetch(`${API_BASE}/cases/${caseId}/files`, { headers: getHeaders() });
|
||||
return await handleResponse(res);
|
||||
}
|
||||
|
||||
export async function getConfiguredPlatforms() {
|
||||
const res = await fetch(`${API_BASE}/credentials`);
|
||||
const data = await res.json();
|
||||
const res = await fetch(`${API_BASE}/credentials`, { headers: getHeaders() });
|
||||
const data = await handleResponse(res);
|
||||
return data.configured_platforms || [];
|
||||
}
|
||||
|
||||
export async function saveCredentials(platform, data) {
|
||||
const res = await fetch(`${API_BASE}/credentials/${platform}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
headers: getHeaders(),
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return await res.json();
|
||||
return await handleResponse(res);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user