WIP: Staged all changes

This commit is contained in:
2025-12-19 22:40:28 +03:00
parent 8f4b469c96
commit ce703322c2
64 changed files with 5985 additions and 833 deletions

55
frontend/src/lib/api.js Normal file
View File

@@ -0,0 +1,55 @@
import { addToast } from './toasts.js';
const API_BASE_URL = 'http://localhost:8000';
/**
* Fetches data from the API.
* @param {string} endpoint The API endpoint to fetch data from.
* @returns {Promise<any>} The JSON response from the API.
*/
async function fetchApi(endpoint) {
try {
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(`Error fetching from ${endpoint}:`, error);
addToast(error.message, 'error');
throw error;
}
}
/**
* Posts data to the API.
* @param {string} endpoint The API endpoint to post data to.
* @param {object} body The data to post.
* @returns {Promise<any>} The JSON response from the API.
*/
async function postApi(endpoint, body) {
try {
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(`Error posting to ${endpoint}:`, error);
addToast(error.message, 'error');
throw error;
}
}
export const api = {
getPlugins: () => fetchApi('/plugins'),
getTasks: () => fetchApi('/tasks'),
getTask: (taskId) => fetchApi(`/tasks/${taskId}`),
createTask: (pluginId, params) => postApi('/tasks', { plugin_id: pluginId, params }),
};