UNPKG

@kaia-team/n8n-nodes-kaia

Version:
230 lines 7.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.systemWebhookApiProperties = void 0; exports.handleSystemWebhook = handleSystemWebhook; const n8n_workflow_1 = require("n8n-workflow"); const utils_1 = require("../utils"); /** * System Webhook API Properties * Properties specific to the systemWebhook API resource */ exports.systemWebhookApiProperties = [ { displayName: 'Webhook Operation', name: 'webhookOperation', type: 'options', noDataExpression: true, displayOptions: { show: { resource: ['systemWebhook'], }, }, options: [ { name: 'Create', value: 'create', description: 'Create a new system webhook', }, { name: 'Delete', value: 'delete', description: 'Delete a system webhook', }, { name: 'List', value: 'list', description: 'List all system webhooks', }, { name: 'Read', value: 'read', description: 'Read a system webhook', }, { name: 'Update', value: 'update', description: 'Update a system webhook', }, ], default: 'list', }, { displayName: 'Webhook Name', name: 'webhookName', type: 'string', default: '', displayOptions: { show: { resource: ['systemWebhook'], webhookOperation: ['create', 'update'], }, }, description: 'Name for the system webhook', required: true, }, { displayName: 'Webhook URL', name: 'webhookUrl', type: 'string', default: '', displayOptions: { show: { resource: ['systemWebhook'], webhookOperation: ['create', 'update'], }, }, description: 'URL where the webhook should send events', required: true, }, { displayName: 'N8N Webhook Base URL', name: 'n8nWebhookBaseUrl', type: 'string', default: '', displayOptions: { show: { resource: ['systemWebhook'], webhookOperation: ['create'], }, }, placeholder: 'https://your-n8n-instance.com/webhook', description: 'Base URL for n8n webhooks (used to generate webhook URLs)', }, { displayName: 'Models', name: 'models', type: 'multiOptions', displayOptions: { show: { resource: ['systemWebhook'], webhookOperation: ['create', 'update'], }, }, options: [ { name: 'Answer Rating', value: 'AnswerRating', }, { name: 'Assistant Rating', value: 'AssistantRating', }, { name: 'Chat History', value: 'ChatHistory', }, { name: 'Daily Statistic', value: 'DailyStatistic', }, { name: 'Usage Entry', value: 'UsageEntry', }, { name: 'Usage Summary', value: 'UsageSummary', }, { name: 'User Account', value: 'UserAccount', }, { name: 'User Email', value: 'UserEmail', }, { name: 'User File', value: 'UserFile', }, ], default: [], description: 'Models to listen for events on', required: true, }, { displayName: 'Webhook ID', name: 'webhookId', type: 'string', default: '', displayOptions: { show: { resource: ['systemWebhook'], webhookOperation: ['read', 'delete', 'update'], }, }, description: 'The ID of the system webhook', required: true, }, ]; /** * System Webhook API Handler * Handles requests to the systemWebhook API */ async function handleSystemWebhook(executeFunctions, itemIndex, adminUrl, adminToken) { const operation = executeFunctions.getNodeParameter('webhookOperation', itemIndex); const additionalFields = executeFunctions.getNodeParameter('additionalFields', itemIndex, {}); const headers = (0, utils_1.createHeaders)(adminToken, additionalFields); if (operation === 'create' || operation === 'update') { const name = executeFunctions.getNodeParameter('webhookName', itemIndex); const models = executeFunctions.getNodeParameter('models', itemIndex); let webhookId = ''; if (operation === 'update') { webhookId = executeFunctions.getNodeParameter('webhookId', itemIndex); } let url = executeFunctions.getNodeParameter('webhookUrl', itemIndex); // For create operations, if n8nWebhookBaseUrl is provided, use it to generate the webhook URL if (operation === 'create') { const n8nWebhookBaseUrl = executeFunctions.getNodeParameter('n8nWebhookBaseUrl', itemIndex, ''); if (n8nWebhookBaseUrl && !url) { // Generate webhook URL using the base URL and node ID const nodeId = executeFunctions.getNode().id; url = `${n8nWebhookBaseUrl}/${nodeId}`; } } const payload = { system_webhook: { name, url, models, }, }; const requestUrl = operation === 'create' ? `${adminUrl}/system_webhooks.json` : `${adminUrl}/system_webhooks/${webhookId}.json`; const method = operation === 'create' ? 'POST' : 'PUT'; const response = await executeFunctions.helpers.httpRequest({ method, url: requestUrl, body: payload, headers, }); return response; } else if (operation === 'list') { const response = await executeFunctions.helpers.httpRequest({ method: 'GET', url: `${adminUrl}/system_webhooks.json`, headers, }); return executeFunctions.helpers.returnJsonArray(response); } else if (operation === 'read') { const webhookId = executeFunctions.getNodeParameter('webhookId', itemIndex); const response = await executeFunctions.helpers.httpRequest({ method: 'GET', url: `${adminUrl}/system_webhooks/${webhookId}.json`, headers, }); return response; } else if (operation === 'delete') { const webhookId = executeFunctions.getNodeParameter('webhookId', itemIndex); const response = await executeFunctions.helpers.httpRequest({ method: 'DELETE', url: `${adminUrl}/system_webhooks/${webhookId}.json`, headers, }); return { success: true, deletedId: webhookId }; } throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `Unknown webhook operation: ${operation}`); } //# sourceMappingURL=systemWebhookApi.js.map