import { apiClient } from './client'; /** Représente un nœud dans un workflow n8n */ export interface WorkflowNode { id: string; name: string; type: string; position: [number, number]; parameters: Record; typeVersion?: number; } /** Tag associé à un workflow */ export interface WorkflowTag { id: string; name: string; createdAt: string; updatedAt: string; } /** * Représente un workflow complet tel que retourné par l'API n8n v1. * Les champs `nodes` et `connections` sont présents dans les réponses détaillées. */ export interface Workflow { id: string; name: string; active: boolean; createdAt: string; updatedAt: string; nodes: WorkflowNode[]; connections: Record; settings?: Record; tags?: WorkflowTag[]; } /** * Réponse paginée standard de l'API n8n. * nextCursor est null si on est sur la dernière page. */ export interface ApiListResponse { data: T[]; nextCursor: string | null; } /** * Récupère la liste complète des workflows de l'instance n8n. * * @returns Tableau de tous les workflows (actifs et inactifs) * @throws AxiosError — géré par l'intercepteur, remonte via errorHandler */ export const fetchWorkflows = async (): Promise => { const response = await apiClient.get>('/api/v1/workflows'); return response.data.data; }; /** * Récupère le détail complet d'un workflow, incluant ses nœuds et connexions. * * @param id - Identifiant du workflow * @returns Workflow avec tous ses détails */ export const fetchWorkflowById = async (id: string): Promise => { const response = await apiClient.get(`/api/v1/workflows/${id}`); return response.data; }; /** * Active un workflow : il répondra désormais à ses déclencheurs. * * @param id - Identifiant du workflow à activer * @returns Workflow mis à jour avec active = true */ export const activateWorkflow = async (id: string): Promise => { const response = await apiClient.post(`/api/v1/workflows/${id}/activate`); return response.data; }; /** * Désactive un workflow : ses déclencheurs sont mis en veille. * * @param id - Identifiant du workflow à désactiver * @returns Workflow mis à jour avec active = false */ export const deactivateWorkflow = async (id: string): Promise => { const response = await apiClient.post(`/api/v1/workflows/${id}/deactivate`); return response.data; }; /** Paramètres optionnels pour le déclenchement manuel d'un workflow */ export interface RunWorkflowParams { /** Données injectées dans le nœud de démarrage manuel */ workflowData?: Record; } /** * Déclenche manuellement l'exécution d'un workflow. * * @param id - Identifiant du workflow à déclencher * @param params - Données optionnelles d'entrée * @returns Identifiant de l'exécution créée */ export const runWorkflow = async ( id: string, params?: RunWorkflowParams ): Promise<{ executionId: string }> => { const response = await apiClient.post<{ executionId: string }>( `/api/v1/workflows/${id}/run`, params ?? {} ); return response.data; };