import { a as addToast } from "./toasts.js"; const API_BASE_URL = "/api"; async function fetchApi(endpoint) { try { console.log(`[api.fetchApi][Action] Fetching from context={{'endpoint': '${endpoint}'}}`); const response = await fetch(`${API_BASE_URL}${endpoint}`); if (!response.ok) { throw new Error(`API request failed with status ${response.status}`); } return await response.json(); } catch (error) { console.error(`[api.fetchApi][Coherence:Failed] Error fetching from ${endpoint}:`, error); addToast(error.message, "error"); throw error; } } async function postApi(endpoint, body) { try { console.log(`[api.postApi][Action] Posting to context={{'endpoint': '${endpoint}'}}`); const response = await fetch(`${API_BASE_URL}${endpoint}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) }); if (!response.ok) { throw new Error(`API request failed with status ${response.status}`); } return await response.json(); } catch (error) { console.error(`[api.postApi][Coherence:Failed] Error posting to ${endpoint}:`, error); addToast(error.message, "error"); throw error; } } async function requestApi(endpoint, method = "GET", body = null) { try { console.log(`[api.requestApi][Action] ${method} to context={{'endpoint': '${endpoint}'}}`); const options = { method, headers: { "Content-Type": "application/json" } }; if (body) { options.body = JSON.stringify(body); } const response = await fetch(`${API_BASE_URL}${endpoint}`, options); if (!response.ok) { const errorData = await response.json().catch(() => ({})); throw new Error(errorData.detail || `API request failed with status ${response.status}`); } return await response.json(); } catch (error) { console.error(`[api.requestApi][Coherence:Failed] Error ${method} to ${endpoint}:`, error); addToast(error.message, "error"); throw error; } } const api = { getPlugins: () => fetchApi("/plugins/"), getTasks: () => fetchApi("/tasks/"), getTask: (taskId) => fetchApi(`/tasks/${taskId}`), createTask: (pluginId, params) => postApi("/tasks/", { plugin_id: pluginId, params }), // Settings getSettings: () => fetchApi("/settings/"), updateGlobalSettings: (settings) => requestApi("/settings/global", "PATCH", settings), getEnvironments: () => fetchApi("/settings/environments"), addEnvironment: (env) => postApi("/settings/environments", env), updateEnvironment: (id, env) => requestApi(`/settings/environments/${id}`, "PUT", env), deleteEnvironment: (id) => requestApi(`/settings/environments/${id}`, "DELETE"), testEnvironmentConnection: (id) => postApi(`/settings/environments/${id}/test`, {}) }; export { api as a };