UNPKG

n8n-nodes-cleverflow

Version:

n8n node to integrate with CleverFlow API for creating workflow runs and processing Tally Forms

145 lines 6.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getWorkflowFields = exports.getWorkflows = exports.getWorkspaces = void 0; const n8n_workflow_1 = require("n8n-workflow"); async function getWorkspaces() { var _a, _b; try { const credentials = await this.getCredentials('cleverFlowApi'); const baseUrl = credentials.baseUrl ? `${credentials.baseUrl}/api` : 'https://api.cleverflow.com/api'; const timestamp = Date.now(); const url = `/workspace?_nocache=${timestamp}`; const options = { method: 'GET', url, baseURL: baseUrl, headers: { Authorization: `Token ${credentials.apiToken}`, 'Content-Type': 'application/json', 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0' }, }; const responseData = await this.helpers.httpRequest(options); if (!Array.isArray(responseData)) { throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: 'Invalid workspace data received from API' }); } return responseData.map((workspace) => ({ name: workspace.name, value: workspace.name, })); } catch (error) { const errorMessage = ((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || error.message; throw new n8n_workflow_1.NodeApiError(this.getNode(), { message: `Failed to load workspaces: ${errorMessage}` }); } } exports.getWorkspaces = getWorkspaces; async function getWorkflows() { try { const credentials = await this.getCredentials('cleverFlowApi'); const workspaceName = this.getCurrentNodeParameter('workspaceId'); const baseUrl = credentials.baseUrl ? `${credentials.baseUrl}/api` : 'https://api.cleverflow.com/api'; if (!workspaceName) return []; const timestamp1 = Date.now(); const workspaceUrl = `/workspace?_nocache=${timestamp1}`; const workspaces = await this.helpers.httpRequest({ method: 'GET', url: workspaceUrl, baseURL: baseUrl, headers: { Authorization: `Token ${credentials.apiToken}`, 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0' }, }); const workspace = workspaces.find((ws) => ws.name === workspaceName); if (!workspace) return [{ name: 'Workspace Not Found', value: '' }]; const timestamp2 = Date.now(); const workflowsUrl = `/workspace/${workspace.id}/workflows/?_nocache=${timestamp2}`; const workflows = await this.helpers.httpRequest({ method: 'GET', url: workflowsUrl, baseURL: baseUrl, headers: { Authorization: `Token ${credentials.apiToken}`, 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0' }, }); return workflows.map((workflow) => ({ name: `${workflow.name} (${workflow.key})`, value: workflow.key, })); } catch (error) { return [{ name: 'Error Fetching Workflows', value: '' }]; } } exports.getWorkflows = getWorkflows; async function getWorkflowFields() { var _a; try { const credentials = await this.getCredentials('cleverFlowApi'); const workspaceName = this.getCurrentNodeParameter('workspaceId'); const workflowKey = this.getCurrentNodeParameter('workflowId'); const baseUrl = credentials.baseUrl ? `${credentials.baseUrl}/api` : 'https://api.cleverflow.com/api'; if (!workspaceName || !workflowKey) return []; const timestamp = Date.now(); const workspaceUrl = `/workspace?_nocache=${timestamp}`; const workspaces = await this.helpers.httpRequest({ method: 'GET', url: workspaceUrl, baseURL: baseUrl, headers: { Authorization: `Token ${credentials.apiToken}`, 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0' }, }); const workspace = workspaces.find((ws) => ws.name === workspaceName); if (!workspace) return [{ name: 'Workspace Not Found', value: '' }]; const workflowsUrl = `/workspace/${workspace.id}/workflows/?_nocache=${timestamp}`; const workflows = await this.helpers.httpRequest({ method: 'GET', url: workflowsUrl, baseURL: baseUrl, headers: { Authorization: `Token ${credentials.apiToken}`, 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0' }, }); const workflow = (workflows === null || workflows === void 0 ? void 0 : workflows.find((wf) => wf.key === workflowKey)) || null; if (!workflow) return [{ name: 'Workflow Not Found', value: '' }]; const returnData = []; if ((_a = workflow.form) === null || _a === void 0 ? void 0 : _a.sections) { for (const section of workflow.form.sections) { if (section.fields_set) { for (const field of section.fields_set) { returnData.push({ name: field.label, value: field.slug || field.label, }); } } } } return returnData; } catch (error) { return [{ name: 'Error Fetching Fields', value: '' }]; } } exports.getWorkflowFields = getWorkflowFields; //# sourceMappingURL=loadOptions.js.map