UNPKG

n8n-nodes-xcredentialsx

Version:

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

206 lines (205 loc) 9.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.XCredentialsXLocal = void 0; const n8n_workflow_1 = require("n8n-workflow"); class XCredentialsXLocal { constructor() { this.description = { displayName: 'X Credentials X (Local)', name: 'xCredentialsXLocal', icon: 'fa:key', group: ['utility'], version: 1, subtitle: '={{$parameter["operation"]}}', description: 'List all Credentials IDs used in n8n workflows (Local Access)', defaults: { name: 'X Credentials X (Local)', }, inputs: ["main" /* NodeConnectionType.Main */], outputs: ["main" /* NodeConnectionType.Main */], 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); // 使用 n8n 内部服务获取数据 // 这里我们直接访问 n8n 的内部数据,无需 API 认证 const workflowService = this.getWorkflowStaticData('global'); // 获取所有工作流(通过内部方法) const workflows = await this.helpers.httpRequest({ method: 'GET', url: 'http://localhost:5678/rest/workflows', headers: { 'accept': 'application/json', }, skipSslCertificateValidation: true, ignoreHttpStatusErrors: true, }).catch(() => { // 如果 HTTP 请求失败,尝试其他方法 throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Cannot access local n8n data. Make sure n8n is running locally.'); }); const credentialsData = []; const uniqueCredentials = new Set(); // 处理每个工作流 for (const workflow of workflows) { if (workflow.nodes) { // 解析工作流节点以查找凭据 for (const node of workflow.nodes) { if (node.credentials) { // 遍历此节点使用的所有凭据类型 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') { // 返回唯一凭据 for (const credentialId of uniqueCredentials) { returnData.push({ json: { credentialId, }, pairedItem: { item: i }, }); } } else if (operation === 'listByWorkflow') { // 按工作流分组凭据 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 { // 返回所有凭据数据 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.XCredentialsXLocal = XCredentialsXLocal;