/home/techb158/balavpn.abdallabala.com/src/lib
Edit: /home/techb158/balavpn.abdallabala.com/src/lib/api-client.js (1012B)
const BASE = '';
async function request(method, path, body) {
const opts = { method, headers: { 'Content-Type': 'application/json' } };
if (body) opts.body = JSON.stringify(body);
const res = await fetch(`${BASE}${path}`, opts);
const data = res.headers.get('content-type')?.includes('application/json')
? await res.json() : await res.text();
if (!res.ok) throw new Error(data?.error || data?.message || `Request failed (${res.status})`);
return data;
}
export const api = {
get: (path) => request('GET', path),
post: (path, body) => request('POST', path, body),
put: (path, body) => request('PUT', path, body),
patch: (path, body) => request('PATCH', path, body),
del: (path) => request('DELETE', path),
};
export async function getSession() {
try {
const data = await api.get('/api/auth/session');
return data.user || data.actor || null;
} catch { return null; }
}
export async function logout() {
await api.post('/api/auth/logout');
window.location.href = '/';
}