File size: 6,369 Bytes
da2e594
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
 * TypeScript type definitions for n8n node parsing
 *
 * This file provides strong typing for node classes and instances,
 * preventing bugs like the v2.17.4 baseDescription issue where
 * TypeScript couldn't catch property name mistakes due to `any` types.
 *
 * @module types/node-types
 * @since 2.17.5
 */

// Import n8n's official interfaces
import type {
  IVersionedNodeType,
  INodeType,
  INodeTypeBaseDescription,
  INodeTypeDescription
} from 'n8n-workflow';

/**
 * Represents a node class that can be either:
 * - A constructor function that returns INodeType
 * - A constructor function that returns IVersionedNodeType
 * - An already-instantiated node instance
 *
 * This covers all patterns we encounter when loading nodes from n8n packages.
 */
export type NodeClass =
  | (new () => INodeType)
  | (new () => IVersionedNodeType)
  | INodeType
  | IVersionedNodeType;

/**
 * Instance of a versioned node type with all properties accessible.
 *
 * This represents nodes that use n8n's VersionedNodeType pattern,
 * such as AI Agent, HTTP Request, Slack, etc.
 *
 * @property currentVersion - The computed current version (defaultVersion ?? max(nodeVersions))
 * @property description - Base description stored as 'description' (NOT 'baseDescription')
 * @property nodeVersions - Map of version numbers to INodeType implementations
 *
 * @example
 * ```typescript
 * const aiAgent = new AIAgentNode() as VersionedNodeInstance;
 * console.log(aiAgent.currentVersion); // 2.2
 * console.log(aiAgent.description.defaultVersion); // 2.2
 * console.log(aiAgent.nodeVersions[1]); // INodeType for version 1
 * ```
 */
export interface VersionedNodeInstance extends IVersionedNodeType {
  currentVersion: number;
  description: INodeTypeBaseDescription;
  nodeVersions: {
    [version: number]: INodeType;
  };
}

/**
 * Instance of a regular (non-versioned) node type.
 *
 * This represents simple nodes that don't use versioning,
 * such as Edit Fields, Set, Code (v1), etc.
 */
export interface RegularNodeInstance extends INodeType {
  description: INodeTypeDescription;
}

/**
 * Union type for any node instance (versioned or regular).
 *
 * Use this when you need to handle both types of nodes.
 */
export type NodeInstance = VersionedNodeInstance | RegularNodeInstance;

/**
 * Type guard to check if a node is a VersionedNodeType instance.
 *
 * This provides runtime type safety and enables TypeScript to narrow
 * the type within conditional blocks.
 *
 * @param node - The node instance to check
 * @returns True if node is a VersionedNodeInstance
 *
 * @example
 * ```typescript
 * const instance = new nodeClass();
 * if (isVersionedNodeInstance(instance)) {
 *   // TypeScript knows instance is VersionedNodeInstance here
 *   console.log(instance.currentVersion);
 *   console.log(instance.nodeVersions);
 * }
 * ```
 */
export function isVersionedNodeInstance(node: any): node is VersionedNodeInstance {
  return (
    node !== null &&
    typeof node === 'object' &&
    'nodeVersions' in node &&
    'currentVersion' in node &&
    'description' in node &&
    typeof node.currentVersion === 'number'
  );
}

/**
 * Type guard to check if a value is a VersionedNodeType class.
 *
 * This checks the constructor name pattern used by n8n's VersionedNodeType.
 *
 * @param nodeClass - The class or value to check
 * @returns True if nodeClass is a VersionedNodeType constructor
 *
 * @example
 * ```typescript
 * if (isVersionedNodeClass(nodeClass)) {
 *   // It's a VersionedNodeType class
 *   const instance = new nodeClass() as VersionedNodeInstance;
 * }
 * ```
 */
export function isVersionedNodeClass(nodeClass: any): boolean {
  return (
    typeof nodeClass === 'function' &&
    nodeClass.prototype?.constructor?.name === 'VersionedNodeType'
  );
}

/**
 * Safely instantiate a node class with proper error handling.
 *
 * Some nodes require specific parameters or environment setup to instantiate.
 * This helper provides safe instantiation with fallback to null on error.
 *
 * @param nodeClass - The node class or instance to instantiate
 * @returns The instantiated node or null if instantiation fails
 *
 * @example
 * ```typescript
 * const instance = instantiateNode(nodeClass);
 * if (instance) {
 *   // Successfully instantiated
 *   const version = isVersionedNodeInstance(instance)
 *     ? instance.currentVersion
 *     : instance.description.version;
 * }
 * ```
 */
export function instantiateNode(nodeClass: NodeClass): NodeInstance | null {
  try {
    if (typeof nodeClass === 'function') {
      return new nodeClass();
    }
    // Already an instance
    return nodeClass;
  } catch (e) {
    // Some nodes require parameters to instantiate
    return null;
  }
}

/**
 * Safely get a node instance, handling both classes and instances.
 *
 * This is a non-throwing version that returns undefined on failure.
 *
 * @param nodeClass - The node class or instance
 * @returns The node instance or undefined
 */
export function getNodeInstance(nodeClass: NodeClass): NodeInstance | undefined {
  const instance = instantiateNode(nodeClass);
  return instance ?? undefined;
}

/**
 * Extract description from a node class or instance.
 *
 * Handles both versioned and regular nodes, with fallback logic.
 *
 * @param nodeClass - The node class or instance
 * @returns The node description or empty object on failure
 */
export function getNodeDescription(
  nodeClass: NodeClass
): INodeTypeBaseDescription | INodeTypeDescription {
  // Try to get description from instance first
  try {
    const instance = instantiateNode(nodeClass);

    if (instance) {
      // For VersionedNodeType, description is the baseDescription
      if (isVersionedNodeInstance(instance)) {
        return instance.description;
      }
      // For regular nodes, description is the full INodeTypeDescription
      return instance.description;
    }
  } catch (e) {
    // Ignore instantiation errors
  }

  // Fallback to static properties
  if (typeof nodeClass === 'object' && 'description' in nodeClass) {
    return nodeClass.description;
  }

  // Last resort: empty description
  return {
    displayName: '',
    name: '',
    group: [],
    description: '',
    version: 1,
    defaults: { name: '', color: '' },
    inputs: [],
    outputs: [],
    properties: []
  } as any; // Type assertion needed for fallback case
}