Refactor: version, node description and if-else statements

This commit is contained in:
Valentina Lilova 2022-05-31 15:00:22 +03:00
commit 1420b0248c
3 changed files with 80 additions and 35 deletions

View file

@ -10,6 +10,7 @@ import {
INodeExecutionData, INodeExecutionData,
INodeType, INodeType,
INodeTypeDescription, INodeTypeDescription,
NodeOperationError,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { import {
@ -53,6 +54,7 @@ export class FriendGrid implements INodeType {
{ {
displayName: 'Resource', displayName: 'Resource',
name: 'resource', name: 'resource',
required: true,
type: 'options', type: 'options',
options: [ options: [
{ {
@ -61,11 +63,17 @@ export class FriendGrid implements INodeType {
}, },
], ],
default: 'contact', default: 'contact',
required: true,
description: 'Resource to consume', description: 'Resource to consume',
}, },
...contactOperations, ...contactOperations,
...contactFields, ...contactFields,
{
displayName: 'Simplify Output',
name: 'simplifyOutput',
type: 'boolean',
default: false,
description: 'Whether to simplify the output data',
},
], ],
}; };
@ -113,33 +121,80 @@ export class FriendGrid implements INodeType {
const items = this.getInputData(); const items = this.getInputData();
let responseData; let responseData;
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
let method: string;
let endpoint: string;
const body: IDataObject = {};
const qs: IDataObject = {}; // query string
const resource = this.getNodeParameter('resource', 0) as string; const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string; const operation = this.getNodeParameter('operation', 0) as string;
let body: IDataObject = {}; let additionalFields;
const qs: IDataObject = {}; // query string
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
try { try {
if (resource === 'contact') { switch (resource) {
if (operation === 'create') { case 'contact':
// https://docs.sendgrid.com/api-reference/contacts/add-or-update-a-contact switch (operation) {
case 'create':
// ----------------------------------
// contact:create
// ----------------------------------
const email = this.getNodeParameter('email', i) as string; // https://docs.sendgrid.com/api-reference/contacts/add-or-update-a-contact
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; endpoint = '';
method = 'POST';
const email = this.getNodeParameter('email', i) as string;
additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const data: IDataObject = { email };
Object.assign(data, additionalFields);
body.contacts = [ data ];
break;
const data: IDataObject = { email }; case 'directContractInsert':
Object.assign(data, additionalFields); // ----------------------------------
// contract:directContractInsert
// ----------------------------------
body = { // .........
contacts: [ break;
data,
],
};
responseData = await friendGridApiRequest.call(this, 'POST', '/marketing/contact', body); default: {
throw new NodeOperationError(this.getNode(), `The operation "${operation}" is not supported for resource "${resource}"!`);
}
}
break;
case 'contractProducts':
switch (operation) {
case 'createDirectInvoice':
// ----------------------------------
// contractProducts:createDirectInvoice
// ----------------------------------
// .........
break;
case 'getDirectInvoice':
// ----------------------------------
// contractProducts:getDirectInvoice
// ----------------------------------
// .........
break;
default: {
throw new NodeOperationError(this.getNode(), `The operation "${operation}" is not supported for resource "${resource}"!`);
}
}
break;
default: {
throw new NodeOperationError(this.getNode(), `The resource "${resource}" is not supported!`);
} }
} }
responseData = await friendGridApiRequest.call(this, method, endpoint, body, qs);
if (Array.isArray(responseData)) { if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]); returnData.push.apply(returnData, responseData as IDataObject[]);
} else if (responseData !== undefined) { } else if (responseData !== undefined) {

View file

@ -9,9 +9,7 @@ export const contactOperations: INodeProperties[] = [
type: 'options', type: 'options',
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: ['contact'],
'contact',
],
}, },
}, },
options: [ options: [
@ -33,20 +31,16 @@ export const contactFields: INodeProperties[] = [
{ {
displayName: 'Email', displayName: 'Email',
name: 'email', name: 'email',
type: 'string',
required: true, required: true,
type: 'string',
displayOptions: { displayOptions: {
show: { show: {
operation: [ resource: ['contact'],
'create', operation: ['create'],
],
resource: [
'contact',
],
}, },
}, },
default:'', default: '',
description:'Primary email for the contact', description: 'Primary email for the contact',
}, },
{ {
displayName: 'Additional Fields', displayName: 'Additional Fields',
@ -56,12 +50,8 @@ export const contactFields: INodeProperties[] = [
default: {}, default: {},
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: ['contact'],
'contact', operation: ['create'],
],
operation: [
'create',
],
}, },
}, },
options: [ options: [

View file

@ -1,6 +1,6 @@
{ {
"name": "@digital-boss/n8n-nodes-starter", "name": "@digital-boss/n8n-nodes-starter",
"version": "0.1.1", "version": "0.1.0",
"description": "FriendGrid support for n8n.", "description": "FriendGrid support for n8n.",
"license": "SEE LICENSE IN LICENSE.md", "license": "SEE LICENSE IN LICENSE.md",
"homepage": "https://n8n.io", "homepage": "https://n8n.io",