| 
									
										
										
										
											2024-10-21 17:00:23 +02:00
										 |  |  | import type { | 
					
						
							| 
									
										
										
										
											2023-08-14 10:22:59 +02:00
										 |  |  | 	IExecuteFunctions, | 
					
						
							| 
									
										
										
										
											2022-06-30 17:09:31 +02:00
										 |  |  | 	INodeExecutionData, | 
					
						
							|  |  |  | 	INodeType, | 
					
						
							|  |  |  | 	INodeTypeDescription, | 
					
						
							|  |  |  | } from 'n8n-workflow'; | 
					
						
							| 
									
										
										
										
											2024-10-21 17:00:23 +02:00
										 |  |  | import { NodeConnectionType, NodeOperationError } from 'n8n-workflow'; | 
					
						
							| 
									
										
										
										
											2019-10-03 08:53:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export class ExampleNode implements INodeType { | 
					
						
							|  |  |  | 	description: INodeTypeDescription = { | 
					
						
							|  |  |  | 		displayName: 'Example Node', | 
					
						
							|  |  |  | 		name: 'exampleNode', | 
					
						
							|  |  |  | 		group: ['transform'], | 
					
						
							|  |  |  | 		version: 1, | 
					
						
							|  |  |  | 		description: 'Basic Example Node', | 
					
						
							|  |  |  | 		defaults: { | 
					
						
							|  |  |  | 			name: 'Example Node', | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2024-10-21 17:00:23 +02:00
										 |  |  | 		inputs: [NodeConnectionType.Main], | 
					
						
							|  |  |  | 		outputs: [NodeConnectionType.Main], | 
					
						
							| 
									
										
										
										
											2025-05-13 14:26:01 +01:00
										 |  |  | 		usableAsTool: true, | 
					
						
							| 
									
										
										
										
											2019-10-03 08:53:03 +02:00
										 |  |  | 		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', | 
					
						
							| 
									
										
										
										
											2022-01-17 18:29:15 +01:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 		], | 
					
						
							| 
									
										
										
										
											2019-10-03 08:53:03 +02:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-29 14:12:28 +02:00
										 |  |  | 	// The function below is responsible for actually doing whatever this node
 | 
					
						
							|  |  |  | 	// is supposed to do. In this case, we're just appending the `myString` property
 | 
					
						
							|  |  |  | 	// with whatever the user has entered.
 | 
					
						
							|  |  |  | 	// You can make async calls and use `await`.
 | 
					
						
							| 
									
										
										
										
											2019-10-03 08:53:03 +02:00
										 |  |  | 	async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { | 
					
						
							|  |  |  | 		const items = this.getInputData(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		let item: INodeExecutionData; | 
					
						
							|  |  |  | 		let myString: string; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-27 15:09:59 +02:00
										 |  |  | 		// Iterates over all input items and add the key "myString" with the
 | 
					
						
							| 
									
										
										
										
											2019-10-03 08:53:03 +02:00
										 |  |  | 		// 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++) { | 
					
						
							| 
									
										
										
										
											2022-06-29 14:12:28 +02:00
										 |  |  | 			try { | 
					
						
							|  |  |  | 				myString = this.getNodeParameter('myString', itemIndex, '') as string; | 
					
						
							|  |  |  | 				item = items[itemIndex]; | 
					
						
							| 
									
										
										
										
											2019-10-03 08:53:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 17:00:23 +02:00
										 |  |  | 				item.json.myString = myString; | 
					
						
							| 
									
										
										
										
											2022-06-29 14:12:28 +02:00
										 |  |  | 			} catch (error) { | 
					
						
							|  |  |  | 				// This node should never fail but we want to showcase how
 | 
					
						
							|  |  |  | 				// to handle errors.
 | 
					
						
							|  |  |  | 				if (this.continueOnFail()) { | 
					
						
							| 
									
										
										
										
											2022-07-12 10:25:39 +02:00
										 |  |  | 					items.push({ json: this.getInputData(itemIndex)[0].json, error, pairedItem: itemIndex }); | 
					
						
							| 
									
										
										
										
											2022-06-29 14:12:28 +02:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					// Adding `itemIndex` allows other workflows to handle this error
 | 
					
						
							| 
									
										
										
										
											2022-06-29 14:55:31 +02:00
										 |  |  | 					if (error.context) { | 
					
						
							|  |  |  | 						// If the error thrown already contains the context property,
 | 
					
						
							|  |  |  | 						// only append the itemIndex
 | 
					
						
							| 
									
										
										
										
											2022-06-29 14:54:53 +02:00
										 |  |  | 						error.context.itemIndex = itemIndex; | 
					
						
							|  |  |  | 						throw error; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2022-06-29 16:27:55 +02:00
										 |  |  | 					throw new NodeOperationError(this.getNode(), error, { | 
					
						
							| 
									
										
										
										
											2022-06-29 14:54:53 +02:00
										 |  |  | 						itemIndex, | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2022-06-29 14:12:28 +02:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-10-03 08:53:03 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-21 17:00:23 +02:00
										 |  |  | 		return [items]; | 
					
						
							| 
									
										
										
										
											2019-10-03 08:53:03 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |