Files
gym-bro/frontend/js/api.js
T
2026-09-30 02:25:03 +03:00

45 lines
1.6 KiB
JavaScript

// Базовый URL API. Если фронт и бэк на одном домене, можно использовать '' или '/api'
const API_BASE = 'http://localhost:8000';
export async function fetchWorkoutTypes() {
const res = await fetch(`${API_BASE}/workout-types/`);
if (!res.ok) throw new Error('Failed to fetch workout types');
return await res.json();
}
export async function fetchMetricTypes() {
const res = await fetch(`${API_BASE}/metric-types/`);
if (!res.ok) throw new Error('Failed to fetch metric types');
return await res.json();
}
export async function fetchClients() {
const res = await fetch(`${API_BASE}/clients/`);
if (!res.ok) throw new Error('Failed to fetch clients');
return await res.json();
}
export async function fetchExerciseHistory(exerciseName, clientId = null) {
let url = `${API_BASE}/exercises/history/${encodeURIComponent(exerciseName)}?limit=10`;
if (clientId) url += `&client_id=${clientId}`;
const res = await fetch(url);
if (!res.ok) throw new Error('Failed to fetch history');
return await res.json();
}
export async function syncData(payload) {
const res = await fetch(`${API_BASE}/sync/`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
if (!res.ok) {
const err = await res.json();
console.error('❌ Sync API Error Detail:', err); // <-- Покажет, какое именно поле не прошло валидацию
throw new Error(err.detail ? JSON.stringify(err.detail) : 'Sync failed');
}
return await res.json();
}