export async function apiFetch(path, opts={}){
    const token = localStorage.getItem('atulya_token');
    const headers = new Headers(opts.headers || {});
    if (!headers.get('Content-Type')) headers.set('Content-Type','application/json');
    if (token) headers.set('Authorization','Bearer '+token);
    const res = await fetch('/api/'+path, {...opts, headers});
    if (res.status===401){
        localStorage.removeItem('atulya_token');
        window.location = '/public/login.html';
        throw new Error('unauthorized');
    }
    return res.json();
}
