desvergue-de-alexandro
js 25 lines 687 Bytes
Raw
1 const API_BASE_URL = 'http://localhost:8080/api';
2
3 const API = {
4 async request(endpoint, options = {}) {
5 const url = `${API_BASE_URL}${endpoint}`;
6 const token = localStorage.getItem('token');
7
8 const config = {
9 headers: {
10 'Content-Type': 'application/json',
11 ...(token && { Authorization: `Bearer ${token}` })
12 },
13 ...options
14 };
15
16 const response = await fetch(url, config);
17 const data = await response.json().catch(() => null);
18
19 if (!response.ok) {
20 throw new Error(data?.message || `Error ${response.status}`);
21 }
22
23 return data;
24 }
25 };