| |
| |
| |
| const API_BASE = '/api' |
|
|
| export async function submitContact(data) { |
| let response |
| try { |
| response = await fetch(`${API_BASE}/contact`, { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json', |
| }, |
| body: JSON.stringify(data), |
| }) |
| } catch (networkErr) { |
| throw new Error('Network error β please check your connection and try again.') |
| } |
|
|
| |
| let json = null |
| const contentType = response.headers.get('content-type') || '' |
| if (contentType.includes('application/json')) { |
| try { |
| json = await response.json() |
| } catch { |
| throw new Error('Server returned an unexpected response. Please try again.') |
| } |
| } else { |
| |
| const text = await response.text().catch(() => '') |
| console.error('[API] Non-JSON response from server:', response.status, text.slice(0, 200)) |
| throw new Error( |
| response.status === 502 || response.status === 503 |
| ? 'The server is temporarily unavailable. Please try again in a moment.' |
| : `Unexpected server response (${response.status}). Please try again.` |
| ) |
| } |
|
|
| if (!response.ok) { |
| throw new Error((json && json.error) || 'Something went wrong. Please try again.') |
| } |
|
|
| return json |
| } |
|
|