mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
✨ Add Pushcut Node & Trigger (#1075)
* ✨ Pushcut Node & Trigger * ⚡ Small improvements Co-authored-by: Jan <janober@users.noreply.github.com>
This commit is contained in:
130
packages/nodes-base/nodes/Pushcut/PushcutTrigger.node.ts
Normal file
130
packages/nodes-base/nodes/Pushcut/PushcutTrigger.node.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import {
|
||||
IHookFunctions,
|
||||
IWebhookFunctions,
|
||||
} from 'n8n-core';
|
||||
|
||||
import {
|
||||
IDataObject,
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
IWebhookResponseData,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import {
|
||||
pushcutApiRequest,
|
||||
} from './GenericFunctions';
|
||||
|
||||
export class PushcutTrigger implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Pushcut Trigger',
|
||||
name: 'pushcutTrigger',
|
||||
icon: 'file:pushcut.png',
|
||||
group: ['trigger'],
|
||||
version: 1,
|
||||
description: 'Starts the workflow when a Github events occurs.',
|
||||
defaults: {
|
||||
name: 'Pushcut Trigger',
|
||||
color: '#1f2957',
|
||||
},
|
||||
inputs: [],
|
||||
outputs: ['main'],
|
||||
credentials: [
|
||||
{
|
||||
name: 'pushcutApi',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
webhooks: [
|
||||
{
|
||||
name: 'default',
|
||||
httpMethod: 'POST',
|
||||
responseMode: 'onReceived',
|
||||
path: 'webhook',
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Action Name',
|
||||
name: 'actionName',
|
||||
type: 'string',
|
||||
description: 'Choose any name you would like. It will show up as a server action in the app',
|
||||
default: '',
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
// @ts-ignore (because of request)
|
||||
webhookMethods = {
|
||||
default: {
|
||||
async checkExists(this: IHookFunctions): Promise<boolean> {
|
||||
const webhookUrl = this.getNodeWebhookUrl('default');
|
||||
const webhookData = this.getWorkflowStaticData('node');
|
||||
const actionName = this.getNodeParameter('actionName');
|
||||
// Check all the webhooks which exist already if it is identical to the
|
||||
// one that is supposed to get created.
|
||||
const endpoint = '/subscriptions';
|
||||
const webhooks = await pushcutApiRequest.call(this, 'GET', endpoint, {});
|
||||
|
||||
for (const webhook of webhooks) {
|
||||
if (webhook.url === webhookUrl &&
|
||||
webhook.actionName === actionName) {
|
||||
webhookData.webhookId = webhook.id;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
async create(this: IHookFunctions): Promise<boolean> {
|
||||
const webhookData = this.getWorkflowStaticData('node');
|
||||
const webhookUrl = this.getNodeWebhookUrl('default');
|
||||
const actionName = this.getNodeParameter('actionName');
|
||||
|
||||
const endpoint = '/subscriptions';
|
||||
|
||||
const body = {
|
||||
actionName,
|
||||
url: webhookUrl
|
||||
};
|
||||
|
||||
const responseData = await pushcutApiRequest.call(this, 'POST', endpoint, body);
|
||||
|
||||
if (responseData.id === undefined) {
|
||||
// Required data is missing so was not successful
|
||||
return false;
|
||||
}
|
||||
|
||||
webhookData.webhookId = responseData.id as string;
|
||||
return true;
|
||||
},
|
||||
async delete(this: IHookFunctions): Promise<boolean> {
|
||||
const webhookData = this.getWorkflowStaticData('node');
|
||||
if (webhookData.webhookId !== undefined) {
|
||||
|
||||
const endpoint = `/subscriptions/${webhookData.webhookId}`;
|
||||
|
||||
try {
|
||||
await pushcutApiRequest.call(this, 'DELETE', endpoint);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Remove from the static workflow data so that it is clear
|
||||
// that no webhooks are registred anymore
|
||||
delete webhookData.webhookId;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
|
||||
const body = this.getBodyData() as IDataObject;
|
||||
|
||||
return {
|
||||
workflowData: [
|
||||
this.helpers.returnJsonArray(body)
|
||||
],
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user