n8n/packages/nodes-base/nodes/Keap/KeapTrigger.node.ts

197 lines
4.6 KiB
TypeScript
Raw Normal View History

2020-04-01 15:10:41 -07:00
import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeTypeDescription,
INodeType,
IWebhookResponseData,
ILoadOptionsFunctions,
INodePropertyOptions,
} from 'n8n-workflow';
import {
2020-04-02 16:37:40 -07:00
keapApiRequest,
2020-04-01 15:10:41 -07:00
} from './GenericFunctions';
import {
2020-04-14 12:07:53 -07:00
capitalCase,
2020-04-01 15:10:41 -07:00
} from 'change-case';
2020-04-02 16:37:40 -07:00
export class KeapTrigger implements INodeType {
2020-04-01 15:10:41 -07:00
description: INodeTypeDescription = {
2020-04-02 16:37:40 -07:00
displayName: 'Keap Trigger',
name: 'keapTrigger',
icon: 'file:keap.png',
2020-04-01 15:10:41 -07:00
group: ['trigger'],
version: 1,
subtitle: '={{$parameter["eventId"]}}',
description: 'Starts the workflow when Infusionsoft events occure.',
defaults: {
2020-04-02 16:37:40 -07:00
name: 'Keap Trigger',
2020-04-01 15:10:41 -07:00
color: '#79af53',
},
inputs: [],
outputs: ['main'],
credentials: [
{
2020-04-02 16:37:40 -07:00
name: 'keapOAuth2Api',
2020-04-01 15:10:41 -07:00
required: true,
},
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Event',
name: 'eventId',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getEvents',
},
default: '',
required: true,
},
{
displayName: 'RAW Data',
name: 'rawData',
type: 'boolean',
default: false,
description: `Returns the data exactly in the way it got received from the API.`,
},
],
};
methods = {
loadOptions: {
// Get all the event types to display them to user so that he can
// select them easily
async getEvents(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
2020-04-02 16:37:40 -07:00
const hooks = await keapApiRequest.call(this, 'GET', '/hooks/event_keys');
2020-04-01 15:10:41 -07:00
for (const hook of hooks) {
const hookName = hook;
const hookId = hook;
returnData.push({
2020-04-14 12:07:53 -07:00
name: capitalCase((hookName as string).replace('.', ' ')),
2020-04-01 15:10:41 -07:00
value: hookId as string,
});
}
return returnData;
},
},
};
// @ts-ignore (because of request)
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
const eventId = this.getNodeParameter('eventId') as string;
const webhookUrl = this.getNodeWebhookUrl('default');
const webhookData = this.getWorkflowStaticData('node');
2020-04-02 16:37:40 -07:00
const responseData = await keapApiRequest.call(this, 'GET', '/hooks', {});
2020-04-01 15:10:41 -07:00
for (const existingData of responseData) {
if (existingData.hookUrl === webhookUrl
&& existingData.eventKey === eventId
&& existingData.status === 'Verified') {
// The webhook exists already
webhookData.webhookId = existingData.key;
return true;
}
}
return false;
},
async create(this: IHookFunctions): Promise<boolean> {
const eventId = this.getNodeParameter('eventId') as string;
const webhookData = this.getWorkflowStaticData('node');
const webhookUrl = this.getNodeWebhookUrl('default');
const body = {
eventKey: eventId,
hookUrl: webhookUrl,
};
2020-04-02 16:37:40 -07:00
const responseData = await keapApiRequest.call(this, 'POST', '/hooks', body);
2020-04-01 15:10:41 -07:00
if (responseData.key === undefined) {
// Required data is missing so was not successful
return false;
}
webhookData.webhookId = responseData.key as string;
return true;
},
async delete(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
if (webhookData.webhookId !== undefined) {
try {
2020-04-02 16:37:40 -07:00
await keapApiRequest.call(this, 'DELETE', `/hooks/${webhookData.webhookId}`);
2020-04-01 15:10:41 -07:00
} catch (e) {
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 rawData = this.getNodeParameter('rawData') as boolean;
const headers = this.getHeaderData() as IDataObject;
const bodyData = this.getBodyData() as IDataObject;
if (headers['x-hook-secret']) {
// Is a create webhook confirmation request
const res = this.getResponseObject();
res.set('x-hook-secret', headers['x-hook-secret'] as string);
res.status(200).end();
return {
noWebhookResponse: true,
};
}
if (rawData) {
return {
workflowData: [
this.helpers.returnJsonArray(bodyData),
],
};
}
const responseData: IDataObject[] = [];
for (const data of bodyData.object_keys as IDataObject[]) {
responseData.push({
eventKey: bodyData.event_key,
objectType: bodyData.object_type,
id: data.id,
timestamp: data.timestamp,
apiUrl: data.apiUrl,
});
}
return {
workflowData: [
this.helpers.returnJsonArray(responseData),
],
};
}
}