Files
ss-tools/frontend/src/services/toolsService.js
2026-01-18 21:29:54 +03:00

52 lines
1.6 KiB
JavaScript

/**
* Service for generic Task API communication used by Tools.
*/
const API_BASE = '/api/tasks';
// [DEF:runTask:Function]
/* @PURPOSE: Start a new task for a given plugin.
@PRE: pluginId and params must be provided.
@POST: Returns a promise resolving to the task instance.
*/
/**
* Start a new task for a given plugin.
* @param {string} pluginId - The ID of the plugin to run.
* @param {Object} params - Parameters for the plugin.
* @returns {Promise<Object>} The created task instance.
*/
export async function runTask(pluginId, params) {
const response = await fetch(API_BASE, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ plugin_id: pluginId, params })
});
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.detail || `Failed to start task: ${response.statusText}`);
}
return await response.json();
}
// [/DEF:runTask:Function]
// [DEF:getTaskStatus:Function]
/* @PURPOSE: Fetch details for a specific task (to poll status or get result).
@PRE: taskId must be provided.
@POST: Returns a promise resolving to task details.
*/
/**
* Fetch details for a specific task (to poll status or get result).
* @param {string} taskId - The ID of the task.
* @returns {Promise<Object>} Task details.
*/
export async function getTaskStatus(taskId) {
const response = await fetch(`${API_BASE}/${taskId}`);
if (!response.ok) {
throw new Error(`Failed to fetch task ${taskId}: ${response.statusText}`);
}
return await response.json();
}
// [/DEF:getTaskStatus:Function]