Files
AutoMarket/frontend/js/api.js
2026-05-15 20:19:47 +03:00

135 lines
3.9 KiB
JavaScript

// frontend/js/api.js
const API_BASE = "/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}` : ''}`, { headers: getHeaders() });
return await handleResponse(res);
}
export async function createCase(data) {
const res = await fetch(`${API_BASE}/cases`, {
method: "POST",
headers: getHeaders(),
body: JSON.stringify(data),
});
return await handleResponse(res);
}
export async function getCase(id) {
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: getHeaders(),
body: JSON.stringify(data),
});
return await handleResponse(res);
}
export async function deleteCase(id) {
const res = await fetch(`${API_BASE}/cases/${id}`, { method: "DELETE", headers: getHeaders() });
return await handleResponse(res);
}
export async function uploadFiles(caseId, fileList) {
const form = new FormData();
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 handleResponse(res);
}
export async function listFiles(caseId) {
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`, { 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: getHeaders(),
body: JSON.stringify(data),
});
return await handleResponse(res);
}