2022-08-17 08:50:24 -07:00
|
|
|
import { IHookFunctions, IWebhookFunctions } from 'n8n-core';
|
2020-12-11 23:58:58 -08:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
import { INodeType, INodeTypeDescription, IWebhookResponseData } from 'n8n-workflow';
|
2020-12-11 23:58:58 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { pushcutApiRequest } from './GenericFunctions';
|
2020-12-11 23:58:58 -08:00
|
|
|
|
|
|
|
export class PushcutTrigger implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Pushcut Trigger',
|
|
|
|
name: 'pushcutTrigger',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
2020-12-11 23:58:58 -08:00
|
|
|
icon: 'file:pushcut.png',
|
|
|
|
group: ['trigger'],
|
|
|
|
version: 1,
|
2021-07-03 05:40:16 -07:00
|
|
|
description: 'Starts the workflow when Pushcut events occur',
|
2020-12-11 23:58:58 -08:00
|
|
|
defaults: {
|
|
|
|
name: 'Pushcut Trigger',
|
|
|
|
},
|
|
|
|
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',
|
2022-08-17 08:50:24 -07:00
|
|
|
description:
|
|
|
|
'Choose any name you would like. It will show up as a server action in the app.',
|
2020-12-11 23:58:58 -08:00
|
|
|
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) {
|
2022-08-17 08:50:24 -07:00
|
|
|
if (webhook.url === webhookUrl && webhook.actionName === actionName) {
|
2020-12-11 23:58:58 -08:00
|
|
|
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,
|
2020-12-12 08:35:35 -08:00
|
|
|
url: webhookUrl,
|
2020-12-11 23:58:58 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2021-04-16 09:33:36 -07:00
|
|
|
} catch (error) {
|
2020-12-11 23:58:58 -08:00
|
|
|
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> {
|
2022-12-02 12:54:28 -08:00
|
|
|
const body = this.getBodyData();
|
2020-12-11 23:58:58 -08:00
|
|
|
|
|
|
|
return {
|
2022-08-17 08:50:24 -07:00
|
|
|
workflowData: [this.helpers.returnJsonArray(body)],
|
2020-12-11 23:58:58 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|