File size: 1,550 Bytes
f1229b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
 * 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();
}