Skip to content

Commit 0db850d

Browse files
author
drowl87
committed
refactor DynamicNode to improve parameter naming and enhance execution logic; bump version to 0.3.0
1 parent 582b94e commit 0db850d

2 files changed

Lines changed: 52 additions & 10 deletions

File tree

nodes/DynamicNode/DynamicNode.node.ts

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ export class DynamicNode implements INodeType {
4040
name: 'disableWait',
4141
type: 'boolean',
4242
default: false,
43-
description: 'Whether to return immediately after starting the sub-workflow. Advanced: if enabled, parent will not wait for results and outputs may be empty.',
43+
description: 'Advanced: if enabled, parent will not wait for results and outputs may be empty.',
4444
},
4545
],
4646
};
4747

4848
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
4949
const inputItems = this.getInputData();
5050
const executeIndividually = this.getNodeParameter('executeIndividually', 0) as boolean;
51-
const doNotWaitToFinish = this.getNodeParameter('doNotWaitToFinish', 0) as boolean;
51+
const disableWait = this.getNodeParameter('disableWait', 0) as boolean;
5252

5353
const rawParam = this.getNodeParameter('nodeJson', 0) as any;
5454
let raw: any;
@@ -101,15 +101,15 @@ export class DynamicNode implements INodeType {
101101
executionId: workflowProxy.$execution.id,
102102
workflowId: workflowProxy.$workflow.id,
103103
},
104-
doNotWaitToFinish,
104+
disableWait,
105105
},
106106
);
107107

108-
if (!doNotWaitToFinish && execResult) {
108+
if (!disableWait && execResult) {
109109
if (Array.isArray(execResult)) {
110110
const flattened = execResult
111111
.flat()
112-
.filter((item: unknown): item is INodeExecutionData => item !== null && typeof item === 'object');
112+
.filter((entry: unknown): entry is INodeExecutionData => entry !== null && typeof entry === 'object');
113113
allResults.push(...flattened);
114114
} else if (
115115
typeof execResult === 'object' &&
@@ -118,7 +118,7 @@ export class DynamicNode implements INodeType {
118118
) {
119119
const flattened = (execResult as any).data
120120
.flat()
121-
.filter((item: unknown): item is INodeExecutionData => item !== null && typeof item === 'object');
121+
.filter((entry: unknown): entry is INodeExecutionData => entry !== null && typeof entry === 'object');
122122
allResults.push(...flattened);
123123
}
124124
}
@@ -129,12 +129,54 @@ export class DynamicNode implements INodeType {
129129
try {
130130
await processItem(inputItems[i], i);
131131
} catch (err) {
132-
this.logger.warn(`DynamicNode: Error processing item #${i + 1}: ${err.message}`);
132+
this.logger.warn(`DynamicNode: Error processing item #${i + 1}: ${err instanceof Error ? err.message : String(err)}`);
133133
}
134134
}
135135
} else {
136-
// Run one sub-workflow with all items
137-
await processItem({ json: {} }, 0);
136+
const template = JSON.parse(JSON.stringify(subWorkflowTemplate));
137+
const nodeClone = JSON.parse(JSON.stringify(baseNode));
138+
139+
nodeClone.name = `${baseNode.name} - Dynamic Node [all]`;
140+
nodeClone.id = `dynamic-${uuidv4()}`;
141+
nodeClone.position = Array.isArray(baseNode.position) && baseNode.position.length === 2
142+
? baseNode.position
143+
: [240, 0];
144+
145+
template.nodes.push(nodeClone);
146+
template.connections.Start.main[0][0].node = nodeClone.name;
147+
148+
const workflowProxy = this.getWorkflowDataProxy(0);
149+
150+
const execResult = await this.executeWorkflow(
151+
{ code: template },
152+
inputItems,
153+
{},
154+
{
155+
parentExecution: {
156+
executionId: workflowProxy.$execution.id,
157+
workflowId: workflowProxy.$workflow.id,
158+
},
159+
disableWait,
160+
},
161+
);
162+
163+
if (!disableWait && execResult) {
164+
if (Array.isArray(execResult)) {
165+
const flattened = execResult
166+
.flat()
167+
.filter((entry: unknown): entry is INodeExecutionData => entry !== null && typeof entry === 'object');
168+
allResults.push(...flattened);
169+
} else if (
170+
typeof execResult === 'object' &&
171+
'data' in execResult &&
172+
Array.isArray((execResult as any).data)
173+
) {
174+
const flattened = (execResult as any).data
175+
.flat()
176+
.filter((entry: unknown): entry is INodeExecutionData => entry !== null && typeof entry === 'object');
177+
allResults.push(...flattened);
178+
}
179+
}
138180
}
139181

140182
return [allResults];

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "n8n-nodes-dynamic-node",
3-
"version": "0.2.9",
3+
"version": "0.3.0",
44
"description": "A dynamic n8n node wrapper that can execute any node JSON by feeding it at runtime.",
55
"keywords": [
66
"n8n-community-node-package",

0 commit comments

Comments
 (0)