const TOKEN_KEY = 'dim_admin_token' export function getToken() { return localStorage.getItem(TOKEN_KEY) || '' } export function setToken(t) { localStorage.setItem(TOKEN_KEY, t) } export function clearToken() { localStorage.removeItem(TOKEN_KEY) } export function apiGet(path) { return fetch(path, { headers: { Authorization: `Bearer ${getToken()}` } }) .then(r => r.json()) .then(j => j.data !== undefined ? j.data : j) .catch(e => { throw new Error(e.message) }) } export function apiPost(path, body) { return fetch(path, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${getToken()}` }, body: JSON.stringify(body) }).then(r => r.json()) } export function apiPut(path, body) { return fetch(path, { method: 'PUT', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${getToken()}` }, body: JSON.stringify(body) }).then(r => r.json()) } export function apiDelete(path) { return fetch(path, { method: 'DELETE', headers: { Authorization: `Bearer ${getToken()}` } }).then(r => r.json()) }