n8n-nodes-nuwa
Version:
An n8n node for integrating with the GVA (Gin-Vue-Admin) backend system.
70 lines (69 loc) • 2.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.gvaApiRequest = gvaApiRequest;
exports.getAllApis = getAllApis;
const n8n_workflow_1 = require("n8n-workflow");
/**
* 执行GVA API请求的通用函数
*/
async function gvaApiRequest(method, endpoint, body = {}, qs = {}, headers = {}) {
// 获取凭证中的baseUrl
const credentials = await this.getCredentials('gvaApi');
const baseUrl = credentials.baseUrl;
// 确保baseUrl格式正确
const formattedBaseUrl = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;
const options = {
method,
url: `${formattedBaseUrl}${endpoint}`,
body,
qs,
headers,
json: true,
};
console.log('构建的完整URL:', options.url);
try {
// 添加调试信息
console.log('GVA API请求:', {
endpoint,
method,
url: options.url,
headers: options.headers,
baseUrl: formattedBaseUrl,
});
const responseData = await this.helpers.httpRequestWithAuthentication.call(this, 'gvaApi', options);
// 添加响应调试信息
console.log('GVA API响应:', {
status: 'success',
dataType: typeof responseData,
hasData: !!(responseData === null || responseData === void 0 ? void 0 : responseData.data),
code: responseData === null || responseData === void 0 ? void 0 : responseData.code,
});
return responseData;
}
catch (error) {
// 添加错误调试信息
console.error('GVA API错误:', {
endpoint,
method,
url: options.url,
baseUrl: formattedBaseUrl,
error: error instanceof Error ? error.message : String(error),
stack: error instanceof Error ? error.stack : undefined,
});
if (error && typeof error === 'object' && 'response' in error) {
const apiError = error;
if (apiError.response && apiError.response.data) {
const description = apiError.response.data.msg || 'API返回错误';
throw new n8n_workflow_1.NodeApiError(this.getNode(), apiError, { message: description });
}
}
const errorMessage = error instanceof Error ? error.message : '未知错误';
throw new n8n_workflow_1.NodeOperationError(this.getNode(), `GVA请求失败: ${errorMessage}`);
}
}
/**
* 获取所有API列表
*/
async function getAllApis() {
return await gvaApiRequest.call(this, 'POST', '/api/api/getAllApis');
}