mirror of
https://github.com/n8n-io/n8n-nodes-starter.git
synced 2025-10-28 22:12:26 -05:00
Adding FriendGrid example node (updated version of the one in the n8n docs)
This commit is contained in:
parent
1e43acf743
commit
a7dffc4df0
8 changed files with 263 additions and 72 deletions
|
|
@ -1,6 +1,6 @@
|
|||
# n8n-nodes-starter
|
||||
|
||||

|
||||

|
||||
|
||||
Example starter module for custom n8n nodes.
|
||||
|
||||
|
|
|
|||
|
|
@ -3,25 +3,19 @@ import {
|
|||
NodePropertyTypes,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
|
||||
export class ExampleCredentials implements ICredentialType {
|
||||
name = 'exampleCredentials';
|
||||
displayName = 'Example Credentials';
|
||||
export class FriendGridApi implements ICredentialType {
|
||||
name = 'friendGridApi';
|
||||
displayName = 'FriendGrid API';
|
||||
documentationUrl = 'friendGrid';
|
||||
properties = [
|
||||
// The credentials to get from user and save encrypted.
|
||||
// Properties can be defined exactly in the same way
|
||||
// as node properties.
|
||||
{
|
||||
displayName: 'User',
|
||||
name: 'user',
|
||||
type: 'string' as NodePropertyTypes,
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Access Token',
|
||||
name: 'accessToken',
|
||||
displayName: 'API Key',
|
||||
name: 'apiKey',
|
||||
type: 'string' as NodePropertyTypes,
|
||||
default: '',
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -1,56 +0,0 @@
|
|||
import { IExecuteFunctions } from 'n8n-core';
|
||||
import {
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
export class ExampleNode implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Example Node',
|
||||
name: 'exampleNode',
|
||||
group: ['transform'],
|
||||
version: 1,
|
||||
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
||||
description: 'Basic Example Node',
|
||||
defaults: {
|
||||
name: 'Example Node',
|
||||
color: '#772244',
|
||||
},
|
||||
inputs: ['main'],
|
||||
outputs: ['main'],
|
||||
properties: [
|
||||
// Node properties which the user gets displayed and
|
||||
// can change on the node.
|
||||
{
|
||||
displayName: 'My String',
|
||||
name: 'myString',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'Placeholder value',
|
||||
description: 'The description text',
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
|
||||
const items = this.getInputData();
|
||||
|
||||
let item: INodeExecutionData;
|
||||
let myString: string;
|
||||
|
||||
// Iterates over all input items and add the key "myString" with the
|
||||
// value the parameter "myString" resolves to.
|
||||
// (This could be a different value for each item in case it contains an expression)
|
||||
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
|
||||
myString = this.getNodeParameter('myString', itemIndex, '') as string;
|
||||
item = items[itemIndex];
|
||||
|
||||
item.json['myString'] = myString;
|
||||
}
|
||||
|
||||
return this.prepareOutputData(items);
|
||||
|
||||
}
|
||||
}
|
||||
82
nodes/FriendGrid/ContactDescription.ts
Normal file
82
nodes/FriendGrid/ContactDescription.ts
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
import {
|
||||
INodeProperties,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
export const contactOperations = [
|
||||
{
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'contact',
|
||||
],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
name: 'Create',
|
||||
value: 'create',
|
||||
description: 'Create a contact',
|
||||
},
|
||||
],
|
||||
default: 'create',
|
||||
description: 'The operation to perform.',
|
||||
},
|
||||
] as INodeProperties[];
|
||||
|
||||
export const contactFields = [
|
||||
/*-------------------------------------------------------------------------- */
|
||||
/* contact:create */
|
||||
/* ------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'Email',
|
||||
name: 'email',
|
||||
type: 'string',
|
||||
required: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: [
|
||||
'create',
|
||||
],
|
||||
resource: [
|
||||
'contact',
|
||||
],
|
||||
},
|
||||
},
|
||||
default:'',
|
||||
description:'Primary email for the contact',
|
||||
},
|
||||
{
|
||||
displayName: 'Additional Fields',
|
||||
name: 'additionalFields',
|
||||
type: 'collection',
|
||||
placeholder: 'Add Field',
|
||||
default: {},
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'contact',
|
||||
],
|
||||
operation: [
|
||||
'create',
|
||||
],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
displayName: 'First Name',
|
||||
name: 'firstName',
|
||||
type: 'string',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Last Name',
|
||||
name: 'lastName',
|
||||
type: 'string',
|
||||
default: '',
|
||||
},
|
||||
],
|
||||
},
|
||||
] as INodeProperties[];
|
||||
107
nodes/FriendGrid/FriendGrid.node.ts
Normal file
107
nodes/FriendGrid/FriendGrid.node.ts
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
import {
|
||||
IExecuteFunctions,
|
||||
} from 'n8n-core';
|
||||
|
||||
import {
|
||||
IDataObject,
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import {
|
||||
contactFields,
|
||||
contactOperations,
|
||||
} from './ContactDescription';
|
||||
|
||||
import {
|
||||
friendGridApiRequest,
|
||||
} from './GenericFunctions';
|
||||
|
||||
export class FriendGrid implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'FriendGrid',
|
||||
name: 'friendGrid',
|
||||
icon: 'file:friendGrid.svg',
|
||||
group: ['transform'],
|
||||
version: 1,
|
||||
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
||||
description: 'Consume FriendGrid API',
|
||||
defaults: {
|
||||
name: 'FriendGrid',
|
||||
color: '#1A82e2',
|
||||
},
|
||||
inputs: ['main'],
|
||||
outputs: ['main'],
|
||||
credentials: [
|
||||
{
|
||||
name: 'friendGridApi',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Resource',
|
||||
name: 'resource',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Contact',
|
||||
value: 'contact',
|
||||
},
|
||||
],
|
||||
default: 'contact',
|
||||
required: true,
|
||||
description: 'Resource to consume',
|
||||
},
|
||||
...contactOperations,
|
||||
...contactFields,
|
||||
],
|
||||
};
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = this.getInputData();
|
||||
let responseData;
|
||||
const returnData: IDataObject[] = [];
|
||||
const resource = this.getNodeParameter('resource', 0) as string;
|
||||
const operation = this.getNodeParameter('operation', 0) as string;
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
try {
|
||||
if (resource === 'contact') {
|
||||
if (operation === 'create') {
|
||||
|
||||
const email = this.getNodeParameter('email', i) as string;
|
||||
|
||||
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
||||
const data: IDataObject = {
|
||||
email,
|
||||
};
|
||||
|
||||
Object.assign(data, additionalFields);
|
||||
|
||||
const body: IDataObject = {
|
||||
contacts: [
|
||||
data,
|
||||
],
|
||||
};
|
||||
|
||||
responseData = await friendGridApiRequest.call(this, 'POST', '/contact', body);
|
||||
}
|
||||
}
|
||||
if (Array.isArray(responseData)) {
|
||||
returnData.push.apply(returnData, responseData as IDataObject[]);
|
||||
} else {
|
||||
returnData.push(responseData as IDataObject);
|
||||
}
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({ error: error.message });
|
||||
continue;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
return [this.helpers.returnJsonArray(returnData)];
|
||||
}
|
||||
}
|
||||
53
nodes/FriendGrid/GenericFunctions.ts
Normal file
53
nodes/FriendGrid/GenericFunctions.ts
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import {
|
||||
OptionsWithUri,
|
||||
} from 'request';
|
||||
|
||||
import {
|
||||
IExecuteFunctions,
|
||||
IExecuteSingleFunctions,
|
||||
IHookFunctions,
|
||||
ILoadOptionsFunctions,
|
||||
} from 'n8n-core';
|
||||
|
||||
import {
|
||||
IDataObject,
|
||||
NodeApiError,
|
||||
NodeOperationError,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
export async function friendGridApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
||||
method: string, endpoint: string, body?: object, query?: object, uri?: string): Promise<any> { // tslint:disable-line:no-any
|
||||
|
||||
//Get credentials the user provided for this node
|
||||
const credentials = await this.getCredentials('friendGridApi') as IDataObject;
|
||||
|
||||
if (credentials === undefined) {
|
||||
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
||||
}
|
||||
|
||||
//Make http request according to <https://sendgrid.com/docs/api-reference/>
|
||||
const options: OptionsWithUri = {
|
||||
method,
|
||||
headers: {
|
||||
'Accept': ' application/json',
|
||||
'Authorization': `Bearer ${credentials.apiKey}`,
|
||||
},
|
||||
qs: query,
|
||||
body,
|
||||
uri: uri || `https://api.sendgrid.com/v3/marketing/${endpoint}`,
|
||||
json: true,
|
||||
};
|
||||
|
||||
if (Object.keys(options.qs).length === 0) {
|
||||
delete options.qs;
|
||||
}
|
||||
if (Object.keys(options.body).length === 0) {
|
||||
delete options.body;
|
||||
}
|
||||
|
||||
try {
|
||||
return this.helpers.request!(options);
|
||||
} catch (error) {
|
||||
throw new NodeApiError(this.getNode(), error);
|
||||
}
|
||||
}
|
||||
11
nodes/FriendGrid/friendGrid.svg
Normal file
11
nodes/FriendGrid/friendGrid.svg
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg width="256px" height="256px" viewBox="0 0 256 256" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid">
|
||||
<g>
|
||||
<path d="M256.000405,0 L256.000405,170.666936 L170.666936,170.666936 L170.666936,255.996382 L0.00201096905,255.996382 L0.002,170.666 L0,170.666936 L0,85.3314569 L85.3334681,85.3314569 L85.3334681,0 L256.000405,0 Z" fill="#9DD6E3"></path>
|
||||
<polygon fill="#3F72AB" points="0.00201096905 255.996382 85.3354791 255.996382 85.3354791 170.662915 0.00201096905 170.662915"></polygon>
|
||||
<polygon fill="#00A9D1" points="170.666936 170.666936 256.000405 170.666936 256.000405 85.3314569 170.666936 85.3314569"></polygon>
|
||||
<polygon fill="#00A9D1" points="85.3334681 85.3334679 170.666936 85.3334679 170.666936 0 85.3334681 0"></polygon>
|
||||
<polygon fill="#2191C4" points="85.3334681 170.664925 170.666936 170.664925 170.666936 85.3314569 85.3334681 85.3314569"></polygon>
|
||||
<polygon fill="#3F72AB" points="170.666936 85.3334679 256.000405 85.3334679 256.000405 0 170.666936 0"></polygon>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 1.1 KiB |
|
|
@ -25,10 +25,10 @@
|
|||
],
|
||||
"n8n": {
|
||||
"credentials": [
|
||||
"dist/credentials/ExampleCredentials.credentials.js"
|
||||
"dist/credentials/FriendGridApi.credentials.js"
|
||||
],
|
||||
"nodes": [
|
||||
"dist/nodes/ExampleNode/ExampleNode.node.js"
|
||||
"dist/nodes/FriendGrid/FriendGrid.node.js"
|
||||
]
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue