🔀 Merge branch 'feature/copper-trigger' of https://github.com/RicardoE105/n8n into RicardoE105-feature/copper-trigger

This commit is contained in:
Jan Oberhauser 2020-01-17 22:40:06 -06:00
commit 69fcf2c2a2
5 changed files with 239 additions and 0 deletions

View file

@ -0,0 +1,31 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class CopperApi implements ICredentialType {
name = 'copperApi';
displayName = 'Copper API';
properties = [
{
displayName: 'API Key',
name: 'apiKey',
required: true,
type: 'string' as NodePropertyTypes,
default: '',
},
{
displayName: 'Email',
name: 'email',
required: true,
type: 'string' as NodePropertyTypes,
default: '',
},
{
displayName: 'Secret',
name: 'secret',
type: 'string' as NodePropertyTypes,
default: '',
},
];
}

View file

@ -0,0 +1,162 @@
import {
IHookFunctions,
IWebhookFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeTypeDescription,
INodeType,
IWebhookResponseData,
} from 'n8n-workflow';
import {
copperApiRequest,
} from './GenericFunctions';
export class CopperTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Copper Trigger',
name: 'copper',
icon: 'file:copper.png',
group: ['trigger'],
version: 1,
description: 'Handle Copper events via webhooks',
defaults: {
name: 'Copper Trigger',
color: '#ff2564',
},
inputs: [],
outputs: ['main'],
credentials: [
{
name: 'copperApi',
required: true,
}
],
webhooks: [
{
name: 'default',
httpMethod: 'POST',
responseMode: 'onReceived',
path: 'webhook',
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
required: true,
default: '',
options: [
{
name: 'Lead',
value: 'lead',
},
{
name: 'Person',
value: 'person',
},
{
name: 'Company',
value: 'company',
},
{
name: 'Opportunity',
value: 'opportunity',
},
{
name: 'Project',
value: 'project',
},
],
description: 'The resource is gonna fire the event',
},
{
displayName: 'Event',
name: 'event',
type: 'options',
required: true,
default: '',
options: [
{
name: 'New',
value: 'new',
description: 'A new record is created',
},
{
name: 'Update',
value: 'update',
description: 'Any field in the existing entity record is changed',
},
{
name: 'Delete',
value: 'delete',
description: 'An existing record is removed',
},
],
description: 'The resource is gonna fire the event',
},
],
};
// @ts-ignore
webhookMethods = {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
if (webhookData.webhookId === undefined) {
return false;
}
const endpoint = `/webhooks/${webhookData.webhookId}`;
try {
await copperApiRequest.call(this, 'GET', endpoint);
} catch (err) {
return false
}
return true;
},
async create(this: IHookFunctions): Promise<boolean> {
const webhookUrl = this.getNodeWebhookUrl('default');
const webhookData = this.getWorkflowStaticData('node');
const resource = this.getNodeParameter('resource') as string;
const event = this.getNodeParameter('event') as string;
const endpoint = '/webhooks';
const body: IDataObject = {
target: webhookUrl,
type: resource,
event: event,
};
const { id } = await copperApiRequest.call(this, 'POST', endpoint, body);
webhookData.webhookId = id;
return true;
},
async delete(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
const endpoint = `/webhooks/${webhookData.webhookId}`;
try {
await copperApiRequest.call(this, 'DELETE', endpoint);
} catch(error) {
return false;
}
delete webhookData.webhookId;
return true;
},
},
};
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const credentials = this.getCredentials('copperApi');
const req = this.getRequestObject();
if (credentials!.secret) {
if (req.body.secret !== credentials!.secret) {
return {};
};
}
return {
workflowData: [
this.helpers.returnJsonArray(req.body),
],
};
}
}

View file

@ -0,0 +1,44 @@
import { OptionsWithUri } from 'request';
import {
IExecuteFunctions,
IExecuteSingleFunctions,
IHookFunctions,
ILoadOptionsFunctions,
IWebhookFunctions,
} from 'n8n-core';
import { IDataObject } from 'n8n-workflow';
export async function copperApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('copperApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
if (credentials.secret) {
body.secret = {
secret: credentials.secret as string,
};
};
let options: OptionsWithUri = {
headers: {
'X-PW-AccessToken': credentials.apiKey,
'X-PW-Application': 'developer_api',
'X-PW-UserEmail': credentials.email,
'Content-Type': 'application/json',
},
method,
qs,
body,
uri: uri ||`https://api.prosperworks.com/developer_api/v1${resource}`,
json: true
};
options = Object.assign({}, options, option);
if (Object.keys(options.body).length === 0) {
delete options.body;
}
try {
return await this.helpers.request!(options);
} catch (error) {
throw new Error('Copper Error: ' + error.message);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.8 KiB

View file

@ -34,6 +34,7 @@
"dist/credentials/BitbucketApi.credentials.js",
"dist/credentials/ChargebeeApi.credentials.js",
"dist/credentials/CodaApi.credentials.js",
"dist/credentials/CopperApi.credentials.js",
"dist/credentials/DropboxApi.credentials.js",
"dist/credentials/EventbriteApi.credentials.js",
"dist/credentials/FreshdeskApi.credentials.js",
@ -95,6 +96,7 @@
"dist/nodes/Chargebee/Chargebee.node.js",
"dist/nodes/Chargebee/ChargebeeTrigger.node.js",
"dist/nodes/Coda/Coda.node.js",
"dist/nodes/Copper/CopperTrigger.node.js",
"dist/nodes/Cron.node.js",
"dist/nodes/Discord/Discord.node.js",
"dist/nodes/Dropbox/Dropbox.node.js",