Spaces:
Running
Running
| /** | |
| * API service for communicating with the FastAPI backend | |
| */ | |
| const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || ""; | |
| /** | |
| * Extract data from a document | |
| * @param {File} file - The file to extract data from | |
| * @returns {Promise<Object>} Extraction result with fields, confidence, etc. | |
| */ | |
| export async function extractDocument(file) { | |
| const formData = new FormData(); | |
| formData.append("file", file); | |
| const response = await fetch(`${API_BASE_URL}/api/extract`, { | |
| method: "POST", | |
| body: formData, | |
| }); | |
| if (!response.ok) { | |
| const errorData = await response.json().catch(() => ({ | |
| error: `HTTP ${response.status}: ${response.statusText}`, | |
| })); | |
| throw new Error(errorData.error || errorData.detail || "Extraction failed"); | |
| } | |
| return await response.json(); | |
| } | |
| /** | |
| * Get extraction history | |
| * @returns {Promise<Array>} Array of extraction records | |
| */ | |
| export async function getHistory() { | |
| const response = await fetch(`${API_BASE_URL}/api/history`); | |
| if (!response.ok) { | |
| const errorData = await response.json().catch(() => ({ | |
| error: `HTTP ${response.status}: ${response.statusText}`, | |
| })); | |
| throw new Error(errorData.error || errorData.detail || "Failed to fetch history"); | |
| } | |
| return await response.json(); | |
| } | |
| /** | |
| * Health check endpoint | |
| * @returns {Promise<Object>} Status object | |
| */ | |
| export async function ping() { | |
| const response = await fetch(`${API_BASE_URL}/ping`); | |
| if (!response.ok) { | |
| throw new Error("Backend is not available"); | |
| } | |
| return await response.json(); | |
| } | |