UNPKG

n8n-nodes-base

Version:

Base nodes of n8n

197 lines 7.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NetlifyTrigger = void 0; const change_case_1 = require("change-case"); const n8n_workflow_1 = require("n8n-workflow"); const GenericFunctions_1 = require("./GenericFunctions"); class NetlifyTrigger { description = { displayName: 'Netlify Trigger', name: 'netlifyTrigger', icon: 'file:netlify.svg', group: ['trigger'], version: 1, subtitle: '={{$parameter["event"]}}', description: 'Handle netlify events via webhooks', defaults: { name: 'Netlify Trigger', }, inputs: [], outputs: [n8n_workflow_1.NodeConnectionTypes.Main], credentials: [ { name: 'netlifyApi', required: true, }, ], webhooks: [ { name: 'default', httpMethod: 'POST', responseMode: 'onReceived', path: 'webhook', }, ], properties: [ { displayName: 'Site Name or ID', name: 'siteId', required: true, type: 'options', default: '', typeOptions: { loadOptionsMethod: 'getSites', }, description: 'Select the Site ID. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>.', }, { displayName: 'Event', name: 'event', type: 'options', required: true, default: '', options: [ { name: 'Deploy Building', value: 'deployBuilding', }, { name: 'Deploy Failed', value: 'deployFailed', }, { name: 'Deploy Created', value: 'deployCreated', }, { name: 'Form Submitted', value: 'submissionCreated', }, ], }, { displayName: 'Form Name or ID', name: 'formId', type: 'options', required: true, displayOptions: { show: { event: ['submissionCreated'], }, }, default: '', typeOptions: { loadOptionsMethod: 'getForms', }, description: 'Select a form. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>.', }, { displayName: 'Simplify', name: 'simple', type: 'boolean', displayOptions: { show: { event: ['submissionCreated'], }, }, default: true, description: 'Whether to return a simplified version of the response instead of the raw data', }, ], }; webhookMethods = { default: { async checkExists() { const qs = {}; const webhookData = this.getWorkflowStaticData('node'); const webhookUrl = this.getNodeWebhookUrl('default'); const event = this.getNodeParameter('event'); qs.site_id = this.getNodeParameter('siteId'); const webhooks = await GenericFunctions_1.netlifyApiRequest.call(this, 'GET', '/hooks', {}, qs); for (const webhook of webhooks) { if (webhook.type === 'url') { if (webhook.data.url === webhookUrl && webhook.event === (0, change_case_1.snakeCase)(event)) { webhookData.webhookId = webhook.id; return true; } } } return false; }, async create() { //TODO - implement missing events // alL posible events can be found doing a GET /hooks/types const webhookUrl = this.getNodeWebhookUrl('default'); const webhookData = this.getWorkflowStaticData('node'); const event = this.getNodeParameter('event'); const body = { event: (0, change_case_1.snakeCase)(event), data: { url: webhookUrl, }, site_id: this.getNodeParameter('siteId'), }; const formId = this.getNodeParameter('formId', '*'); if (event === 'submissionCreated' && formId !== '*') { body.form_id = this.getNodeParameter('formId'); } const webhook = await GenericFunctions_1.netlifyApiRequest.call(this, 'POST', '/hooks', body); webhookData.webhookId = webhook.id; return true; }, async delete() { const webhookData = this.getWorkflowStaticData('node'); try { await GenericFunctions_1.netlifyApiRequest.call(this, 'DELETE', `/hooks/${webhookData.webhookId}`); } catch (error) { return false; } delete webhookData.webhookId; return true; }, }, }; methods = { loadOptions: { async getSites() { const returnData = []; const sites = await GenericFunctions_1.netlifyApiRequest.call(this, 'GET', '/sites'); for (const site of sites) { returnData.push({ name: site.name, value: site.site_id, }); } return returnData; }, async getForms() { const returnData = []; const siteId = this.getNodeParameter('siteId'); const forms = await GenericFunctions_1.netlifyApiRequest.call(this, 'GET', `/sites/${siteId}/forms`); for (const form of forms) { returnData.push({ name: form.name, value: form.id, }); } returnData.unshift({ name: '[All Forms]', value: '*' }); return returnData; }, }, }; async webhook() { const req = this.getRequestObject(); const simple = this.getNodeParameter('simple', false); const event = this.getNodeParameter('event'); let response = req.body; if (simple && event === 'submissionCreated') { response = response.data; } return { workflowData: [this.helpers.returnJsonArray(response)], }; } } exports.NetlifyTrigger = NetlifyTrigger; //# sourceMappingURL=NetlifyTrigger.node.js.map