Files
resman/backend/public/js/api.js
T
2026-04-06 18:21:01 +02:00

50 lines
767 B
JavaScript

const API = "/resman/api"
async function api(url, options = {}, config = {}) {
const showToast = config.showError !== false
const res = await fetch(url, options)
let data
try {
data = await res.json()
} catch {
data = {}
}
if (!res.ok) {
const msg = data.error || "Unbekannter Fehler"
if (showToast) {
showError(msg)
}
const error = new Error(msg)
error.status = res.status
error.data = data
throw error
}
return data
}
window.runAction = async function(action, onSuccess) {
try {
await action()
if (onSuccess) {
await onSuccess()
}
return true
} catch (error) {
if (error && error.silent) {
return false
}
console.error(error)
return false
}
}