n8n/packages/nodes-base/nodes/Figma/FigmaTrigger.node.ts
Iván Ovejero 0448feec56
refactor: Apply eslint-plugin-n8n-nodes-base autofixable rules (#3174)
*  Initial setup

* 👕 Update `.eslintignore`

* 👕 Autofix node-param-default-missing (#3173)

* 🔥 Remove duplicate key

* 👕 Add exceptions

* 📦 Update package-lock.json

* 👕 Apply `node-class-description-inputs-wrong-trigger-node` (#3176)

* 👕 Apply `node-class-description-inputs-wrong-regular-node` (#3177)

* 👕 Apply `node-class-description-outputs-wrong` (#3178)

* 👕 Apply `node-execute-block-double-assertion-for-items` (#3179)

* 👕 Apply `node-param-default-wrong-for-collection` (#3180)

* 👕 Apply node-param-default-wrong-for-boolean (#3181)

* Autofixed default missing

* Autofixed booleans, worked well

*  Fix params

*  Undo exempted autofixes

* 📦 Update package-lock.json

* 👕 Apply node-class-description-missing-subtitle (#3182)

*  Fix missing comma

* 👕 Apply `node-param-default-wrong-for-fixed-collection` (#3184)

* 👕 Add exception for `node-class-description-missing-subtitle`

* 👕 Apply `node-param-default-wrong-for-multi-options` (#3185)

* 👕 Apply `node-param-collection-type-unsorted-items` (#3186)

* Missing coma

* 👕 Apply `node-param-default-wrong-for-simplify` (#3187)

* 👕 Apply `node-param-description-comma-separated-hyphen` (#3190)

* 👕 Apply `node-param-description-empty-string` (#3189)

* 👕 Apply `node-param-description-excess-inner-whitespace` (#3191)

* Rule looks good

* Add whitespace rule in eslint config

* :zao: fix

* 👕 Apply `node-param-description-identical-to-display-name` (#3193)

* 👕 Apply `node-param-description-missing-for-ignore-ssl-issues` (#3195)

*  Revert ":zao: fix"

This reverts commit ef8a76f3df.

* 👕 Apply `node-param-description-missing-for-simplify`  (#3196)

* 👕 Apply `node-param-description-missing-final-period` (#3194)

* Rule working as intended

* Add rule to eslint

* 👕 Apply node-param-description-missing-for-return-all (#3197)

*  Restore `lintfix` command

Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com>
Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: agobrech <ael.gobrecht@gmail.com>
Co-authored-by: Michael Kret <michael.k@radency.com>
2022-04-22 18:29:51 +02:00

183 lines
4.9 KiB
TypeScript

import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeType,
INodeTypeDescription,
IWebhookResponseData,
} from 'n8n-workflow';
import {
figmaApiRequest,
} from './GenericFunctions';
import {
snakeCase,
} from 'change-case';
import {
randomBytes,
} from 'crypto';
export class FigmaTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Figma Trigger (Beta)',
name: 'figmaTrigger',
icon: 'file:figma.svg',
group: ['trigger'],
version: 1,
subtitle: '={{$parameter["triggerOn"]}}',
description: 'Starts the workflow when Figma events occur',
defaults: {
name: 'Figma Trigger (Beta)',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'figmaApi',
required: true,
},
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Team ID',
name: 'teamId',
type: 'string',
required: true,
default: '',
description: 'Trigger will monitor this Figma Team for changes. Team ID can be found in the URL of a Figma Team page when viewed in a web browser: figma.com/files/team/{TEAM-ID}/.',
},
{
displayName: 'Trigger On',
name: 'triggerOn',
type: 'options',
options: [
{
name: 'File Commented',
value: 'fileComment',
description: 'Triggers when someone comments on a file',
},
{
name: 'File Deleted',
value: 'fileDelete',
description: 'Triggers whenever a file has been deleted. Does not trigger on all files within a folder, if the folder is deleted.',
},
{
name: 'File Updated',
value: 'fileUpdate',
description: 'Triggers whenever a file saves or is deleted. This occurs whenever a file is closed or within 30 seconds after changes have been made.',
},
{
name: 'File Version Updated',
value: 'fileVersionUpdate',
description: 'Triggers whenever a named version is created in the version history of a file',
},
{
name: 'Library Publish',
value: 'libraryPublish',
description: 'Triggers whenever a library file is published',
},
],
default: '',
required: true,
},
],
};
// @ts-ignore (because of request)
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
const webhookUrl = this.getNodeWebhookUrl('default');
const webhookData = this.getWorkflowStaticData('node');
const teamId = this.getNodeParameter('teamId') as string;
const triggerOn = this.getNodeParameter('triggerOn') as string;
// Check all the webhooks which exist already if it is identical to the
// one that is supposed to get created.
const { webhooks } = await figmaApiRequest.call(this, 'GET', `/v2/teams/${teamId}/webhooks`);
for (const webhook of webhooks) {
if (webhook.endpoint === webhookUrl
&& webhook.team_id === teamId
&& webhook.event_type === snakeCase(triggerOn).toUpperCase()
&& webhook.status === 'ACTIVE') {
webhookData.webhookId = webhook.id as string;
return true;
}
}
return false;
},
async create(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
const webhookUrl = this.getNodeWebhookUrl('default');
const triggerOn = this.getNodeParameter('triggerOn') as string;
const teamId = this.getNodeParameter('teamId') as string;
const endpoint = '/v2/webhooks';
const body: IDataObject = {
event_type: snakeCase(triggerOn).toUpperCase(),
team_id: teamId,
description: `n8n-webhook:${webhookUrl}`,
endpoint: webhookUrl,
passcode: randomBytes(10).toString('hex') as string,
};
const responseData = await figmaApiRequest.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 = `/v2/webhooks/${webhookData.webhookId}`;
try {
await figmaApiRequest.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 bodyData = this.getBodyData();
if (bodyData.event_type === 'PING') {
const res = this.getResponseObject();
res.status(200).end();
return {
noWebhookResponse: true,
};
}
return {
workflowData: [
this.helpers.returnJsonArray(bodyData),
],
};
}
}