-
Notifications
You must be signed in to change notification settings - Fork 9k
/
NodeHelpers.ts
1625 lines (1474 loc) · 45.8 KB
/
NodeHelpers.ts
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-use-before-define */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/prefer-nullish-coalescing */
/* eslint-disable prefer-spread */
import get from 'lodash/get';
import isEqual from 'lodash/isEqual';
import uniqBy from 'lodash/uniqBy';
import type {
FieldType,
IContextObject,
IHttpRequestMethods,
INode,
INodeCredentialDescription,
INodeIssueObjectProperty,
INodeIssues,
INodeParameterResourceLocator,
INodeParameters,
INodeProperties,
INodePropertyCollection,
INodePropertyMode,
INodePropertyModeValidation,
INodePropertyOptions,
INodePropertyRegexValidation,
INodeType,
IParameterDependencies,
IRunExecutionData,
IVersionedNodeType,
IWebhookData,
IWorkflowExecuteAdditionalData,
NodeParameterValue,
ResourceMapperValue,
ConnectionTypes,
INodeTypeDescription,
INodeOutputConfiguration,
INodeInputConfiguration,
GenericValue,
} from './Interfaces';
import {
isFilterValue,
isResourceMapperValue,
isValidResourceLocatorParameterValue,
} from './type-guards';
import { deepCopy } from './utils';
import type { Workflow } from './Workflow';
import { validateFilterParameter } from './NodeParameters/FilterParameter';
import { validateFieldType } from './TypeValidation';
import { ApplicationError } from './errors/application.error';
export const cronNodeOptions: INodePropertyCollection[] = [
{
name: 'item',
displayName: 'Item',
values: [
{
displayName: 'Mode',
name: 'mode',
type: 'options',
options: [
{
name: 'Every Minute',
value: 'everyMinute',
},
{
name: 'Every Hour',
value: 'everyHour',
},
{
name: 'Every Day',
value: 'everyDay',
},
{
name: 'Every Week',
value: 'everyWeek',
},
{
name: 'Every Month',
value: 'everyMonth',
},
{
name: 'Every X',
value: 'everyX',
},
{
name: 'Custom',
value: 'custom',
},
],
default: 'everyDay',
description: 'How often to trigger.',
},
{
displayName: 'Hour',
name: 'hour',
type: 'number',
typeOptions: {
minValue: 0,
maxValue: 23,
},
displayOptions: {
hide: {
mode: ['custom', 'everyHour', 'everyMinute', 'everyX'],
},
},
default: 14,
description: 'The hour of the day to trigger (24h format)',
},
{
displayName: 'Minute',
name: 'minute',
type: 'number',
typeOptions: {
minValue: 0,
maxValue: 59,
},
displayOptions: {
hide: {
mode: ['custom', 'everyMinute', 'everyX'],
},
},
default: 0,
description: 'The minute of the day to trigger',
},
{
displayName: 'Day of Month',
name: 'dayOfMonth',
type: 'number',
displayOptions: {
show: {
mode: ['everyMonth'],
},
},
typeOptions: {
minValue: 1,
maxValue: 31,
},
default: 1,
description: 'The day of the month to trigger',
},
{
displayName: 'Weekday',
name: 'weekday',
type: 'options',
displayOptions: {
show: {
mode: ['everyWeek'],
},
},
options: [
{
name: 'Monday',
value: '1',
},
{
name: 'Tuesday',
value: '2',
},
{
name: 'Wednesday',
value: '3',
},
{
name: 'Thursday',
value: '4',
},
{
name: 'Friday',
value: '5',
},
{
name: 'Saturday',
value: '6',
},
{
name: 'Sunday',
value: '0',
},
],
default: '1',
description: 'The weekday to trigger',
},
{
displayName: 'Cron Expression',
name: 'cronExpression',
type: 'string',
displayOptions: {
show: {
mode: ['custom'],
},
},
default: '* * * * * *',
description:
'Use custom cron expression. Values and ranges as follows:<ul><li>Seconds: 0-59</li><li>Minutes: 0 - 59</li><li>Hours: 0 - 23</li><li>Day of Month: 1 - 31</li><li>Months: 0 - 11 (Jan - Dec)</li><li>Day of Week: 0 - 6 (Sun - Sat)</li></ul>',
},
{
displayName: 'Value',
name: 'value',
type: 'number',
typeOptions: {
minValue: 0,
maxValue: 1000,
},
displayOptions: {
show: {
mode: ['everyX'],
},
},
default: 2,
description: 'All how many X minutes/hours it should trigger',
},
{
displayName: 'Unit',
name: 'unit',
type: 'options',
displayOptions: {
show: {
mode: ['everyX'],
},
},
options: [
{
name: 'Minutes',
value: 'minutes',
},
{
name: 'Hours',
value: 'hours',
},
],
default: 'hours',
description: 'If it should trigger all X minutes or hours',
},
],
},
];
const commonPollingParameters: INodeProperties[] = [
{
displayName: 'Poll Times',
name: 'pollTimes',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
multipleValueButtonText: 'Add Poll Time',
},
default: { item: [{ mode: 'everyMinute' }] },
description: 'Time at which polling should occur',
placeholder: 'Add Poll Time',
options: cronNodeOptions,
},
];
const commonCORSParameters: INodeProperties[] = [
{
displayName: 'Allowed Origins (CORS)',
name: 'allowedOrigins',
type: 'string',
default: '*',
description: 'The origin(s) to allow cross-origin non-preflight requests from in a browser',
},
];
/**
* Apply special parameters which should be added to nodeTypes depending on their type or configuration
*/
export function applySpecialNodeParameters(nodeType: INodeType): void {
const { properties, polling, supportsCORS } = nodeType.description;
if (polling) {
properties.unshift(...commonPollingParameters);
}
if (nodeType.webhook && supportsCORS) {
const optionsProperty = properties.find(({ name }) => name === 'options');
if (optionsProperty) optionsProperty.options!.push(...commonCORSParameters);
else properties.push(...commonCORSParameters);
}
}
/**
* Returns if the parameter should be displayed or not
*
* @param {INodeParameters} nodeValues The data on the node which decides if the parameter
* should be displayed
* @param {(INodeProperties | INodeCredentialDescription)} parameter The parameter to check if it should be displayed
* @param {INodeParameters} [nodeValuesRoot] The root node-parameter-data
*/
export function displayParameter(
nodeValues: INodeParameters,
parameter: INodeProperties | INodeCredentialDescription,
node: INode | null, // Allow null as it does also get used by credentials and they do not have versioning yet
nodeValuesRoot?: INodeParameters,
) {
if (!parameter.displayOptions) {
return true;
}
nodeValuesRoot = nodeValuesRoot || nodeValues;
let value;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const values: any[] = [];
if (parameter.displayOptions.show) {
// All the defined rules have to match to display parameter
for (const propertyName of Object.keys(parameter.displayOptions.show)) {
if (propertyName.charAt(0) === '/') {
// Get the value from the root of the node
value = get(nodeValuesRoot, propertyName.slice(1));
} else if (propertyName === '@version') {
value = node?.typeVersion || 0;
} else {
// Get the value from current level
value = get(nodeValues, propertyName);
}
if (value && typeof value === 'object' && '__rl' in value && value.__rl) {
value = value.value;
}
values.length = 0;
if (!Array.isArray(value)) {
values.push(value);
} else {
values.push.apply(values, value);
}
if (values.some((v) => typeof v === 'string' && v.charAt(0) === '=')) {
return true;
}
if (
values.length === 0 ||
!parameter.displayOptions.show[propertyName]!.some((v) => values.includes(v))
) {
return false;
}
}
}
if (parameter.displayOptions.hide) {
// Any of the defined hide rules have to match to hide the parameter
for (const propertyName of Object.keys(parameter.displayOptions.hide)) {
if (propertyName.charAt(0) === '/') {
// Get the value from the root of the node
value = get(nodeValuesRoot, propertyName.slice(1));
} else if (propertyName === '@version') {
value = node?.typeVersion || 0;
} else {
// Get the value from current level
value = get(nodeValues, propertyName);
}
if (value && typeof value === 'object' && '__rl' in value && value.__rl) {
value = value.value;
}
values.length = 0;
if (!Array.isArray(value)) {
values.push(value);
} else {
values.push.apply(values, value);
}
if (
values.length !== 0 &&
parameter.displayOptions.hide[propertyName]!.some((v) => values.includes(v))
) {
return false;
}
}
}
return true;
}
/**
* Returns if the given parameter should be displayed or not considering the path
* to the properties
*
* @param {INodeParameters} nodeValues The data on the node which decides if the parameter
* should be displayed
* @param {(INodeProperties | INodeCredentialDescription)} parameter The parameter to check if it should be displayed
* @param {string} path The path to the property
*/
export function displayParameterPath(
nodeValues: INodeParameters,
parameter: INodeProperties | INodeCredentialDescription,
path: string,
node: INode | null,
) {
let resolvedNodeValues = nodeValues;
if (path !== '') {
resolvedNodeValues = get(nodeValues, path) as INodeParameters;
}
// Get the root parameter data
let nodeValuesRoot = nodeValues;
if (path && path.split('.').indexOf('parameters') === 0) {
nodeValuesRoot = get(nodeValues, 'parameters') as INodeParameters;
}
return displayParameter(resolvedNodeValues, parameter, node, nodeValuesRoot);
}
/**
* Returns the context data
*
* @param {IRunExecutionData} runExecutionData The run execution data
* @param {string} type The data type. "node"/"flow"
* @param {INode} [node] If type "node" is set the node to return the context of has to be supplied
*/
export function getContext(
runExecutionData: IRunExecutionData,
type: string,
node?: INode,
): IContextObject {
if (runExecutionData.executionData === undefined) {
// TODO: Should not happen leave it for test now
throw new ApplicationError('`executionData` is not initialized');
}
let key: string;
if (type === 'flow') {
key = 'flow';
} else if (type === 'node') {
if (node === undefined) {
// @TODO: What does this mean?
throw new ApplicationError(
'The request data of context type "node" the node parameter has to be set!',
);
}
key = `node:${node.name}`;
} else {
throw new ApplicationError('Unknown context type. Only `flow` and `node` are supported.', {
extra: { contextType: type },
});
}
if (runExecutionData.executionData.contextData[key] === undefined) {
runExecutionData.executionData.contextData[key] = {};
}
return runExecutionData.executionData.contextData[key];
}
/**
* Returns which parameters are dependent on which
*
*/
function getParameterDependencies(nodePropertiesArray: INodeProperties[]): IParameterDependencies {
const dependencies: IParameterDependencies = {};
for (const nodeProperties of nodePropertiesArray) {
const { name, displayOptions } = nodeProperties;
if (!dependencies[name]) {
dependencies[name] = [];
}
if (!displayOptions) {
// Does not have any dependencies
continue;
}
for (const displayRule of Object.values(displayOptions)) {
for (const parameterName of Object.keys(displayRule)) {
if (!dependencies[name].includes(parameterName)) {
if (parameterName.charAt(0) === '@') {
// Is a special parameter so can be skipped
continue;
}
dependencies[name].push(parameterName);
}
}
}
}
return dependencies;
}
/**
* Returns in which order the parameters should be resolved
* to have the parameters available they depend on
*
*/
export function getParameterResolveOrder(
nodePropertiesArray: INodeProperties[],
parameterDependencies: IParameterDependencies,
): number[] {
const executionOrder: number[] = [];
const indexToResolve = Array.from({ length: nodePropertiesArray.length }, (v, k) => k);
const resolvedParameters: string[] = [];
let index: number;
let property: INodeProperties;
let lastIndexLength = indexToResolve.length;
let lastIndexReduction = -1;
let iterations = 0;
while (indexToResolve.length !== 0) {
iterations += 1;
index = indexToResolve.shift() as number;
property = nodePropertiesArray[index];
if (parameterDependencies[property.name].length === 0) {
// Does not have any dependencies so simply add
executionOrder.push(index);
resolvedParameters.push(property.name);
continue;
}
// Parameter has dependencies
for (const dependency of parameterDependencies[property.name]) {
if (!resolvedParameters.includes(dependency)) {
if (dependency.charAt(0) === '/') {
// Assume that root level dependencies are resolved
continue;
}
// Dependencies for that parameter are still missing so
// try to add again later
indexToResolve.push(index);
continue;
}
}
// All dependencies got found so add
executionOrder.push(index);
resolvedParameters.push(property.name);
if (indexToResolve.length < lastIndexLength) {
lastIndexReduction = iterations;
}
if (iterations > lastIndexReduction + nodePropertiesArray.length) {
throw new ApplicationError(
'Could not resolve parameter dependencies. Max iterations reached! Hint: If `displayOptions` are specified in any child parameter of a parent `collection` or `fixedCollection`, remove the `displayOptions` from the child parameter.',
);
}
lastIndexLength = indexToResolve.length;
}
return executionOrder;
}
/**
* Returns the node parameter values. Depending on the settings it either just returns the none
* default values or it applies all the default values.
*
* @param {INodeProperties[]} nodePropertiesArray The properties which exist and their settings
* @param {INodeParameters} nodeValues The node parameter data
* @param {boolean} returnDefaults If default values get added or only none default values returned
* @param {boolean} returnNoneDisplayed If also values which should not be displayed should be returned
* @param {boolean} [onlySimpleTypes=false] If only simple types should be resolved
* @param {boolean} [dataIsResolved=false] If nodeValues are already fully resolved (so that all default values got added already)
* @param {INodeParameters} [nodeValuesRoot] The root node-parameter-data
*/
export function getNodeParameters(
nodePropertiesArray: INodeProperties[],
nodeValues: INodeParameters | null,
returnDefaults: boolean,
returnNoneDisplayed: boolean,
node: INode | null,
onlySimpleTypes = false,
dataIsResolved = false,
nodeValuesRoot?: INodeParameters,
parentType?: string,
parameterDependencies?: IParameterDependencies,
): INodeParameters | null {
if (parameterDependencies === undefined) {
parameterDependencies = getParameterDependencies(nodePropertiesArray);
}
// Get the parameter names which get used multiple times as for this
// ones we have to always check which ones get displayed and which ones not
const duplicateParameterNames: string[] = [];
const parameterNames: string[] = [];
for (const nodeProperties of nodePropertiesArray) {
if (parameterNames.includes(nodeProperties.name)) {
if (!duplicateParameterNames.includes(nodeProperties.name)) {
duplicateParameterNames.push(nodeProperties.name);
}
} else {
parameterNames.push(nodeProperties.name);
}
}
const nodeParameters: INodeParameters = {};
const nodeParametersFull: INodeParameters = {};
let nodeValuesDisplayCheck = nodeParametersFull;
if (!dataIsResolved && !returnNoneDisplayed) {
nodeValuesDisplayCheck = getNodeParameters(
nodePropertiesArray,
nodeValues,
true,
true,
node,
true,
true,
nodeValuesRoot,
parentType,
parameterDependencies,
) as INodeParameters;
}
nodeValuesRoot = nodeValuesRoot || nodeValuesDisplayCheck;
// Go through the parameters in order of their dependencies
const parameterIterationOrderIndex = getParameterResolveOrder(
nodePropertiesArray,
parameterDependencies,
);
for (const parameterIndex of parameterIterationOrderIndex) {
const nodeProperties = nodePropertiesArray[parameterIndex];
if (
!nodeValues ||
(nodeValues[nodeProperties.name] === undefined &&
(!returnDefaults || parentType === 'collection'))
) {
// The value is not defined so go to the next
continue;
}
if (
!returnNoneDisplayed &&
!displayParameter(nodeValuesDisplayCheck, nodeProperties, node, nodeValuesRoot)
) {
if (!returnNoneDisplayed || !returnDefaults) {
continue;
}
}
if (!['collection', 'fixedCollection'].includes(nodeProperties.type)) {
// Is a simple property so can be set as it is
if (duplicateParameterNames.includes(nodeProperties.name)) {
if (!displayParameter(nodeValuesDisplayCheck, nodeProperties, node, nodeValuesRoot)) {
continue;
}
}
if (returnDefaults) {
// Set also when it has the default value
if (['boolean', 'number', 'options'].includes(nodeProperties.type)) {
// Boolean, numbers and options are special as false and 0 are valid values
// and should not be replaced with default value
nodeParameters[nodeProperties.name] =
nodeValues[nodeProperties.name] !== undefined
? nodeValues[nodeProperties.name]
: nodeProperties.default;
} else if (
nodeProperties.type === 'resourceLocator' &&
typeof nodeProperties.default === 'object'
) {
nodeParameters[nodeProperties.name] =
nodeValues[nodeProperties.name] !== undefined
? nodeValues[nodeProperties.name]
: { __rl: true, ...nodeProperties.default };
} else {
nodeParameters[nodeProperties.name] =
nodeValues[nodeProperties.name] ?? nodeProperties.default;
}
nodeParametersFull[nodeProperties.name] = nodeParameters[nodeProperties.name];
} else if (
(nodeValues[nodeProperties.name] !== nodeProperties.default &&
typeof nodeValues[nodeProperties.name] !== 'object') ||
(typeof nodeValues[nodeProperties.name] === 'object' &&
!isEqual(nodeValues[nodeProperties.name], nodeProperties.default)) ||
(nodeValues[nodeProperties.name] !== undefined && parentType === 'collection')
) {
// Set only if it is different to the default value
nodeParameters[nodeProperties.name] = nodeValues[nodeProperties.name];
nodeParametersFull[nodeProperties.name] = nodeParameters[nodeProperties.name];
continue;
}
}
if (onlySimpleTypes) {
// It is only supposed to resolve the simple types. So continue.
continue;
}
// Is a complex property so check lower levels
let tempValue: INodeParameters | null;
if (nodeProperties.type === 'collection') {
// Is collection
if (
nodeProperties.typeOptions !== undefined &&
nodeProperties.typeOptions.multipleValues === true
) {
// Multiple can be set so will be an array
// Return directly the values like they are
if (nodeValues[nodeProperties.name] !== undefined) {
nodeParameters[nodeProperties.name] = nodeValues[nodeProperties.name];
} else if (returnDefaults) {
// Does not have values defined but defaults should be returned
if (Array.isArray(nodeProperties.default)) {
nodeParameters[nodeProperties.name] = deepCopy(nodeProperties.default);
} else {
// As it is probably wrong for many nodes, do we keep on returning an empty array if
// anything else than an array is set as default
nodeParameters[nodeProperties.name] = [];
}
}
nodeParametersFull[nodeProperties.name] = nodeParameters[nodeProperties.name];
} else if (nodeValues[nodeProperties.name] !== undefined) {
// Has values defined so get them
const tempNodeParameters = getNodeParameters(
nodeProperties.options as INodeProperties[],
nodeValues[nodeProperties.name] as INodeParameters,
returnDefaults,
returnNoneDisplayed,
node,
false,
false,
nodeValuesRoot,
nodeProperties.type,
);
if (tempNodeParameters !== null) {
nodeParameters[nodeProperties.name] = tempNodeParameters;
nodeParametersFull[nodeProperties.name] = nodeParameters[nodeProperties.name];
}
} else if (returnDefaults) {
// Does not have values defined but defaults should be returned
nodeParameters[nodeProperties.name] = deepCopy(nodeProperties.default);
nodeParametersFull[nodeProperties.name] = nodeParameters[nodeProperties.name];
}
} else if (nodeProperties.type === 'fixedCollection') {
// Is fixedCollection
const collectionValues: INodeParameters = {};
let tempNodeParameters: INodeParameters;
let tempNodePropertiesArray: INodeProperties[];
let nodePropertyOptions: INodePropertyCollection | undefined;
let propertyValues = nodeValues[nodeProperties.name];
if (returnDefaults) {
if (propertyValues === undefined) {
propertyValues = deepCopy(nodeProperties.default);
}
}
if (
!returnDefaults &&
nodeProperties.typeOptions?.multipleValues === false &&
propertyValues &&
Object.keys(propertyValues).length === 0
) {
// For fixedCollections, which only allow one value, it is important to still return
// the empty object which indicates that a value got added, even if it does not have
// anything set. If that is not done, the value would get lost.
return nodeValues;
}
// Iterate over all collections
for (const itemName of Object.keys(propertyValues || {})) {
if (
nodeProperties.typeOptions !== undefined &&
nodeProperties.typeOptions.multipleValues === true
) {
// Multiple can be set so will be an array
const tempArrayValue: INodeParameters[] = [];
// Iterate over all items as it contains multiple ones
for (const nodeValue of (propertyValues as INodeParameters)[
itemName
] as INodeParameters[]) {
nodePropertyOptions = nodeProperties.options!.find(
// eslint-disable-next-line @typescript-eslint/no-shadow
(nodePropertyOptions) => nodePropertyOptions.name === itemName,
) as INodePropertyCollection;
if (nodePropertyOptions === undefined) {
throw new ApplicationError('Could not find property option', {
extra: { propertyOption: itemName, property: nodeProperties.name },
});
}
tempNodePropertiesArray = nodePropertyOptions.values!;
tempValue = getNodeParameters(
tempNodePropertiesArray,
nodeValue,
returnDefaults,
returnNoneDisplayed,
node,
false,
false,
nodeValuesRoot,
nodeProperties.type,
);
if (tempValue !== null) {
tempArrayValue.push(tempValue);
}
}
collectionValues[itemName] = tempArrayValue;
} else {
// Only one can be set so is an object of objects
tempNodeParameters = {};
// Get the options of the current item
// eslint-disable-next-line @typescript-eslint/no-shadow
const nodePropertyOptions = nodeProperties.options!.find(
(data) => data.name === itemName,
);
if (nodePropertyOptions !== undefined) {
tempNodePropertiesArray = (nodePropertyOptions as INodePropertyCollection).values!;
tempValue = getNodeParameters(
tempNodePropertiesArray,
(nodeValues[nodeProperties.name] as INodeParameters)[itemName] as INodeParameters,
returnDefaults,
returnNoneDisplayed,
node,
false,
false,
nodeValuesRoot,
nodeProperties.type,
);
if (tempValue !== null) {
Object.assign(tempNodeParameters, tempValue);
}
}
if (Object.keys(tempNodeParameters).length !== 0) {
collectionValues[itemName] = tempNodeParameters;
}
}
}
if (
!returnDefaults &&
nodeProperties.typeOptions?.multipleValues === false &&
collectionValues &&
Object.keys(collectionValues).length === 0 &&
propertyValues &&
propertyValues?.constructor.name === 'Object' &&
Object.keys(propertyValues).length !== 0
) {
// For fixedCollections, which only allow one value, it is important to still return
// the object with an empty collection property which indicates that a value got added
// which contains all default values. If that is not done, the value would get lost.
const returnValue = {} as INodeParameters;
Object.keys(propertyValues || {}).forEach((value) => {
returnValue[value] = {};
});
nodeParameters[nodeProperties.name] = returnValue;
}
if (Object.keys(collectionValues).length !== 0 || returnDefaults) {
// Set only if value got found
if (returnDefaults) {
// Set also when it has the default value
if (collectionValues === undefined) {
nodeParameters[nodeProperties.name] = deepCopy(nodeProperties.default);
} else {
nodeParameters[nodeProperties.name] = collectionValues;
}
nodeParametersFull[nodeProperties.name] = nodeParameters[nodeProperties.name];
} else if (collectionValues !== nodeProperties.default) {
// Set only if values got found and it is not the default
nodeParameters[nodeProperties.name] = collectionValues;
nodeParametersFull[nodeProperties.name] = nodeParameters[nodeProperties.name];
}
}
}
}
return nodeParameters;
}
/**
* Returns all the webhooks which should be created for the give node
*/
export function getNodeWebhooks(
workflow: Workflow,
node: INode,
additionalData: IWorkflowExecuteAdditionalData,
ignoreRestartWebhooks = false,
): IWebhookData[] {
if (node.disabled === true) {
// Node is disabled so webhooks will also not be enabled
return [];
}
const nodeType = workflow.nodeTypes.getByNameAndVersion(node.type, node.typeVersion);
if (nodeType.description.webhooks === undefined) {
// Node does not have any webhooks so return
return [];
}
const workflowId = workflow.id || '__UNSAVED__';
const mode = 'internal';
const returnData: IWebhookData[] = [];
for (const webhookDescription of nodeType.description.webhooks) {
if (ignoreRestartWebhooks && webhookDescription.restartWebhook === true) {
continue;
}
let nodeWebhookPath = workflow.expression.getSimpleParameterValue(
node,
webhookDescription.path,
mode,
{},
);
if (nodeWebhookPath === undefined) {
// TODO: Use a proper logger
console.error(
`No webhook path could be found for node "${node.name}" in workflow "${workflowId}".`,
);
continue;
}
nodeWebhookPath = nodeWebhookPath.toString();
if (nodeWebhookPath.startsWith('/')) {
nodeWebhookPath = nodeWebhookPath.slice(1);
}
if (nodeWebhookPath.endsWith('/')) {
nodeWebhookPath = nodeWebhookPath.slice(0, -1);
}
const isFullPath: boolean = workflow.expression.getSimpleParameterValue(
node,
webhookDescription.isFullPath,
'internal',
{},
undefined,
false,
) as boolean;
const restartWebhook: boolean = workflow.expression.getSimpleParameterValue(
node,
webhookDescription.restartWebhook,
'internal',
{},
undefined,
false,
) as boolean;
const path = getNodeWebhookPath(workflowId, node, nodeWebhookPath, isFullPath, restartWebhook);
const httpMethod = workflow.expression.getSimpleParameterValue(
node,
webhookDescription.httpMethod,
mode,
{},
undefined,
'GET',
);
if (httpMethod === undefined) {
// TODO: Use a proper logger
console.error(
`The webhook "${path}" for node "${node.name}" in workflow "${workflowId}" could not be added because the httpMethod is not defined.`,
);
continue;
}
let webhookId: string | undefined;
if ((path.startsWith(':') || path.includes('/:')) && node.webhookId) {
webhookId = node.webhookId;
}
returnData.push({
httpMethod: httpMethod.toString() as IHttpRequestMethods,
node: node.name,
path,
webhookDescription,
workflowId,
workflowExecuteAdditionalData: additionalData,
webhookId,
});
}
return returnData;
}
/**
* Returns the webhook path
*
*/
export function getNodeWebhookPath(
workflowId: string,
node: INode,
path: string,
isFullPath?: boolean,
restartWebhook?: boolean,
): string {
let webhookPath = '';