2023-12-13 05:45:22 -08:00
|
|
|
import set from 'lodash/set';
|
2024-08-16 08:36:15 -07:00
|
|
|
import {
|
|
|
|
ApplicationError,
|
|
|
|
NodeOperationError,
|
|
|
|
type IExecuteFunctions,
|
|
|
|
type INodeExecutionData,
|
|
|
|
type INodeType,
|
|
|
|
type INodeTypeBaseDescription,
|
|
|
|
type INodeTypeDescription,
|
2023-12-13 05:45:22 -08:00
|
|
|
} from 'n8n-workflow';
|
2024-07-11 05:36:39 -07:00
|
|
|
import { ENABLE_LESS_STRICT_TYPE_VALIDATION } from '../../../utils/constants';
|
2024-08-19 09:01:33 -07:00
|
|
|
import { looseTypeValidationProperty } from '../../../utils/descriptions';
|
|
|
|
import { getTypeValidationParameter, getTypeValidationStrictness } from './utils';
|
2023-12-13 05:45:22 -08:00
|
|
|
|
|
|
|
export class IfV2 implements INodeType {
|
|
|
|
description: INodeTypeDescription;
|
|
|
|
|
|
|
|
constructor(baseDescription: INodeTypeBaseDescription) {
|
|
|
|
this.description = {
|
|
|
|
...baseDescription,
|
2024-08-19 09:01:33 -07:00
|
|
|
version: [2, 2.1],
|
2023-12-13 05:45:22 -08:00
|
|
|
defaults: {
|
|
|
|
name: 'If',
|
|
|
|
color: '#408000',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main', 'main'],
|
|
|
|
outputNames: ['true', 'false'],
|
2024-02-06 09:34:34 -08:00
|
|
|
parameterPane: 'wide',
|
2023-12-13 05:45:22 -08:00
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Conditions',
|
|
|
|
name: 'conditions',
|
|
|
|
placeholder: 'Add Condition',
|
|
|
|
type: 'filter',
|
|
|
|
default: {},
|
|
|
|
typeOptions: {
|
|
|
|
filter: {
|
|
|
|
caseSensitive: '={{!$parameter.options.ignoreCase}}',
|
2024-08-19 09:01:33 -07:00
|
|
|
typeValidation: getTypeValidationStrictness(2.1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
...looseTypeValidationProperty,
|
|
|
|
default: false,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
'@version': [{ _cnd: { gte: 2.1 } }],
|
2023-12-13 05:45:22 -08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Options',
|
|
|
|
name: 'options',
|
|
|
|
type: 'collection',
|
|
|
|
placeholder: 'Add option',
|
|
|
|
default: {},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: 'Ignore Case',
|
|
|
|
description: 'Whether to ignore letter case when evaluating conditions',
|
|
|
|
name: 'ignoreCase',
|
|
|
|
type: 'boolean',
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
{
|
2024-08-19 09:01:33 -07:00
|
|
|
...looseTypeValidationProperty,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
'@version': [{ _cnd: { lt: 2.1 } }],
|
|
|
|
},
|
|
|
|
},
|
2023-12-13 05:45:22 -08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const trueItems: INodeExecutionData[] = [];
|
|
|
|
const falseItems: INodeExecutionData[] = [];
|
|
|
|
|
|
|
|
this.getInputData().forEach((item, itemIndex) => {
|
|
|
|
try {
|
|
|
|
const options = this.getNodeParameter('options', itemIndex) as {
|
|
|
|
ignoreCase?: boolean;
|
|
|
|
looseTypeValidation?: boolean;
|
|
|
|
};
|
|
|
|
let pass = false;
|
|
|
|
try {
|
|
|
|
pass = this.getNodeParameter('conditions', itemIndex, false, {
|
|
|
|
extractValue: true,
|
|
|
|
}) as boolean;
|
|
|
|
} catch (error) {
|
2024-08-19 09:01:33 -07:00
|
|
|
if (
|
|
|
|
!getTypeValidationParameter(2.1)(this, itemIndex, options.looseTypeValidation) &&
|
|
|
|
!error.description
|
|
|
|
) {
|
2024-07-11 05:36:39 -07:00
|
|
|
set(error, 'description', ENABLE_LESS_STRICT_TYPE_VALIDATION);
|
2023-12-13 05:45:22 -08:00
|
|
|
}
|
2024-03-07 08:08:01 -08:00
|
|
|
set(error, 'context.itemIndex', itemIndex);
|
|
|
|
set(error, 'node', this.getNode());
|
2023-12-13 05:45:22 -08:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item.pairedItem === undefined) {
|
|
|
|
item.pairedItem = { item: itemIndex };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pass) {
|
|
|
|
trueItems.push(item);
|
|
|
|
} else {
|
|
|
|
falseItems.push(item);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2024-06-19 22:45:00 -07:00
|
|
|
if (this.continueOnFail(error)) {
|
2023-12-13 05:45:22 -08:00
|
|
|
falseItems.push(item);
|
|
|
|
} else {
|
2024-08-16 08:36:15 -07:00
|
|
|
if (error instanceof NodeOperationError) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error instanceof ApplicationError) {
|
|
|
|
set(error, 'context.itemIndex', itemIndex);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new NodeOperationError(this.getNode(), error, {
|
|
|
|
itemIndex,
|
|
|
|
});
|
2023-12-13 05:45:22 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return [trueItems, falseItems];
|
|
|
|
}
|
|
|
|
}
|