mirror of
				https://github.com/n8n-io/n8n-nodes-starter.git
				synced 2025-10-30 14:52:27 -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
				
			
		
							
								
								
									
										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)]; | ||||
| 	} | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue