mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 21:19:43 -08:00
185 lines
4.4 KiB
TypeScript
185 lines
4.4 KiB
TypeScript
import {
|
|
IHookFunctions,
|
|
IWebhookFunctions,
|
|
} from 'n8n-core';
|
|
|
|
import {
|
|
IDataObject,
|
|
INodeTypeDescription,
|
|
INodeType,
|
|
IWebhookResonseData,
|
|
} from 'n8n-workflow';
|
|
|
|
import {
|
|
asanaApiRequest,
|
|
} from './GenericFunctions';
|
|
|
|
import { createHmac } from 'crypto';
|
|
|
|
export class AsanaTrigger implements INodeType {
|
|
description: INodeTypeDescription = {
|
|
displayName: 'Asana Trigger',
|
|
name: 'asanaTrigger',
|
|
icon: 'file:asana.png',
|
|
group: ['trigger'],
|
|
version: 1,
|
|
description: 'Starts the workflow when Asana events occure.',
|
|
defaults: {
|
|
name: 'Asana Trigger',
|
|
color: '#559922',
|
|
},
|
|
inputs: [],
|
|
outputs: ['main'],
|
|
credentials: [
|
|
{
|
|
name: 'asanaApi',
|
|
required: true,
|
|
}
|
|
],
|
|
webhooks: [
|
|
{
|
|
name: 'default',
|
|
httpMethod: 'POST',
|
|
reponseMode: 'onReceived',
|
|
path: 'webhook',
|
|
},
|
|
],
|
|
properties: [
|
|
{
|
|
displayName: 'Resource',
|
|
name: 'resource',
|
|
type: 'string',
|
|
default: '',
|
|
required: true,
|
|
description: 'The resource ID to subscribe to. The resource can be a task or project.',
|
|
},
|
|
],
|
|
|
|
};
|
|
|
|
// @ts-ignore (because of request)
|
|
webhookMethods = {
|
|
default: {
|
|
async checkExists(this: IHookFunctions): Promise<boolean> {
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
if (webhookData.webhookId === undefined) {
|
|
// No webhook id is set so no webhook can exist
|
|
return false;
|
|
}
|
|
|
|
// Webhook got created before so check if it still exists
|
|
const endpoint = `webhooks/${webhookData.webhookId}`;
|
|
|
|
try {
|
|
await asanaApiRequest.call(this, 'GET', endpoint, {});
|
|
} catch (e) {
|
|
if (e.statusCode === 404) {
|
|
// Webhook does not exist
|
|
delete webhookData.webhookId;
|
|
|
|
return false;
|
|
}
|
|
|
|
// Some error occured
|
|
throw e;
|
|
}
|
|
|
|
// If it did not error then the webhook exists
|
|
return true;
|
|
},
|
|
async create(this: IHookFunctions): Promise<boolean> {
|
|
const webhookUrl = this.getNodeWebhookUrl('default');
|
|
|
|
const resource = this.getNodeParameter('resource') as string;
|
|
|
|
const endpont = `webhooks`;
|
|
|
|
const body = {
|
|
resource,
|
|
target: webhookUrl,
|
|
};
|
|
|
|
const responseData = await asanaApiRequest.call(this, 'POST', endpont, body);
|
|
|
|
if (responseData.data === undefined || responseData.data.id === undefined) {
|
|
// Required data is missing so was not successful
|
|
return false;
|
|
}
|
|
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
webhookData.webhookId = responseData.data.id as string;
|
|
|
|
return true;
|
|
},
|
|
async delete(this: IHookFunctions): Promise<boolean> {
|
|
const webhookData = this.getWorkflowStaticData('node');
|
|
|
|
if (webhookData.webhookId !== undefined) {
|
|
const endpoint = `webhooks/${webhookData.webhookId}`;
|
|
const body = {};
|
|
|
|
try {
|
|
await asanaApiRequest.call(this, 'DELETE', endpoint, body);
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
|
|
// Remove from the static workflow data so that it is clear
|
|
// that no webhooks are registred anymore
|
|
delete webhookData.webhookId;
|
|
delete webhookData.webhookEvents;
|
|
}
|
|
|
|
return true;
|
|
},
|
|
},
|
|
};
|
|
|
|
|
|
|
|
async webhook(this: IWebhookFunctions): Promise<IWebhookResonseData> {
|
|
const bodyData = this.getBodyData() as IDataObject;
|
|
const headerData = this.getHeaderData() as IDataObject;
|
|
const req = this.getRequestObject();
|
|
|
|
const webhookData = this.getWorkflowStaticData('node') as IDataObject;
|
|
|
|
|
|
if (headerData['x-hook-secret'] !== undefined) {
|
|
// Is a create webhook confirmation request
|
|
webhookData.hookSecret = headerData['x-hook-secret'];
|
|
|
|
const res = this.getResponseObject();
|
|
res.set('X-Hook-Secret', webhookData.hookSecret as string);
|
|
res.status(200).end();
|
|
return {
|
|
noWebhookResponse: true,
|
|
};
|
|
}
|
|
|
|
// Is regular webhook call
|
|
// Check if it contains any events
|
|
if (bodyData.events === undefined || !Array.isArray(bodyData.events) ||
|
|
bodyData.events.length === 0) {
|
|
// Does not contain any event data so nothing to process so no reason to
|
|
// start the workflow
|
|
return {};
|
|
}
|
|
|
|
// Check if the request is valid
|
|
// (if the signature matches to data and hookSecret)
|
|
const computedSignature = createHmac("sha256", webhookData.hookSecret as string).update(JSON.stringify(req.body)).digest("hex");
|
|
if (headerData['x-hook-signature'] !== computedSignature) {
|
|
// Signature is not valid so ignore call
|
|
return {};
|
|
}
|
|
|
|
return {
|
|
workflowData: [
|
|
this.helpers.returnJsonArray(req.body)
|
|
],
|
|
};
|
|
}
|
|
}
|