File size: 6,942 Bytes
f1229b6 1c3e70d f1229b6 fd7246b f1229b6 fd7246b f1229b6 fd7246b f1229b6 1c3e70d f1229b6 1c3e70d f1229b6 3fe1ff4 e160839 403d9d3 9d0a717 403d9d3 9d0a717 403d9d3 9d0a717 403d9d3 9d0a717 403d9d3 f1229b6 d7fe8fb | 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | /**
* API service for communicating with the FastAPI backend
*/
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || "";
/**
* Get authorization headers with token
*/
function getAuthHeaders() {
const token = localStorage.getItem("auth_token");
return token ? { Authorization: `Bearer ${token}` } : {};
}
/**
* Extract data from a document
* @param {File} file - The file to extract data from
* @param {string} keyFields - Optional comma-separated list of fields to extract
* @returns {Promise<Object>} Extraction result with fields, confidence, etc.
*/
export async function extractDocument(file, keyFields = "") {
const formData = new FormData();
formData.append("file", file);
if (keyFields && keyFields.trim()) {
formData.append("key_fields", keyFields.trim());
}
const response = await fetch(`${API_BASE_URL}/api/extract`, {
method: "POST",
headers: getAuthHeaders(),
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`, {
headers: getAuthHeaders(),
});
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();
}
/**
* Get a specific extraction by ID with full fields data
* @param {number} extractionId - The extraction ID
* @returns {Promise<Object>} Extraction result with fields
*/
export async function getExtractionById(extractionId) {
const response = await fetch(`${API_BASE_URL}/api/extraction/${extractionId}`, {
headers: getAuthHeaders(),
});
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 extraction");
}
return await response.json();
}
/**
* Create a shareable link for an extraction
* @param {number} extractionId - The extraction ID to share
* @returns {Promise<Object>} Share link result with share_link
*/
export async function createShareLink(extractionId) {
const response = await fetch(`${API_BASE_URL}/api/share/link`, {
method: "POST",
headers: {
"Content-Type": "application/json",
...getAuthHeaders(),
},
body: JSON.stringify({
extraction_id: extractionId,
}),
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({
error: `HTTP ${response.status}: ${response.statusText}`,
}));
throw new Error(errorData.error || errorData.detail || "Failed to create share link");
}
return await response.json();
}
/**
* Share an extraction with another user(s)
* @param {number} extractionId - The extraction ID to share
* @param {string|string[]} recipientEmails - Recipient email address(es) - can be a single email or array of emails
* @returns {Promise<Object>} Share result
*/
export async function shareExtraction(extractionId, recipientEmails) {
// Ensure recipient_emails is always an array
const emailsArray = Array.isArray(recipientEmails) ? recipientEmails : [recipientEmails];
const response = await fetch(`${API_BASE_URL}/api/share`, {
method: "POST",
headers: {
"Content-Type": "application/json",
...getAuthHeaders(),
},
body: JSON.stringify({
extraction_id: extractionId,
recipient_emails: emailsArray,
}),
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({
error: `HTTP ${response.status}: ${response.statusText}`,
}));
throw new Error(errorData.error || errorData.detail || "Failed to share extraction");
}
return await response.json();
}
/**
* Access a shared extraction by token
* @param {string} token - Share token
* @returns {Promise<Object>} Share access result with extraction_id
*/
export async function accessSharedExtraction(token) {
const response = await fetch(`${API_BASE_URL}/api/share/${token}`, {
headers: getAuthHeaders(),
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({
error: `HTTP ${response.status}: ${response.statusText}`,
}));
throw new Error(errorData.error || errorData.detail || "Failed to access shared extraction");
}
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();
}
/**
* Create a new API key
* @param {string} name - User-friendly name for the API key
* @returns {Promise<Object>} API key creation result with api_key
*/
export async function createAPIKey(name) {
const response = await fetch(`${API_BASE_URL}/api/auth/api-key/create`, {
method: "POST",
headers: {
"Content-Type": "application/json",
...getAuthHeaders(),
},
body: JSON.stringify({ name }),
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({
error: `HTTP ${response.status}: ${response.statusText}`,
}));
throw new Error(errorData.error || errorData.detail || "Failed to create API key");
}
return await response.json();
}
/**
* List all API keys for the current user
* @returns {Promise<Object>} API keys list with api_keys array
*/
export async function listAPIKeys() {
const response = await fetch(`${API_BASE_URL}/api/auth/api-keys`, {
headers: getAuthHeaders(),
});
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 API keys");
}
return await response.json();
}
/**
* Delete (deactivate) an API key
* @param {number} keyId - The API key ID to delete
* @returns {Promise<Object>} Deletion result
*/
export async function deleteAPIKey(keyId) {
const response = await fetch(`${API_BASE_URL}/api/auth/api-key/${keyId}`, {
method: "DELETE",
headers: getAuthHeaders(),
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({
error: `HTTP ${response.status}: ${response.statusText}`,
}));
throw new Error(errorData.error || errorData.detail || "Failed to delete API key");
}
return await response.json();
}
|