Download frontend/src/utils/api.js from parthib07/Portfolio: direct link, hf CLI and curl.
- Browser
- Download file 1.39 kB
-
https://huggingface.co/spaces/parthib07/Portfolio/resolve/main/frontend/src/utils/api.js
- Command line
-
hf download hf://spaces/parthib07/Portfolio/frontend/src/utils/api.js
-
curl -L -o api.js https://huggingface.co/spaces/parthib07/Portfolio/resolve/main/frontend/src/utils/api.js
1.39 kB
| /** | |
| * API utility — all calls to the backend | |
| */ | |
| 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.') | |
| } | |
| // Safely parse JSON — guard against Nginx returning HTML error pages | |
| 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 { | |
| // Non-JSON body (e.g. Nginx 502/504 HTML page) | |
| 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 | |
| } | |