Files
Schule-Sportanmeldung-Frontend/js/api.js
DOMINIK SCHRADER 3b8e5c2b09 Initial Commit
2025-12-01 11:32:31 +01:00

62 lines
1.6 KiB
JavaScript

const API = {
async request(endpoint, options = {}) {
const url = `${API_CONFIG.baseUrl}${endpoint}`;
try {
const response = await fetch(url, {
headers: {
'Content-Type': 'application/json',
...(options.headers || {}),
},
credentials: 'include',
...options,
});
const text = await response.text();
if (!response.ok) {
let error;
try {
error = JSON.parse(text);
} catch {
error = { message: text || 'Unbekannter Fehler' };
}
throw new Error(error.message || 'API-Fehler');
}
return text ? JSON.parse(text) : null;
} catch (err) {
console.error('API error:', err);
throw err;
}
},
getKurse() {
return this.request(API_CONFIG.endpoints.kurse);
},
getBetriebe() {
return this.request(API_CONFIG.endpoints.betriebe);
},
anmelden(data) {
return this.request(API_CONFIG.endpoints.anmeldung, {
method: 'POST',
body: JSON.stringify(data),
});
},
getBerichtBetrieb() {
return this.request(API_CONFIG.endpoints.berichtBetrieb);
},
getBerichtKurs() {
return this.request(API_CONFIG.endpoints.berichtKurs);
},
getRechnung(betriebId) {
return this.request(`${API_CONFIG.endpoints.rechnung}${betriebId}`);
},
login(email, password) {
return this.request(API_CONFIG.endpoints.login, {
method: 'POST',
body: JSON.stringify({ email, password }),
});
},
logout() {
return this.request(API_CONFIG.endpoints.logout, { method: 'POST' });
},
me() {
return this.request(API_CONFIG.endpoints.me);
},
};