UNPKG

n8n-nodes-xcredentialsx

Version:

Enhanced n8n nodes for credentials management and improved node execution with dropdown selection

201 lines (200 loc) 9.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.XCredentialsX = void 0; class XCredentialsX { constructor() { this.description = { displayName: 'X Credentials X', name: 'xCredentialsX', icon: 'fa:key', group: ['utility'], version: 1, subtitle: '={{$parameter["operation"]}}', description: 'List all Credentials IDs used in n8n workflows', defaults: { name: 'X Credentials X', }, inputs: ["main" /* NodeConnectionType.Main */], outputs: ["main" /* NodeConnectionType.Main */], credentials: [ { name: 'n8nApi', required: true, }, ], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, options: [ { name: 'List All Workflow Credentials', value: 'listWorkflowCredentials', description: 'Get all Credentials IDs used in workflows', action: 'List all workflow credentials', }, { name: 'List Credentials by Workflow', value: 'listByWorkflow', description: 'Get Credentials IDs grouped by workflow', action: 'List credentials by workflow', }, { name: 'Get Unique Credentials', value: 'getUniqueCredentials', description: 'Get unique Credentials IDs across all workflows', action: 'Get unique credentials', }, ], default: 'listWorkflowCredentials', }, { displayName: 'Include Workflow Details', name: 'includeWorkflowDetails', type: 'boolean', displayOptions: { show: { operation: ['listWorkflowCredentials', 'listByWorkflow'], }, }, default: true, description: 'Whether to include workflow name and ID in the output', }, { displayName: 'Include Node Details', name: 'includeNodeDetails', type: 'boolean', displayOptions: { show: { operation: ['listWorkflowCredentials', 'listByWorkflow'], }, }, default: false, description: 'Whether to include node name and type that uses the credential', }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; const operation = this.getNodeParameter('operation', 0); for (let i = 0; i < items.length; i++) { try { const includeWorkflowDetails = this.getNodeParameter('includeWorkflowDetails', i, true); const includeNodeDetails = this.getNodeParameter('includeNodeDetails', i, false); // Get all workflows using n8n's API with authentication const workflows = await this.helpers.requestWithAuthentication.call(this, 'n8nApi', { method: 'GET', url: '/rest/workflows', json: true, }); const credentialsData = []; const uniqueCredentials = new Set(); // Process each workflow for (const workflow of workflows.data || workflows) { if (workflow.nodes) { // Parse workflow nodes to find credentials for (const node of workflow.nodes) { if (node.credentials) { // Iterate through all credential types used by this node for (const [credentialType, credentialConfig] of Object.entries(node.credentials)) { const credentialId = credentialConfig === null || credentialConfig === void 0 ? void 0 : credentialConfig.id; if (credentialId) { const credentialInfo = { credentialId, credentialType, }; if (includeWorkflowDetails) { credentialInfo.workflowId = workflow.id; credentialInfo.workflowName = workflow.name; } if (includeNodeDetails) { credentialInfo.nodeName = node.name; credentialInfo.nodeType = node.type; } if (operation === 'listWorkflowCredentials') { credentialsData.push(credentialInfo); } else if (operation === 'listByWorkflow') { credentialInfo.workflowId = workflow.id; credentialInfo.workflowName = workflow.name; credentialsData.push(credentialInfo); } uniqueCredentials.add(credentialId); } } } } } } if (operation === 'getUniqueCredentials') { // Return unique credentials only for (const credentialId of uniqueCredentials) { returnData.push({ json: { credentialId, }, pairedItem: { item: i }, }); } } else if (operation === 'listByWorkflow') { // Group credentials by workflow const workflowGroups = {}; for (const cred of credentialsData) { const workflowKey = `${cred.workflowId}-${cred.workflowName}`; if (!workflowGroups[workflowKey]) { workflowGroups[workflowKey] = { workflowId: cred.workflowId, workflowName: cred.workflowName, credentials: [], }; } workflowGroups[workflowKey].credentials.push({ credentialId: cred.credentialId, credentialType: cred.credentialType, ...(includeNodeDetails && { nodeName: cred.nodeName, nodeType: cred.nodeType, }), }); } for (const group of Object.values(workflowGroups)) { returnData.push({ json: group, pairedItem: { item: i }, }); } } else { // Return all credentials data for (const cred of credentialsData) { returnData.push({ json: cred, pairedItem: { item: i }, }); } } } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: error instanceof Error ? error.message : 'Unknown error', operation, item: i, }, pairedItem: { item: i }, }); continue; } throw error; } } return [returnData]; } } exports.XCredentialsX = XCredentialsX;