Spaces:
Sleeping
Sleeping
| const API_BASE = process.env.REACT_APP_BACKEND_URL || ''; | |
| export async function apiCall(endpoint, data, options = {}) { | |
| const { timeout = 30000 } = options; | |
| const controller = new AbortController(); | |
| const timer = setTimeout(() => controller.abort(), timeout); | |
| try { | |
| const res = await fetch(`${API_BASE}${endpoint}`, { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify(data), | |
| signal: controller.signal, | |
| }); | |
| clearTimeout(timer); | |
| if (!res.ok) throw new Error(`HTTP ${res.status}`); | |
| return await res.json(); | |
| } catch (err) { | |
| clearTimeout(timer); | |
| if (err.name === 'AbortError') throw new Error('Request timed out'); | |
| throw err; | |
| } | |
| } | |