|
|
|
const API_CONFIG = { |
|
|
|
|
|
BASE_URL: |
|
process.env.NODE_ENV === "development" || |
|
window.location.hostname === "localhost" |
|
? "http://localhost:3001" |
|
: window.location.origin, |
|
}; |
|
|
|
|
|
const apiService = { |
|
|
|
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.ok) { |
|
const errorData = await response.json().catch(() => ({})); |
|
throw new Error(errorData.detail || `API error: ${response.status}`); |
|
} |
|
|
|
|
|
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; |
|
} |
|
}, |
|
|
|
|
|
async get(endpoint, options = {}) { |
|
return await this.call(endpoint, { |
|
method: "GET", |
|
...options, |
|
}); |
|
}, |
|
|
|
|
|
async post(endpoint, data, options = {}) { |
|
return await this.call(endpoint, { |
|
method: "POST", |
|
body: JSON.stringify(data), |
|
...options, |
|
}); |
|
}, |
|
|
|
|
|
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: { |
|
|
|
...(options.headers || {}), |
|
}, |
|
}); |
|
|
|
|
|
if (!response.ok) { |
|
const errorData = await response.json().catch(() => ({})); |
|
throw new Error(errorData.detail || `API error: ${response.status}`); |
|
} |
|
|
|
|
|
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; |
|
} |
|
}, |
|
|
|
|
|
async put(endpoint, data, options = {}) { |
|
return await this.call(endpoint, { |
|
method: "PUT", |
|
body: JSON.stringify(data), |
|
...options, |
|
}); |
|
}, |
|
|
|
|
|
async delete(endpoint, options = {}) { |
|
return await this.call(endpoint, { |
|
method: "DELETE", |
|
...options, |
|
}); |
|
}, |
|
|
|
|
|
async uploadFile(file) { |
|
const formData = new FormData(); |
|
formData.append("file", file); |
|
|
|
|
|
try { |
|
const response = await fetch(`${API_CONFIG.BASE_URL}/upload`, { |
|
method: "POST", |
|
body: formData, |
|
|
|
}); |
|
|
|
|
|
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; |
|
} |
|
}, |
|
|
|
|
|
async uploadUrl(url) { |
|
const formData = new FormData(); |
|
formData.append("url", url); |
|
|
|
|
|
try { |
|
const response = await fetch(`${API_CONFIG.BASE_URL}/upload-url`, { |
|
method: "POST", |
|
body: formData, |
|
|
|
}); |
|
|
|
|
|
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; |
|
} |
|
}, |
|
|
|
|
|
async getDocumentContent(docId, extension) { |
|
return await this.get(`/${docId}.${extension}`); |
|
}, |
|
|
|
|
|
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; |
|
|