Skip to content

Commit 262b0ad

Browse files
author
drowl87
committed
additional tweaks
1 parent 5ef3457 commit 262b0ad

File tree

3 files changed

+71
-43
lines changed

3 files changed

+71
-43
lines changed

nodes/DynamicNode/DynamicNode.node.ts

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import {
44
INodeType,
55
INodeTypeDescription,
66
NodeOperationError,
7-
NodeConnectionType,
87
} from 'n8n-workflow';
9-
import { WorkflowExecute } from 'n8n-core';
8+
9+
// JSON import requires "resolveJsonModule" in tsconfig
10+
import subWorkflowTemplate from './subWorkflowTemplate.json';
1011

1112
export class DynamicNode implements INodeType {
1213
description: INodeTypeDescription = {
@@ -15,57 +16,57 @@ export class DynamicNode implements INodeType {
1516
icon: 'file:dynamicNode.svg',
1617
group: ['transform'],
1718
version: 1,
18-
defaults: {
19-
name: 'Dynamic Node',
20-
color: '#00BB00',
21-
},
22-
inputs: ['main'] as NodeConnectionType[],
23-
outputs: ['main'] as NodeConnectionType[],
19+
defaults: { name: 'Dynamic Node', color: '#00BB00' },
20+
inputs: ['main'],
21+
outputs: ['main'],
2422
properties: [
2523
{
2624
displayName: 'Node JSON',
2725
name: 'nodeJson',
2826
type: 'json',
2927
default: {},
30-
description: 'Full node definition (from a workflow export) to execute',
28+
description: 'Paste in your exported node JSON here',
3129
},
3230
],
3331
};
3432

3533
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
36-
// 1) Grab and validate the JSON the user pasted in
37-
const nodeJson = this.getNodeParameter('nodeJson', 0) as any;
38-
if (typeof nodeJson !== 'object') {
39-
throw new NodeOperationError(this.getNode(), 'The Node JSON must be a valid object');
34+
// 1) Pull in the incoming items
35+
const items = this.getInputData();
36+
37+
// 2) Get the user-provided node JSON
38+
const raw = this.getNodeParameter('nodeJson', 0) as any;
39+
if (typeof raw !== 'object') {
40+
throw new NodeOperationError(this.getNode(), 'Node JSON must be an object');
4041
}
4142

42-
// 2) Build a one-node workflow around it
43-
const workflowData = {
44-
name: 'DynamicWorkflow',
45-
nodes: [
46-
{
47-
...nodeJson,
48-
position: [0, 0],
49-
},
50-
],
51-
connections: {},
52-
};
43+
// 3) Clone the sub-workflow template
44+
const template = JSON.parse(JSON.stringify(subWorkflowTemplate)) as any;
5345

54-
// 3) Pull in whatever “additionalData” n8n-core needs
55-
// (this.getWorkflow() is your current running workflow instance)
56-
const additionalData = await (this.getWorkflow() as any).getActiveWorkflow();
57-
const workflow = new WorkflowExecute(additionalData, workflowData);
46+
// 4) Make sure the JSON includes a name
47+
if (!raw.name) {
48+
throw new NodeOperationError(this.getNode(), 'Your JSON must include a `name` field');
49+
}
50+
// 5) Inject the node definition
51+
template.nodes.push(raw);
52+
// 6) Wire Start -> your node
53+
template.connections.Start.main[0][0].node = raw.name;
5854

59-
// 4) Run it in “manual” mode
60-
const runResult = await workflow.run('manual', {} as any);
55+
// 7) Execute the mini-workflow
56+
const executionResult = await this.executeWorkflow(
57+
{ code: template },
58+
items,
59+
);
6160

62-
// 5) Extract just the data for our node by name
63-
const nodeName = nodeJson.name;
64-
const runData = (runResult as any).runData
65-
?.resultData
66-
?.runData[nodeName] as INodeExecutionData[][];
61+
// 8) Normalize the result: either it's an array or { data }
62+
let outputData: INodeExecutionData[][];
63+
if (Array.isArray(executionResult)) {
64+
outputData = executionResult;
65+
} else {
66+
outputData = (executionResult as any).data;
67+
}
6768

68-
// 6) Return that so n8n pipes it downstream
69-
return runData || [[]];
69+
// 9) Prepare and return
70+
return this.prepareOutputData(outputData);
7071
}
71-
}
72+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "DynamicSubflow",
3+
"nodes": [
4+
{
5+
"parameters": {},
6+
"name": "Start",
7+
"type": "n8n-nodes-base.start",
8+
"typeVersion": 1,
9+
"position": [0, 0]
10+
}
11+
],
12+
"connections": {
13+
"Start": {
14+
"main": [
15+
[
16+
{
17+
"node": "__PLACEHOLDER__",
18+
"type": "main",
19+
"index": 0
20+
}
21+
]
22+
]
23+
}
24+
}
25+
}

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
"pnpm": ">=9.1"
2525
},
2626
"packageManager": "[email protected]",
27-
"main": "dist/DynamicNode.node.js",
27+
"main": "dist/index.js",
2828
"scripts": {
29-
"build": "tsc",
29+
"build": "tsc && gulp build:icons",
3030
"prepare": "npm run build",
3131
"test": "echo \"No tests specified\" && exit 0",
3232
"publish": "npm publish"
@@ -38,7 +38,7 @@
3838
"n8nNodesApiVersion": 1,
3939
"credentials": [],
4040
"nodes": [
41-
"dist/nodes/DynamicNode/DynamicNode.node.js"
41+
"dist/*.node.js"
4242
]
4343
},
4444
"devDependencies": {
@@ -49,9 +49,11 @@
4949
"prettier": "^3.5.3",
5050
"typescript": "^5.8.3"
5151
},
52+
"dependencies": {
53+
"n8n-workflow": "^1.90.0"
54+
},
5255
"peerDependencies": {
53-
"n8n-workflow": "*",
54-
"n8n-core": "*"
56+
"n8n-workflow": "^1.90.0"
5557
},
5658
"bugs": {
5759
"url": "https://github.com/drowl87/n8n-nodes-dynamic-node/issues"

0 commit comments

Comments
 (0)