2026-05-12 01:04:17 -05:00
|
|
|
import { queryClient } from './queryClient'
|
|
|
|
|
|
|
|
|
|
const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:3001/api'
|
|
|
|
|
|
|
|
|
|
export const api = {
|
|
|
|
|
get: async (endpoint) => {
|
2026-05-17 15:46:59 -05:00
|
|
|
let response
|
|
|
|
|
try {
|
|
|
|
|
response = await fetch(`${API_BASE_URL}${endpoint}`)
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (err instanceof TypeError && err.message === 'Failed to fetch') {
|
|
|
|
|
throw new Error('Unable to reach the server. This may be a network or CORS issue.')
|
|
|
|
|
}
|
|
|
|
|
throw new Error(`Network error: ${err.message}`)
|
|
|
|
|
}
|
2026-05-12 01:04:17 -05:00
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`API error: ${response.statusText}`)
|
|
|
|
|
}
|
|
|
|
|
return response.json()
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
post: async (endpoint, data) => {
|
2026-05-17 15:46:59 -05:00
|
|
|
let response
|
|
|
|
|
try {
|
|
|
|
|
response = await fetch(`${API_BASE_URL}${endpoint}`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify(data),
|
|
|
|
|
})
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (err instanceof TypeError && err.message === 'Failed to fetch') {
|
|
|
|
|
throw new Error('Unable to reach the server. This may be a network or CORS issue.')
|
|
|
|
|
}
|
|
|
|
|
throw new Error(`Network error: ${err.message}`)
|
|
|
|
|
}
|
2026-05-12 01:04:17 -05:00
|
|
|
if (!response.ok) {
|
2026-05-17 15:46:59 -05:00
|
|
|
let errorData
|
|
|
|
|
try {
|
|
|
|
|
errorData = await response.json()
|
|
|
|
|
} catch {
|
|
|
|
|
throw new Error(`Server error (${response.status}): ${response.statusText}`)
|
|
|
|
|
}
|
2026-05-12 01:04:17 -05:00
|
|
|
throw new Error(errorData.error || `API error: ${response.statusText}`)
|
|
|
|
|
}
|
|
|
|
|
return response.json()
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-17 15:46:59 -05:00
|
|
|
export { queryClient }
|