n8n-nodes-nuwa
Version:
An n8n node for integrating with the GVA (Gin-Vue-Admin) backend system.
111 lines (110 loc) • 4.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getApiGroups = getApiGroups;
exports.getApis = getApis;
const GenericFunctions_1 = require("./GenericFunctions");
/**
* 获取API分组列表
*/
async function getApiGroups() {
var _a;
try {
console.log('=== getApiGroups函数被调用 ===');
console.log('时间戳:', new Date().toISOString());
// 尝试从API获取
try {
const response = await GenericFunctions_1.getAllApis.call(this);
console.log('API分组响应:', response);
if (response.code !== 0 || !((_a = response.data) === null || _a === void 0 ? void 0 : _a.apis)) {
console.log('API分组获取失败,返回空数组');
return [];
}
const allApis = response.data.apis;
console.log(`总共获取到 ${allApis.length} 个API`);
const apiGroups = new Set();
for (const api of allApis) {
if (api.apiGroup) {
apiGroups.add(api.apiGroup);
}
}
const groups = Array.from(apiGroups).map(group => ({
name: group,
value: group
}));
console.log('提取到的API分组:', Array.from(apiGroups));
// 如果API返回的分组为空,返回空数组
if (groups.length === 0) {
console.log('API返回的分组为空,返回空数组');
return [];
}
console.log('成功获取API分组:', groups);
return groups;
}
catch (apiError) {
console.error('API调用失败:', apiError instanceof Error ? apiError.message : String(apiError));
console.log('返回空数组');
return [];
}
}
catch (error) {
console.error('获取API分组出错:', error instanceof Error ? error.message : String(error));
// 出错时返回默认选项
return [
{ name: '标签管理', value: '标签管理' },
{ name: '系统管理', value: '系统管理' },
{ name: '用户管理', value: '用户管理' },
];
}
}
/**
* 根据选择的分组获取具体的API列表
*/
async function getApis() {
var _a;
try {
console.log('=== getApis函数被调用 ===');
console.log('时间戳:', new Date().toISOString());
const apiGroup = this.getNodeParameter('resource', 0);
console.log('选择的API分组:', apiGroup);
console.log('API分组类型:', typeof apiGroup);
if (!apiGroup) {
console.log('未选择API分组,返回空数组');
return [];
}
// 尝试从API获取
let response;
try {
response = await GenericFunctions_1.getAllApis.call(this);
console.log('API列表响应:', response);
}
catch (apiError) {
console.error('API调用失败:', apiError instanceof Error ? apiError.message : String(apiError));
console.log('返回空数组');
return [];
}
if (response.code !== 0 || !((_a = response.data) === null || _a === void 0 ? void 0 : _a.apis)) {
console.log('API列表获取失败,返回空数组');
return [];
}
const allApis = response.data.apis;
// 根据选择的分组过滤API
const filteredApis = allApis.filter(api => api.apiGroup === apiGroup);
// 将API转换为选项格式,值包含路径和方法
const options = filteredApis.map(api => ({
name: `${api.description || api.path} (${api.method})`,
value: `${api.path}|${api.method}`,
description: `${api.method} ${api.path}`,
}));
console.log('=== 返回API选项 ===');
console.log('选项数量:', options.length);
console.log('选项内容:', JSON.stringify(options, null, 2));
return options;
}
catch (error) {
console.error('获取API列表出错:', error instanceof Error ? error.message : String(error));
console.error('错误堆栈:', error instanceof Error ? error.stack : undefined);
// 出错时返回空数组
console.log('返回空数组');
return [];
}
}