import { clearAdminSession, getAdminAccessToken, getAdminRefreshToken, getStoredAdmin, setAdminSession, } from '../auth/adminStorage' import { apiUrl } from '../config/apiBase' async function readDetail(res) { try { const data = await res.json() const d = data.detail if (typeof d === 'string') return d if (Array.isArray(d)) return d.map((e) => e.msg || JSON.stringify(e)).join(' ') return data.message || res.statusText } catch { return res.statusText || 'Request failed' } } async function postAdminRefresh() { const rt = getAdminRefreshToken() if (!rt) return null const res = await fetch(apiUrl('/admin/refresh'), { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ refresh_token: rt }), }) if (!res.ok) return null const text = await res.text() return text ? JSON.parse(text) : null } function redirectToAdminLogin() { clearAdminSession() if (typeof window !== 'undefined') { window.location.assign('/admin/login') } } export async function apiAdminJson(path, options = {}) { const { auth = false, ...fetchOpts } = options function buildHeaders() { const h = { 'Content-Type': 'application/json', ...(fetchOpts.headers || {}), } if (auth) { const access = getAdminAccessToken() if (access) { h.Authorization = `Bearer ${access}` } } return h } const url = apiUrl(path) let res = await fetch(url, { ...fetchOpts, headers: buildHeaders(), }) if (res.status === 401 && auth) { const data = await postAdminRefresh() if (data?.access_token && data?.refresh_token) { setAdminSession({ access_token: data.access_token, refresh_token: data.refresh_token, admin: getStoredAdmin(), }) res = await fetch(url, { ...fetchOpts, headers: buildHeaders(), }) } else { redirectToAdminLogin() throw new Error('Session expired') } } if (!res.ok) { if (res.status === 401 && auth) { redirectToAdminLogin() } throw new Error(await readDetail(res)) } if (res.status === 204) return null const text = await res.text() return text ? JSON.parse(text) : null } /** * Multipart POST; pass a factory so body can be recreated after token refresh. * @param {string} path * @param {() => FormData} formDataFactory * @param {{ auth?: boolean }} options */ export async function apiAdminUpload(path, formDataFactory, options = {}) { const { auth = false } = options function authHeaders() { const h = {} if (auth) { const access = getAdminAccessToken() if (access) { h.Authorization = `Bearer ${access}` } } return h } const uploadUrl = apiUrl(path) let res = await fetch(uploadUrl, { method: 'POST', headers: authHeaders(), body: formDataFactory(), }) if (res.status === 401 && auth) { const data = await postAdminRefresh() if (data?.access_token && data?.refresh_token) { setAdminSession({ access_token: data.access_token, refresh_token: data.refresh_token, admin: getStoredAdmin(), }) res = await fetch(uploadUrl, { method: 'POST', headers: authHeaders(), body: formDataFactory(), }) } else { redirectToAdminLogin() throw new Error('Session expired') } } if (!res.ok) { if (res.status === 401 && auth) { redirectToAdminLogin() } throw new Error(await readDetail(res)) } const text = await res.text() return text ? JSON.parse(text) : null }