secutorpro commited on
Commit
f1f70b6
·
verified ·
1 Parent(s): 67f6d1b

api client

Browse files
Files changed (1) hide show
  1. web/src/api.js +9 -9
web/src/api.js CHANGED
@@ -2,24 +2,24 @@ let token = sessionStorage.getItem('dc_token') || '';
2
 
3
  export function setToken(t) {
4
  token = t || '';
5
- if (token) sessionStorage.setItem('dc_token', token);
6
  else sessionStorage.removeItem('dc_token');
7
  }
8
 
9
- export function getToken() { return token; }
 
 
10
 
11
- async function request(path, options = {}) {
12
- const res = await fetch(`/api${path}`, {
13
- ...options,
14
  headers: {
15
  'Content-Type': 'application/json',
16
  ...(token ? { Authorization: `Bearer ${token}` } : {}),
 
17
  },
18
  });
19
- if (res.status === 401) {
20
- setToken('');
21
- throw new Error('Session expired — log in again');
22
- }
23
  const data = await res.json().catch(() => null);
24
  if (!res.ok) throw new Error(data?.error || res.statusText);
25
  return data;
 
2
 
3
  export function setToken(t) {
4
  token = t || '';
5
+ if (t) sessionStorage.setItem('dc_token', t);
6
  else sessionStorage.removeItem('dc_token');
7
  }
8
 
9
+ export function getToken() {
10
+ return token;
11
+ }
12
 
13
+ async function request(path, opts = {}) {
14
+ const res = await fetch('/api' + path, {
15
+ ...opts,
16
  headers: {
17
  'Content-Type': 'application/json',
18
  ...(token ? { Authorization: `Bearer ${token}` } : {}),
19
+ ...(opts.headers || {}),
20
  },
21
  });
22
+ if (res.status === 401) setToken('');
 
 
 
23
  const data = await res.json().catch(() => null);
24
  if (!res.ok) throw new Error(data?.error || res.statusText);
25
  return data;