webapp/src/services/taskService.ts

55 lines
1.6 KiB
TypeScript

import { getConfig } from './configService';
import { RunTaskRequest, RunTaskApiResponse, Task } from '../types/task';
import api from './api';
// 获取任务列表
export const getTasks = async (params: {
pageNum: number;
pageSize: number;
}): Promise<Task[]> => {
try {
const config = await getConfig();
const endpoint = config?.apiEndpoints?.getTasks;
if (!endpoint) throw new Error('获取任务列表的API端点未配置');
const response = await api.get(endpoint, { params });
return response as unknown as Task[];
} catch (error) {
console.error('获取任务列表失败 (service):', error);
throw error;
}
};
// 获取任务详情
export const getTaskDetail = async (taskId: string): Promise<Task> => {
try {
const config = await getConfig();
let endpoint = config?.apiEndpoints?.getTaskDetail;
if (!endpoint) throw new Error('获取任务详情的API端点未配置');
endpoint = endpoint.replace('{taskId}', taskId);
const response = await api.get(endpoint);
return response as unknown as Task;
} catch (error) {
console.error(`获取任务详情失败 (ID: ${taskId}):`, error);
throw error;
}
};
// 运行任务
export const runTask = async (
taskData: RunTaskRequest,
): Promise<RunTaskApiResponse> => {
try {
const config = await getConfig();
const endpoint = config?.apiEndpoints?.runTask;
if (!endpoint) throw new Error('运行任务的API端点未配置');
const response = await api.post(endpoint, taskData);
return response as unknown as RunTaskApiResponse;
} catch (error) {
console.error('运行任务失败:', error);
throw error;
}
};