n8n/packages/nodes-base/nodes/Asana/AsanaTrigger.node.ts

210 lines
5.3 KiB
TypeScript
Raw Normal View History

2019-06-23 03:35:23 -07:00
import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeTypeDescription,
INodeType,
IWebhookResponseData,
2019-06-23 03:35:23 -07:00
} 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',
2019-08-28 08:16:09 -07:00
responseMode: 'onReceived',
2019-06-23 03:35:23 -07:00
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.',
},
2019-11-27 17:20:15 -08:00
{
displayName: 'Workspace',
name: 'workspace',
type: 'string',
default: '',
required: false,
description: 'The workspace ID the resource is registered under. This is only required if you want to allow overriding existing webhooks.',
},
2019-06-23 03:35:23 -07:00
],
};
// @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;
2019-11-27 17:20:15 -08:00
const workspace = this.getNodeParameter('workspace') as string;
2019-07-09 11:31:44 -07:00
const endpoint = `webhooks`;
2019-06-23 03:35:23 -07:00
const body = {
resource,
target: webhookUrl,
};
2019-11-30 09:05:34 -08:00
let responseData;
2019-11-27 17:20:15 -08:00
try {
responseData = await asanaApiRequest.call(this, 'POST', endpoint, body);
} catch(error) {
// delete webhook if it already exists
if (error.statusCode === 403) {
const webhookData = await asanaApiRequest.call(this, 'GET', endpoint, {}, { workspace });
2019-11-30 09:05:34 -08:00
const webhook = webhookData.data.find((webhook: any) => { // tslint:disable-line:no-any
return webhook.target === webhookUrl && webhook.resource.gid === resource;
2019-11-27 17:20:15 -08:00
});
await asanaApiRequest.call(this, 'DELETE', `${endpoint}/${webhook.gid}`, {});
responseData = await asanaApiRequest.call(this, 'POST', endpoint, body);
} else {
2019-11-30 09:05:34 -08:00
throw error;
2019-11-27 17:20:15 -08:00
}
}
2019-06-23 03:35:23 -07:00
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<IWebhookResponseData> {
2019-06-23 03:35:23 -07:00
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)
],
};
}
}