n8n/packages/nodes-base/nodes/Pushcut/PushcutTrigger.node.ts
Michael Kret 61e26804ba
refactor(core): Remove linting exceptions in nodes-base (#4794)
*  enabled array-type

*  await-thenable on

*  ban-types on

*  default-param-last on

*  dot-notation on

*  member-delimiter-style on

*  no-duplicate-imports on

*  no-empty-interface on

*  no-floating-promises on

*  no-for-in-array on

*  no-invalid-void-type on

*  no-loop-func on

*  no-shadow on

*  ban-ts-comment re enabled

*  @typescript-eslint/lines-between-class-members on

* address my own comment

* @typescript-eslint/return-await on

* @typescript-eslint/promise-function-async on

* @typescript-eslint/no-unnecessary-boolean-literal-compare on

* @typescript-eslint/no-unnecessary-type-assertion on

* prefer-const on

* @typescript-eslint/prefer-optional-chain on

Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
2022-12-02 21:54:28 +01:00

118 lines
3.1 KiB
TypeScript

import { IHookFunctions, IWebhookFunctions } from 'n8n-core';
import { INodeType, INodeTypeDescription, IWebhookResponseData } from 'n8n-workflow';
import { pushcutApiRequest } from './GenericFunctions';
export class PushcutTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Pushcut Trigger',
name: 'pushcutTrigger',
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
icon: 'file:pushcut.png',
group: ['trigger'],
version: 1,
description: 'Starts the workflow when Pushcut events occur',
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',
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 (error) {
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();
return {
workflowData: [this.helpers.returnJsonArray(body)],
};
}
}