12 lines
1.1 KiB
JavaScript
12 lines
1.1 KiB
JavaScript
const api = {
|
|
base: '',
|
|
token: null,
|
|
setToken(t){ this.token = t; localStorage.setItem('ss_token', t); },
|
|
getToken(){ return this.token || localStorage.getItem('ss_token'); },
|
|
headers(){ const h = { 'Content-Type':'application/json' }; const t=this.getToken(); if(t) h['Authorization']='Bearer '+t; return h; },
|
|
async get(path){ const r = await fetch('/api'+path, { headers: this.headers() }); if(!r.ok) throw new Error(await r.text()); return r.json(); },
|
|
async post(path, body){ const r = await fetch('/api'+path, { method:'POST', headers:this.headers(), body: JSON.stringify(body) }); if(!r.ok) throw new Error(await r.text()); return r.json(); },
|
|
async put(path, body){ const r = await fetch('/api'+path, { method:'PUT', headers:this.headers(), body: JSON.stringify(body) }); if(!r.ok) throw new Error(await r.text()); return r.json(); },
|
|
async del(path, body){ const r = await fetch('/api'+path, { method:'DELETE', headers:this.headers(), body: JSON.stringify(body) }); if(!r.ok) throw new Error(await r.text()); return r.json(); }
|
|
};
|
|
window.SSAPI = api; |