Spaces:
Sleeping
Sleeping
Create services/api.js
Browse files- frontend/src/services/api.js +59 -0
frontend/src/services/api.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* API service for communicating with the FastAPI backend
|
| 3 |
+
*/
|
| 4 |
+
|
| 5 |
+
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || "";
|
| 6 |
+
|
| 7 |
+
/**
|
| 8 |
+
* Extract data from a document
|
| 9 |
+
* @param {File} file - The file to extract data from
|
| 10 |
+
* @returns {Promise<Object>} Extraction result with fields, confidence, etc.
|
| 11 |
+
*/
|
| 12 |
+
export async function extractDocument(file) {
|
| 13 |
+
const formData = new FormData();
|
| 14 |
+
formData.append("file", file);
|
| 15 |
+
|
| 16 |
+
const response = await fetch(`${API_BASE_URL}/api/extract`, {
|
| 17 |
+
method: "POST",
|
| 18 |
+
body: formData,
|
| 19 |
+
});
|
| 20 |
+
|
| 21 |
+
if (!response.ok) {
|
| 22 |
+
const errorData = await response.json().catch(() => ({
|
| 23 |
+
error: `HTTP ${response.status}: ${response.statusText}`,
|
| 24 |
+
}));
|
| 25 |
+
throw new Error(errorData.error || errorData.detail || "Extraction failed");
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
return await response.json();
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
/**
|
| 32 |
+
* Get extraction history
|
| 33 |
+
* @returns {Promise<Array>} Array of extraction records
|
| 34 |
+
*/
|
| 35 |
+
export async function getHistory() {
|
| 36 |
+
const response = await fetch(`${API_BASE_URL}/api/history`);
|
| 37 |
+
|
| 38 |
+
if (!response.ok) {
|
| 39 |
+
const errorData = await response.json().catch(() => ({
|
| 40 |
+
error: `HTTP ${response.status}: ${response.statusText}`,
|
| 41 |
+
}));
|
| 42 |
+
throw new Error(errorData.error || errorData.detail || "Failed to fetch history");
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
return await response.json();
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
/**
|
| 49 |
+
* Health check endpoint
|
| 50 |
+
* @returns {Promise<Object>} Status object
|
| 51 |
+
*/
|
| 52 |
+
export async function ping() {
|
| 53 |
+
const response = await fetch(`${API_BASE_URL}/ping`);
|
| 54 |
+
if (!response.ok) {
|
| 55 |
+
throw new Error("Backend is not available");
|
| 56 |
+
}
|
| 57 |
+
return await response.json();
|
| 58 |
+
}
|
| 59 |
+
|