Adding FriendGrid example node (updated version of the one in the n8n docs)

This commit is contained in:
Valentina 2021-12-02 12:23:05 +02:00
commit a7dffc4df0
8 changed files with 263 additions and 72 deletions

View 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[];

View 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)];
}
}

View 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);
}
}

View 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