Create node template

This commit is contained in:
Iván Ovejero 2021-03-24 08:54:13 +01:00
parent 00e7edea85
commit 646d076520
7 changed files with 509 additions and 0 deletions

View file

@ -0,0 +1,18 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class HighLevelApi implements ICredentialType {
name = 'highLevelApi';
displayName = 'HighLevel API';
documentationUrl = 'highLevel';
properties = [
{
displayName: 'API Key',
name: 'apiKey',
type: 'string' as NodePropertyTypes,
default: '',
},
];
}

View file

@ -0,0 +1,49 @@
import {
IExecuteFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import {
IHookFunctions,
} from 'n8n-workflow';
/**
* Make an authenticated REST API request to HighLevel.
*/
export async function highLevelApiRequest(
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions,
method: string,
endpoint: string,
body: object = {},
qs: object = {},
) {
const { apiKey } = this.getCredentials('highLevelApi') as { apiKey: string };
const options = {
headers: {
Authorization: apiKey,
},
method,
body,
qs,
uri: `https://rest.gohighlevel.com/v1${endpoint}`,
json: true,
};
try {
return await this.helpers.request!.call(this, options);
} catch (error) {
if (error?.response?.body?.error) {
const { error: errorMessage } = error.response.body;
throw new Error(
`HighLevel error response [${error.statusCode}]: ${errorMessage}`,
);
}
throw error;
}
}

View file

@ -0,0 +1,156 @@
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
highLevelApiRequest,
} from './GenericFunctions';
import {
contactFields,
contactOperations,
} from './descriptions';
export class HighLevel implements INodeType {
description: INodeTypeDescription = {
displayName: 'HighLevel',
name: 'highLevel',
icon: 'file:highLevel.png',
group: ['transform'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume the HighLevel API',
defaults: {
name: 'HighLevel',
color: '#16416c',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'highLevelApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Contact',
value: 'contact',
},
],
default: 'contact',
description: 'Resource to consume',
},
...contactOperations,
...contactFields,
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
let responseData;
for (let i = 0; i < items.length; i++) {
if (resource === 'contact') {
// **********************************************************************
// contact
// **********************************************************************
if (operation === 'create') {
// ----------------------------------------
// contact: create
// ----------------------------------------
// https://developers.gohighlevel.com/#3ae04a5f-a029-4855-8ad2-bccdf2a858ae
const body: IDataObject = {
email: this.getNodeParameter('email', i),
};
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (Object.keys(additionalFields).length) {
Object.assign(body, additionalFields);
}
responseData = await highLevelApiRequest.call(this, 'POST', `/contacts`, body);
} else if (operation === 'delete') {
// ----------------------------------------
// contact: delete
// ----------------------------------------
// https://developers.gohighlevel.com/#58f28e16-b4d5-47f9-9378-5f22d2189706
const contactId = this.getNodeParameter('contactId', i);
const endpoint = `/contacts/${contactId}`;
responseData = await highLevelApiRequest.call(this, 'DELETE', endpoint);
} else if (operation === 'get') {
// ----------------------------------------
// contact: get
// ----------------------------------------
// https://developers.gohighlevel.com/#cc2dd625-abaa-4cdd-b506-46b8e36f6252
const contactId = this.getNodeParameter('contactId', i);
const endpoint = `/contacts/${contactId}`;
responseData = await highLevelApiRequest.call(this, 'GET', endpoint);
} else if (operation === 'update') {
// ----------------------------------------
// contact: update
// ----------------------------------------
// https://developers.gohighlevel.com/#373479f5-6aaa-4be4-b854-40f0f78cd752
const contactId = this.getNodeParameter('contactId', i);
const body: IDataObject = {};
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
if (Object.keys(updateFields).length) {
Object.assign(body, updateFields);
}
const endpoint = `/contacts/${contactId}`;
responseData = await highLevelApiRequest.call(this, 'PUT', endpoint, body);
}
}
Array.isArray(responseData)
? returnData.push(...responseData)
: returnData.push(responseData);
}
return [this.helpers.returnJsonArray(returnData)];
}
}

View file

@ -0,0 +1,283 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const contactOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'contact',
],
},
},
options: [
{
name: 'Create',
value: 'create',
},
{
name: 'Delete',
value: 'delete',
},
{
name: 'Get',
value: 'get',
},
{
name: 'Update',
value: 'update',
},
],
default: 'create',
description: 'Operation to perform',
},
] as INodeProperties[];
export const contactFields = [
// ----------------------------------------
// contact: create
// ----------------------------------------
{
displayName: 'Email',
name: 'email',
description: 'Email of the contact to create.',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'contact',
],
operation: [
'create',
],
},
},
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
default: {},
displayOptions: {
show: {
resource: [
'contact',
],
operation: [
'create',
],
},
},
options: [
{
displayName: 'Address',
name: 'address1',
type: 'string',
default: '',
description: 'Address of the contact to create.',
},
{
displayName: 'City',
name: 'city',
type: 'string',
default: '',
description: 'City of the contact to create.',
},
{
displayName: 'First Name',
name: 'firstName',
type: 'string',
default: '',
description: 'First name of the contact to create.',
},
{
displayName: 'Last Name',
name: 'lastName',
type: 'string',
default: '',
description: 'Last name of the contact to create.',
},
{
displayName: 'Full Name',
name: 'name',
type: 'string',
default: '',
description: 'Full name of the contact to create.',
},
{
displayName: 'Phone',
name: 'phone',
type: 'string',
default: '',
description: 'Phone of the contact to create.',
},
{
displayName: 'Postal Code',
name: 'postalCode',
type: 'string',
default: '',
description: 'Postal code of the contact to create.',
},
{
displayName: 'State',
name: 'state',
type: 'string',
default: '',
description: 'State of the contact to create.',
},
],
},
// ----------------------------------------
// contact: delete
// ----------------------------------------
{
displayName: 'Contact ID',
name: 'contactId',
description: 'ID of the contact to delete.',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'contact',
],
operation: [
'delete',
],
},
},
},
// ----------------------------------------
// contact: get
// ----------------------------------------
{
displayName: 'Contact ID',
name: 'contactId',
description: 'ID of the contact to retrieve.',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'contact',
],
operation: [
'get',
],
},
},
},
// ----------------------------------------
// contact: update
// ----------------------------------------
{
displayName: 'Contact ID',
name: 'contactId',
description: 'ID of the contact to update.',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'contact',
],
operation: [
'update',
],
},
},
},
{
displayName: 'Update Fields',
name: 'updateFields',
type: 'collection',
default: {},
displayOptions: {
show: {
resource: [
'contact',
],
operation: [
'update',
],
},
},
options: [
{
displayName: 'Address',
name: 'address1',
type: 'string',
default: '',
description: 'Address to set for the contact.',
},
{
displayName: 'City',
name: 'city',
type: 'string',
default: '',
description: 'City to set for the contact.',
},
{
displayName: 'Email',
name: 'email',
type: 'string',
default: '',
description: 'Email to set for the contact.',
},
{
displayName: 'First Name',
name: 'firstName',
type: 'string',
default: '',
description: 'First name to set for the contact.',
},
{
displayName: 'Last Name',
name: 'lastName',
type: 'string',
default: '',
description: 'Last name to set for the contact.',
},
{
displayName: 'Full Name',
name: 'name',
type: 'string',
default: '',
description: 'Full name to set for the contact.',
},
{
displayName: 'Phone',
name: 'phone',
type: 'string',
default: '',
description: 'Phone number to set for the contact.',
},
{
displayName: 'Postal Code',
name: 'postalCode',
type: 'string',
default: '',
description: 'Postal code to set for the contact.',
},
{
displayName: 'State',
name: 'state',
type: 'string',
default: '',
description: 'State to set for the contact.',
},
],
},
] as INodeProperties[];

View file

@ -0,0 +1 @@
export * from './ContactDescription';

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

View file

@ -113,6 +113,7 @@
"dist/credentials/HarvestApi.credentials.js", "dist/credentials/HarvestApi.credentials.js",
"dist/credentials/HarvestOAuth2Api.credentials.js", "dist/credentials/HarvestOAuth2Api.credentials.js",
"dist/credentials/HelpScoutOAuth2Api.credentials.js", "dist/credentials/HelpScoutOAuth2Api.credentials.js",
"dist/credentials/HighLevelApi.credentials.js",
"dist/credentials/HttpBasicAuth.credentials.js", "dist/credentials/HttpBasicAuth.credentials.js",
"dist/credentials/HttpDigestAuth.credentials.js", "dist/credentials/HttpDigestAuth.credentials.js",
"dist/credentials/HttpHeaderAuth.credentials.js", "dist/credentials/HttpHeaderAuth.credentials.js",
@ -377,6 +378,7 @@
"dist/nodes/Harvest/Harvest.node.js", "dist/nodes/Harvest/Harvest.node.js",
"dist/nodes/HelpScout/HelpScout.node.js", "dist/nodes/HelpScout/HelpScout.node.js",
"dist/nodes/HelpScout/HelpScoutTrigger.node.js", "dist/nodes/HelpScout/HelpScoutTrigger.node.js",
"dist/nodes/HighLevel/HighLevel.node.js",
"dist/nodes/HtmlExtract/HtmlExtract.node.js", "dist/nodes/HtmlExtract/HtmlExtract.node.js",
"dist/nodes/HttpRequest.node.js", "dist/nodes/HttpRequest.node.js",
"dist/nodes/Hubspot/Hubspot.node.js", "dist/nodes/Hubspot/Hubspot.node.js",