wip: слияние прода-версии с портированными фичами (до merge iovi)
This commit is contained in:
+50
-29
@@ -1,48 +1,69 @@
|
||||
// frontend/js/api.js
|
||||
import { apiFetch } from "./auth.js";
|
||||
import { API_BASE } from "./config.js"; // <-- Добавили импорт
|
||||
|
||||
// УДАЛИТЬ эти строки из начала api.js:
|
||||
// export const API_BASE = ...
|
||||
import { apiFetch } from "./auth.js";
|
||||
|
||||
async function jsonOrThrow(res, fallbackMessage) {
|
||||
if (!res.ok) {
|
||||
let detail = fallbackMessage;
|
||||
|
||||
try {
|
||||
const err = await res.json();
|
||||
|
||||
if (typeof err.detail === "string") {
|
||||
detail = err.detail;
|
||||
} else if (Array.isArray(err.detail)) {
|
||||
detail = err.detail
|
||||
.map((d) => `${d.loc?.join(".") || ""}: ${d.msg || ""}`.trim())
|
||||
.join("; ");
|
||||
} else {
|
||||
detail = JSON.stringify(err.detail || err);
|
||||
}
|
||||
} catch {
|
||||
// ignore parse error
|
||||
}
|
||||
|
||||
throw new Error(detail);
|
||||
}
|
||||
|
||||
return await res.json();
|
||||
}
|
||||
|
||||
export async function fetchBootstrapData() {
|
||||
const res = await apiFetch("/bootstrap/");
|
||||
return jsonOrThrow(res, "Failed to fetch bootstrap data");
|
||||
}
|
||||
|
||||
export async function fetchWorkoutTypes() {
|
||||
const res = await apiFetch("/workout-types/");
|
||||
if (!res.ok) throw new Error('Failed to fetch workout types');
|
||||
return await res.json();
|
||||
const res = await apiFetch("/workout-types/");
|
||||
return jsonOrThrow(res, "Failed to fetch workout types");
|
||||
}
|
||||
|
||||
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();
|
||||
const res = await apiFetch("/metric-types/");
|
||||
return jsonOrThrow(res, "Failed to fetch metric types");
|
||||
}
|
||||
|
||||
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();
|
||||
const res = await apiFetch("/clients/");
|
||||
return jsonOrThrow(res, "Failed to fetch clients");
|
||||
}
|
||||
|
||||
export async function fetchExerciseHistory(exerciseName, clientId = null) {
|
||||
let url = `${API_BASE}/exercises/history/${encodeURIComponent(exerciseName)}?limit=10`;
|
||||
if (clientId) url += `&client_id=${clientId}`;
|
||||
let path = `/exercises/history/${encodeURIComponent(exerciseName)}?limit=10`;
|
||||
|
||||
const res = await fetch(url);
|
||||
if (!res.ok) throw new Error('Failed to fetch history');
|
||||
return await res.json();
|
||||
if (clientId) {
|
||||
path += `&client_id=${clientId}`;
|
||||
}
|
||||
|
||||
const res = await apiFetch(path);
|
||||
return jsonOrThrow(res, "Failed to fetch history");
|
||||
}
|
||||
|
||||
export async function syncData(payload) {
|
||||
const res = await fetch(`${API_BASE}/sync/`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
const res = await apiFetch("/sync/", {
|
||||
method: "POST",
|
||||
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();
|
||||
return jsonOrThrow(res, "Sync failed");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user