ConvertKit Trigger and Regular Node

This commit is contained in:
Erin 2020-07-02 16:41:59 -04:00
parent 549b26fa3d
commit e209077160
10 changed files with 1155 additions and 0 deletions

View file

@ -0,0 +1,21 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class ConvertKitApi implements ICredentialType {
name = 'convertKitApi';
displayName = 'ConvertKit Api';
properties = [
{
displayName: 'API Secret',
name: 'apiSecret',
type: 'string' as NodePropertyTypes,
default: '',
typeOptions: {
password: true,
},
},
];
}

View file

@ -0,0 +1,289 @@
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeTypeDescription,
INodeType,
} from 'n8n-workflow';
import {
convertKitApiRequest,
} from './GenericFunctions';
import {
fieldOperations,
fieldFields,
} from './FieldDescription';
import {
formOperations,
formFields,
} from './FormDescription';
import {
sequenceOperations,
sequenceFields,
} from './SequenceDescription';
import {
tagOperations,
tagFields,
} from './TagDescription';
export class ConvertKit implements INodeType {
description: INodeTypeDescription = {
displayName: 'ConvertKit',
name: 'convertKit',
icon: 'file:convertKit.png',
group: ['input'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume ConvertKit API.',
defaults: {
name: 'ConvertKit',
color: '#fb6970',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'convertKitApi',
required: true,
}
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Field',
value: 'field',
},
{
name: 'Form',
value: 'form',
},
{
name: 'Sequence',
value: 'sequence',
},
{
name: 'Tag',
value: 'tag',
},
],
default: 'field',
description: 'The resource to operate on.'
},
//--------------------
// Field Description
//--------------------
...fieldOperations,
...fieldFields,
//--------------------
// FormDescription
//--------------------
...formOperations,
...formFields,
//--------------------
// Sequence Description
//--------------------
...sequenceOperations,
...sequenceFields,
//--------------------
// Tag Description
//--------------------
...tagOperations,
...tagFields,
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
let method = '';
let endpoint = '';
const qs: IDataObject = {};
let responseData;
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
const fullOperation = `${resource}/${operation}`;
for (let i = 0; i < items.length; i++) {
//--------------------
// Field Operations
//--------------------
if(resource === 'field') {
//---------
// Update
//---------
if(operation === 'update') {
qs.label = this.getNodeParameter('label', i) as string;
const id = this.getNodeParameter('id', i) as string;
method = 'PUT';
endpoint = `/custom_fields/${id}`;
//---------
// Get All
//---------
} else if(operation === 'getAll') {
method = 'GET';
endpoint = '/custom_fields';
//---------
// Create
//---------
} else if(operation === 'create') {
qs.label = this.getNodeParameter('label', i) as string;
method = 'POST';
endpoint = '/custom_fields';
//---------
// Delete
//---------
} else if(operation === 'delete') {
const id = this.getNodeParameter('id', i) as string;
method = 'DELETE';
endpoint = `/custom_fields/${id}`;
} else {
throw new Error(`The operation "${operation}" is not known!`);
}
//--------------------------------------------
// Form, Sequence, and Tag Operations
//--------------------------------------------
} else if(['form', 'sequence', 'tag'].includes(resource)) {
//-----------------
// Add Subscriber
//-----------------
if(operation === 'addSubscriber') {
qs.email= this.getNodeParameter('email', i) as string;
const id = this.getNodeParameter('id', i);
const additionalParams = this.getNodeParameter('additionalFields', 0) as IDataObject;
if(additionalParams.firstName) {
qs.first_name = additionalParams.firstName;
}
if(additionalParams.fields !== undefined) {
const fields = {} as IDataObject;
const fieldsParams = additionalParams.fields as IDataObject;
const field = fieldsParams?.field as IDataObject[];
for(let j = 0; j < field.length; j++) {
const key = field[j].key as string;
const value = field[j].value as string;
fields[key] = value;
}
qs.fields = fields;
}
if(resource === 'form') {
method = 'POST';
endpoint = `/forms/${id}/subscribe`;
} else if(resource === 'sequence') {
method = 'POST';
endpoint = `/sequences/${id}/subscribe`;
} else if(resource === 'tag') {
method = 'POST';
endpoint = `/tags/${id}/subscribe`;
}
//-----------------
// Get All
//-----------------
} else if(operation === 'getAll') {
method = 'GET';
if(resource === 'form') {
endpoint = '/forms';
} else if(resource === 'tag') {
endpoint = '/tags';
} else if(resource === 'sequence') {
endpoint = '/sequences';
}
//--------------------
// Get Subscriptions
//--------------------
} else if(operation === 'getSubscriptions') {
const id = this.getNodeParameter('id', i);
const additionalParams = this.getNodeParameter('additionalFields', 0) as IDataObject;
if(additionalParams.subscriberState) {
qs.subscriber_state = additionalParams.subscriberState;
}
method = 'GET';
if(resource === 'form') {
endpoint = `/forms/${id}/subscriptions`;
} else if(resource === 'tag') {
endpoint = `/tags/${id}/subscriptions`;
} else if(resource === 'sequence') {
endpoint = `/sequences/${id}/subscriptions`;
}
//------------
// Create Tag
//------------
} else if(operation === 'create') {
const name = this.getNodeParameter('name', i);
qs.tag = { name, };
method = 'POST';
endpoint = '/tags';
//------------
// Remove Tag
//------------
} else if(operation === 'removeSubscriber') {
const id = this.getNodeParameter('id', i);
qs.email = this.getNodeParameter('email', i);
method = 'POST';
endpoint = `/tags/${id}/unsubscribe`;
} else {
throw new Error(`The operation "${operation}" is not known!`);
}
} else {
throw new Error(`The resource "${resource}" is not known!`);
}
responseData = await convertKitApiRequest.call(this, method, endpoint, {}, qs);
if(fullOperation === 'field/getAll') {
responseData = responseData.custom_fields;
} else if(['form/addSubscriber', 'tag/addSubscriber', 'sequence/addSubscriber'].includes(fullOperation)) {
responseData = responseData.subscription;
} else if(fullOperation === 'form/getAll') {
responseData = responseData.forms;
} else if(['form/getSubscriptions', 'tag/getSubscriptions'].includes(fullOperation)) {
responseData = responseData.subscriptions;
} else if(fullOperation === 'tag/getAll') {
responseData = responseData.tags;
} else if(fullOperation === 'sequence/getAll') {
responseData = responseData.courses;
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else if (responseData !== undefined) {
returnData.push(responseData as IDataObject);
} else {
if(method === 'GET') {
returnData.push( { } );
} else {
returnData.push( { success: true } );
}
}
}
return [this.helpers.returnJsonArray(returnData)];
}
}

View file

@ -0,0 +1,163 @@
import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeTypeDescription,
INodeType,
IWebhookResponseData,
} from 'n8n-workflow';
import {
convertKitApiRequest,
} from './GenericFunctions';
export class ConvertKitTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'ConvertKit Trigger',
name: 'convertKitTrigger',
icon: 'file:convertKit.png',
subtitle: '={{$parameter["event"]}}',
group: ['trigger'],
version: 1,
description: 'Handle ConvertKit events via webhooks',
defaults: {
name: 'ConvertKit Trigger',
color: '#fb6970',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'convertKitApi',
required: true,
}
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Event',
name: 'event',
type: 'options',
required: true,
default: 'subscriberActivated',
description: 'The events that can trigger the webhook and whether they are enabled.',
options: [
{
name: 'Subscriber Activated',
value: 'subscriberActivated',
description: 'Whether the webhook is triggered when a subscriber is activated.',
},
{
name: 'Link Clicked',
value: 'linkClicked',
description: 'Whether the webhook is triggered when a link is clicked.',
},
],
},
{
displayName: 'Initiating Link',
name: 'link',
type: 'string',
required: true,
default: '',
description: 'The URL of the initiating link',
displayOptions: {
show: {
event: [
'linkClicked',
],
},
},
},
],
};
// @ts-ignore (because of request)
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
if(webhookData.webhookId) {
return true;
}
return false;
},
async create(this: IHookFunctions): Promise<boolean> {
let webhook;
const webhookUrl = this.getNodeWebhookUrl('default');
const event = this.getNodeParameter('event', 0);
const endpoint = '/automations/hooks';
const qs: IDataObject = {};
try {
qs.target_url = webhookUrl;
if(event === 'subscriberActivated') {
qs.event = {
name: 'subscriber.subscriber_activate',
};
} else if(event === 'linkClicked') {
const link = this.getNodeParameter('link', 0) as string;
qs.event = {
name: 'subscriber.link_click',
initiator_value: link,
};
}
webhook = await convertKitApiRequest.call(this, 'POST', endpoint, {}, qs);
} catch (error) {
throw error;
}
if (webhook.rule.id === undefined) {
return false;
}
const webhookData = this.getWorkflowStaticData('node');
webhookData.webhookId = webhook.rule.id as string;
webhookData.events = event;
return true;
},
async delete(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
if (webhookData.webhookId !== undefined) {
const endpoint = `/automations/hooks/${webhookData.webhookId}`;
try {
await convertKitApiRequest.call(this, 'DELETE', endpoint, {}, {});
} catch (error) {
return false;
}
delete webhookData.webhookId;
delete webhookData.events;
}
return true;
},
},
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const returnData: IDataObject[] = [];
returnData.push(this.getBodyData());
return {
workflowData: [
this.helpers.returnJsonArray(returnData),
],
};
}
}

View file

@ -0,0 +1,83 @@
import {
INodeProperties
} from 'n8n-workflow';
export const fieldOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'field',
],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create a field.',
},
{
name: 'Delete',
value: 'delete',
description: 'Delete a field.',
},
{
name: 'Get All',
value: 'getAll',
description: `List all of your account's custom fields.`,
},
{
name: 'Update',
value: 'update',
description: 'Update a field.',
},
],
default: 'update',
description: 'The operation to perform.',
},
] as INodeProperties[];
export const fieldFields = [
{
displayName: 'Field ID',
name: 'id',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'field',
],
operation: [
'update',
'delete',
],
},
},
default: '',
description: 'The ID of your custom field.',
},
{
displayName: 'Label',
name: 'label',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'field',
],
operation: [
'update',
'create',
],
},
},
default: '',
description: 'The label of the custom field.',
},
] as INodeProperties[];

View file

@ -0,0 +1,174 @@
import {
INodeProperties
} from 'n8n-workflow';
export const formOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'form',
],
},
},
options: [
{
name: 'Add Subscriber',
value: 'addSubscriber',
description: 'Add a subscriber.',
},
{
name: 'Get All',
value: 'getAll',
description: 'Get a list of all the forms for your account.',
},
{
name: 'Get Subscriptions',
value: 'getSubscriptions',
description: 'List subscriptions to a form including subscriber data.',
},
],
default: 'addSubscriber',
description: 'The operations to perform.',
},
] as INodeProperties[];
export const formFields = [
{
displayName: 'Email Address',
name: 'email',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'form',
],
operation: [
'addSubscriber',
],
},
},
default: '',
description: `The subscriber's email address.`,
},
{
displayName: 'Form ID',
name: 'id',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'form',
],
operation: [
'addSubscriber',
'getSubscriptions',
],
},
},
default: '',
description: 'Form ID.',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'form',
],
operation: [
'addSubscriber',
],
},
},
options: [
{
displayName: 'First Name',
name: 'firstName',
type: 'string',
default: '',
description: `The subscriber's first name.`,
},
{
displayName: 'Custom Fields',
name: 'fields',
placeholder: 'Add Custom Field',
description: 'Object of key/value pairs for custom fields (the custom field must exist before you can use it here).',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
},
default: {},
options: [
{
name: 'field',
displayName: 'Custom Field',
values: [
{
displayName: 'Field Key',
name: 'key',
type: 'string',
default: '',
placeholder: 'last_name',
description: `The field's key.`,
},
{
displayName: 'Field Value',
name: 'value',
type: 'string',
default: '',
placeholder: 'Doe',
description: 'Value of the field.',
},
],
},
],
},
],
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'form',
],
operation: [
'getSubscriptions',
],
},
},
options: [
{
displayName: 'Subscriber State',
name: 'subscriberState',
type: 'options',
options: [
{
name: 'Active',
value: 'active',
},
{
name: 'Cancelled',
value: 'cancelled',
},
],
default: 'active',
},
],
description: 'Receive only active subscribers or cancelled subscribers.',
},
] as INodeProperties[];

View file

@ -0,0 +1,44 @@
import {
OptionsWithUri
} from 'request';
import {
IExecuteFunctions,
ILoadOptionsFunctions,
IExecuteSingleFunctions,
} from 'n8n-core';
import {
IDataObject,
IHookFunctions
} from 'n8n-workflow';
export async function convertKitApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IHookFunctions,
method: string, endpoint: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('convertKitApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
let options: OptionsWithUri = {
headers: {
'Content-Type': 'application/json',
},
method,
qs,
body,
uri: uri ||`https://api.convertkit.com/v3${endpoint}`,
json: true,
};
options = Object.assign({}, options, option);
if (Object.keys(options.body).length === 0) {
delete options.body;
}
try {
qs.api_secret = credentials.apiSecret;
return await this.helpers.request!(options);
} catch (error) {
throw new Error(`ConvertKit error response: ${error.message}`);
}
}

View file

@ -0,0 +1,174 @@
import {
INodeProperties
} from 'n8n-workflow';
export const sequenceOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'sequence',
],
},
},
options: [
{
name: 'Add Subscriber',
value: 'addSubscriber',
description: 'Add a subscriber.',
},
{
name: 'Get All',
value: 'getAll',
description: 'Returns a list of sequences for the account.',
},
{
name: 'Get Subscriptions',
value: 'getSubscriptions',
description: 'List subscriptions to a sequence including subscriber data.',
},
],
default: 'addSubscriber',
description: 'The operations to perform.',
},
] as INodeProperties[];
export const sequenceFields = [
{
displayName: 'Email Address',
name: 'email',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'sequence',
],
operation: [
'addSubscriber',
],
},
},
default: '',
description: `The subscriber's email address.`,
},
{
displayName: 'Sequence ID',
name: 'id',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'sequence',
],
operation: [
'addSubscriber',
'getSubscriptions',
],
},
},
default: '',
description: 'Sequence ID.',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'sequence',
],
operation: [
'addSubscriber',
],
},
},
options: [
{
displayName: 'First Name',
name: 'firstName',
type: 'string',
default: '',
description: `The subscriber's first name.`,
},
{
displayName: 'Custom Fields',
name: 'fields',
placeholder: 'Add Custom Field',
description: 'Object of key/value pairs for custom fields (the custom field must exist before you can use it here).',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
},
default: {},
options: [
{
name: 'field',
displayName: 'Custom Field',
values: [
{
displayName: 'Field Key',
name: 'key',
type: 'string',
default: '',
placeholder: 'last_name',
description: `The field's key.`,
},
{
displayName: 'Field Value',
name: 'value',
type: 'string',
default: '',
placeholder: 'Doe',
description: 'Value of the field.',
},
],
},
],
},
],
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'sequence',
],
operation: [
'getSubscriptions',
],
},
},
options: [
{
displayName: 'Subscriber State',
name: 'subscriberState',
type: 'options',
options: [
{
name: 'Active',
value: 'active',
},
{
name: 'Cancelled',
value: 'cancelled',
},
],
default: 'active',
},
],
description: 'Receive only active subscribers or cancelled subscribers.',
},
] as INodeProperties[];

View file

@ -0,0 +1,204 @@
import {
INodeProperties
} from 'n8n-workflow';
export const tagOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'tag',
],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Create a tag.',
},
{
name: 'Get All',
value: 'getAll',
description: 'Returns a list of tags for the account.',
},
{
name: 'Get Subscriptions',
value: 'getSubscriptions',
description: 'List subscriptions to a tag including subscriber data.',
},
{
name: 'Remove Subscriber',
value: 'removeSubscriber',
description: 'Remove a tag from a subscriber.',
},
{
name: 'Add Subscriber',
value: 'addSubscriber',
description: 'Add a tag to a subscriber.',
},
],
default: 'create',
description: 'The operation to perform.',
},
] as INodeProperties[];
export const tagFields = [
{
displayName: 'Name',
name: 'name',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'tag',
],
operation: [
'create',
],
},
},
default: '',
description: 'Tag name.',
},
{
displayName: 'Email Address',
name: 'email',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'tag',
],
operation: [
'addSubscriber',
'removeSubscriber',
],
},
},
default: '',
description: 'Subscriber email address.',
},
{
displayName: 'Tag ID',
name: 'id',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'tag',
],
operation: [
'addSubscriber',
'removeSubscriber',
'getSubscriptions',
],
},
},
default: '',
description: 'Tag ID.',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'tag',
],
operation: [
'addSubscriber',
],
},
},
options: [
{
displayName: 'First Name',
name: 'firstName',
type: 'string',
default: '',
description: 'Subscriber first name.',
},
{
displayName: 'Custom Fields',
name: 'fields',
placeholder: 'Add Custom Field',
description: 'Object of key/value pairs for custom fields (the custom field must exist before you can use it here).',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
},
default: {},
options: [
{
name: 'field',
displayName: 'Custom Field',
values: [
{
displayName: 'Field Key',
name: 'key',
type: 'string',
default: '',
placeholder: 'last_name',
description: `The field's key.`,
},
{
displayName: 'Field Value',
name: 'value',
type: 'string',
default: '',
placeholder: 'Doe',
description: 'Value of the field.',
},
],
},
],
},
],
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'tag',
],
operation: [
'getSubscriptions',
],
},
},
options: [
{
displayName: 'Subscriber State',
name: 'subscriberState',
type: 'options',
options: [
{
name: 'Active',
value: 'active',
},
{
name: 'Cancelled',
value: 'cancelled',
},
],
default: 'active',
},
],
description: 'Receive only active subscribers or cancelled subscribers.',
},
] as INodeProperties[];

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

View file

@ -43,6 +43,7 @@
"dist/credentials/ClockifyApi.credentials.js",
"dist/credentials/CockpitApi.credentials.js",
"dist/credentials/CodaApi.credentials.js",
"dist/credentials/ConvertKitApi.credentials.js",
"dist/credentials/CopperApi.credentials.js",
"dist/credentials/CalendlyApi.credentials.js",
"dist/credentials/DisqusApi.credentials.js",
@ -173,6 +174,8 @@
"dist/nodes/Clockify/ClockifyTrigger.node.js",
"dist/nodes/Cockpit/Cockpit.node.js",
"dist/nodes/Coda/Coda.node.js",
"dist/nodes/ConvertKit/ConvertKit.node.js",
"dist/nodes/ConvertKit/ConvertKitTrigger.node.js",
"dist/nodes/Copper/CopperTrigger.node.js",
"dist/nodes/Cron.node.js",
"dist/nodes/Crypto.node.js",