All checks were successful
Deploy Development / deploy (push) Successful in 43s
Test Suite / pytest-backend (push) Successful in 29s
Test Suite / lint-backend (push) Successful in 2s
Test Suite / compose-smoke (push) Has been skipped
Test Suite / k6 /api/health Baseline (push) Successful in 18s
Test Suite / playwright-smoke (push) Successful in 12s
45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
const TOKEN_KEY = 'kairo_auth_token'
|
|
|
|
export { TOKEN_KEY }
|
|
|
|
export function getToken() {
|
|
return localStorage.getItem(TOKEN_KEY) || ''
|
|
}
|
|
|
|
export function setToken(token) {
|
|
if (token) localStorage.setItem(TOKEN_KEY, token)
|
|
else localStorage.removeItem(TOKEN_KEY)
|
|
}
|
|
|
|
export function apiHeaders(token = getToken()) {
|
|
const headers = { 'Content-Type': 'application/json' }
|
|
if (token) headers['X-Auth-Token'] = token
|
|
return headers
|
|
}
|
|
|
|
export function parseError(body, fallback) {
|
|
if (!body) return fallback
|
|
if (typeof body.detail === 'string') return body.detail
|
|
if (Array.isArray(body.detail)) {
|
|
return body.detail.map((d) => d.msg || JSON.stringify(d)).join(', ')
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
export async function apiFetch(path, options = {}) {
|
|
const token = options.token ?? getToken()
|
|
const res = await fetch(path, {
|
|
...options,
|
|
headers: { ...apiHeaders(token), ...options.headers },
|
|
})
|
|
const body = await res.json().catch(() => null)
|
|
if (!res.ok) {
|
|
const message = parseError(body, `Anfrage fehlgeschlagen (${res.status})`)
|
|
const err = new Error(message)
|
|
err.status = res.status
|
|
err.body = body
|
|
throw err
|
|
}
|
|
return body
|
|
}
|