tfrere's picture
minor design fixes | update upload url api calls
f8362f3
raw
history blame
5.34 kB
// API Configuration and Service
const API_CONFIG = {
// Use the current origin in production
// In development, use localhost:3001 explicitly
BASE_URL:
process.env.NODE_ENV === "development" ||
window.location.hostname === "localhost"
? "http://localhost:3001"
: window.location.origin,
};
// API Service for making HTTP requests
const apiService = {
// Generic API call method with error handling
async call(endpoint, options = {}) {
try {
const url = endpoint.startsWith("http")
? endpoint
: `${API_CONFIG.BASE_URL}${endpoint}`;
const response = await fetch(url, {
...options,
headers: {
"Content-Type": "application/json",
...options.headers,
},
});
// If response is not ok, throw an error
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.detail || `API error: ${response.status}`);
}
// Check if response is JSON or text
const contentType = response.headers.get("content-type");
if (contentType && contentType.includes("application/json")) {
return await response.json();
}
return await response.text();
} catch (error) {
console.error("API call failed:", error);
throw error;
}
},
// GET request
async get(endpoint, options = {}) {
return await this.call(endpoint, {
method: "GET",
...options,
});
},
// POST request with JSON data
async post(endpoint, data, options = {}) {
return await this.call(endpoint, {
method: "POST",
body: JSON.stringify(data),
...options,
});
},
// POST request with FormData
async postFormData(endpoint, formData, options = {}) {
try {
const url = endpoint.startsWith("http")
? endpoint
: `${API_CONFIG.BASE_URL}${endpoint}`;
const response = await fetch(url, {
method: "POST",
body: formData,
...options,
headers: {
// Ne pas définir Content-Type pour FormData
...(options.headers || {}),
},
});
// Si response is not ok, throw an error
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.detail || `API error: ${response.status}`);
}
// Check if response is JSON or text
const contentType = response.headers.get("content-type");
if (contentType && contentType.includes("application/json")) {
return await response.json();
}
return await response.text();
} catch (error) {
console.error("API FormData call failed:", error);
throw error;
}
},
// PUT request
async put(endpoint, data, options = {}) {
return await this.call(endpoint, {
method: "PUT",
body: JSON.stringify(data),
...options,
});
},
// DELETE request
async delete(endpoint, options = {}) {
return await this.call(endpoint, {
method: "DELETE",
...options,
});
},
// Upload a file
async uploadFile(file) {
const formData = new FormData();
formData.append("file", file);
// Utilisation directe de fetch pour éviter les problèmes avec les en-têtes de FormData
try {
const response = await fetch(`${API_CONFIG.BASE_URL}/upload`, {
method: "POST",
body: formData,
// Ne pas définir le Content-Type pour FormData
});
// Si response is not ok, throw an error
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.detail || `API error: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error("Upload file failed:", error);
throw error;
}
},
// Upload content from a URL
async uploadUrl(url) {
const formData = new FormData();
formData.append("url", url);
// Utilisation directe de fetch pour éviter les problèmes avec les en-têtes de FormData
try {
const response = await fetch(`${API_CONFIG.BASE_URL}/upload-url`, {
method: "POST",
body: formData,
// Ne pas définir le Content-Type pour FormData
});
// Si response is not ok, throw an error
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.detail || `API error: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error("Upload URL failed:", error);
throw error;
}
},
// Get static document content
async getDocumentContent(docId, extension) {
return await this.get(`/${docId}.${extension}`);
},
// Download document
downloadDocument(docId, extension, documentName) {
try {
const link = document.createElement("a");
link.href = `/${docId}.${extension}`;
link.setAttribute("download", `${documentName}.${extension}`);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
return true;
} catch (error) {
console.error("Download document failed:", error);
throw error;
}
},
};
export { API_CONFIG, apiService };
export default API_CONFIG;