UNPKG

n8n-nodes-nuwa

Version:

An n8n node for integrating with the GVA (Gin-Vue-Admin) backend system.

135 lines (134 loc) 6.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Nuwa = void 0; const n8n_workflow_1 = require("n8n-workflow"); const description_1 = require("./description"); const methods_1 = require("./methods"); class Nuwa { constructor() { this.description = { displayName: '女娲', name: 'nuwa', icon: 'fa:exchange-alt', group: ['transform'], version: [1], subtitle: '={{$parameter["operation"]}}', description: '与GVA后端系统集成的动态API调用节点', defaults: { name: '女娲', }, inputs: ['main'], outputs: ['main'], credentials: [ { name: 'gvaApi', required: true, }, ], properties: description_1.nuwaNodeDescription, }; this.methods = { loadOptions: { getApiGroups: methods_1.getApiGroups, getApis: methods_1.getApis, }, }; } async execute() { var _a; const items = this.getInputData(); const returnData = []; for (let i = 0; i < items.length; i++) { try { const resource = this.getNodeParameter('resource', i); const operation = this.getNodeParameter('operation', i); const sendBody = this.getNodeParameter('sendBody', i, false); const sendQuery = this.getNodeParameter('sendQuery', i, false); console.log('=== 节点执行开始 ==='); console.log('资源分组:', resource); console.log('选择的操作:', operation); // 解析operation值(格式:"/path|METHOD") const operationParts = operation.split('|'); if (operationParts.length !== 2) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `操作格式错误: ${operation},期望格式为 "路径|方法"`, { itemIndex: i }); } const apiPath = operationParts[0]; const apiMethod = operationParts[1]; console.log('解析的API路径:', apiPath); console.log('解析的API方法:', apiMethod); // 获取所有API信息(用于验证,可选) try { const apisResponse = await methods_1.getAllApis.call(this); if (apisResponse.code === 0 && ((_a = apisResponse.data) === null || _a === void 0 ? void 0 : _a.apis)) { const allApis = apisResponse.data.apis; const selectedApi = allApis.find(api => api.path === apiPath && api.method === apiMethod); if (selectedApi) { console.log('找到匹配的API:', selectedApi.description); } else { console.log('警告: 在API列表中未找到匹配的API,但继续执行'); } } } catch (error) { console.log('获取API列表失败,但继续执行:', error instanceof Error ? error.message : String(error)); } // 构建请求体 let body = {}; if (sendBody) { const bodyContentType = this.getNodeParameter('bodyContentType', i, 'json'); if (bodyContentType === 'form') { const bodyParameters = this.getNodeParameter('bodyParameters', i, { parameters: [] }); for (const param of bodyParameters.parameters) { if (param.name) { body[param.name] = param.value; } } } else if (bodyContentType === 'json') { const jsonBody = this.getNodeParameter('jsonBody', i, '{}'); try { body = JSON.parse(jsonBody); } catch (error) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `JSON格式错误: ${error.message}`, { itemIndex: i }); } } } // 构建查询参数 let qs = {}; if (sendQuery) { const queryParameters = this.getNodeParameter('queryParameters', i, { parameters: [] }); for (const param of queryParameters.parameters) { if (param.name) { qs[param.name] = param.value; } } } // 不需要自定义请求头,只使用凭证中的x-token const headers = {}; // 构建完整的API路径(baseUrl + /api + path) const fullApiPath = `/api${apiPath}`; console.log('完整API路径:', fullApiPath); console.log('请求方法:', apiMethod); console.log('请求体:', body); console.log('查询参数:', qs); console.log('请求头:', headers); // 执行API请求 const response = await methods_1.gvaApiRequest.call(this, apiMethod, fullApiPath, body, qs, headers); const executionData = this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(response), { itemData: { item: i } }); returnData.push(...executionData); } catch (error) { if (this.continueOnFail()) { const executionErrorData = this.helpers.constructExecutionMetaData([{ json: { error: error.message } }], { itemData: { item: i } }); returnData.push(...executionErrorData); continue; } throw error; } } return [returnData]; } } exports.Nuwa = Nuwa;