fix(core): Fix resolve RL values in expressions (#4173)

* update interface

* update expression resolving

* 🔥 remove ExtractValue functions

* add flags

* update resolving

* update expr

* fix for list mode

* clean up

* Fix up

* update guard

* fix bug with switching

* update to handle expr referencing

* fix legacy expression

* fix when switching

* update spacing

Co-authored-by: Valya Bullions <valya@n8n.io>
This commit is contained in:
Mutasem Aldmour
2022-09-22 19:04:26 +02:00
committed by GitHub
parent d01f7d4d93
commit 469c391fee
12 changed files with 85 additions and 361 deletions

View File

@@ -1,183 +0,0 @@
import {
INode,
INodeParameters,
INodeProperties,
INodePropertyCollection,
INodePropertyOptions,
INodeType,
NodeOperationError,
NodeParameterValueType,
NodeHelpers,
LoggerProxy,
} from 'n8n-workflow';
function findPropertyFromParameterName(
parameterName: string,
nodeType: INodeType,
node: INode,
nodeParameters: INodeParameters,
): INodePropertyOptions | INodeProperties | INodePropertyCollection {
let property: INodePropertyOptions | INodeProperties | INodePropertyCollection | undefined;
const paramParts = parameterName.split('.');
let currentParamPath = '';
const findProp = (
name: string,
options: Array<INodePropertyOptions | INodeProperties | INodePropertyCollection>,
): INodePropertyOptions | INodeProperties | INodePropertyCollection | undefined => {
return options.find(
(i) =>
i.name === name &&
NodeHelpers.displayParameterPath(nodeParameters, i, currentParamPath, node),
);
};
// eslint-disable-next-line no-restricted-syntax
for (const p of paramParts) {
const param = p.split('[')[0];
if (!property) {
property = findProp(param, nodeType.description.properties);
} else if ('options' in property && property.options) {
property = findProp(param, property.options);
currentParamPath += `.${param}`;
} else if ('values' in property) {
property = findProp(param, property.values);
currentParamPath += `.${param}`;
} else {
throw new Error(`Couldn't not find property "${parameterName}"`);
}
if (!property) {
throw new Error(`Couldn't not find property "${parameterName}"`);
}
}
if (!property) {
throw new Error(`Couldn't not find property "${parameterName}"`);
}
return property;
}
function executeRegexExtractValue(
value: string,
regex: RegExp,
parameterName: string,
parameterDisplayName: string,
): NodeParameterValueType | object {
const extracted = regex.exec(value);
if (!extracted) {
throw new Error(
`ERROR: ${parameterDisplayName} parameter's value is invalid. This is likely because the URL entered is incorrect`,
);
}
if (extracted.length < 2 || extracted.length > 2) {
throw new Error(
`Property "${parameterName}" has an invalid extractValue regex "${regex.source}". extractValue expects exactly one group to be returned.`,
);
}
return extracted[1];
}
function extractValueRLC(
value: NodeParameterValueType | object,
property: INodeProperties,
parameterName: string,
): NodeParameterValueType | object {
// Not an RLC value
if (typeof value !== 'object' || !value || !('mode' in value) || !('value' in value)) {
return value;
}
const modeProp = (property.modes ?? []).find((i) => i.name === value.mode);
if (!modeProp) {
return value.value;
}
if (!('extractValue' in modeProp) || !modeProp.extractValue) {
return value.value;
}
if (typeof value.value !== 'string') {
let typeName: string | undefined = value.value?.constructor.name;
if (value.value === null) {
typeName = 'null';
} else if (typeName === undefined) {
typeName = 'undefined';
}
LoggerProxy.error(
`Only strings can be passed to extractValue. Parameter "${parameterName}" passed "${typeName}"`,
);
throw new Error(
`ERROR: ${property.displayName} parameter's value is invalid. Please enter a valid ${modeProp.displayName}.`,
);
}
if (modeProp.extractValue.type !== 'regex') {
throw new Error(
`Property "${parameterName}" has an unknown extractValue type "${
modeProp.extractValue.type as string
}"`,
);
}
const regex = new RegExp(modeProp.extractValue.regex);
return executeRegexExtractValue(value.value, regex, parameterName, property.displayName);
}
function extractValueOther(
value: NodeParameterValueType | object,
property: INodeProperties | INodePropertyCollection,
parameterName: string,
): NodeParameterValueType | object {
if (!('extractValue' in property) || !property.extractValue) {
return value;
}
if (typeof value !== 'string') {
let typeName: string | undefined = value?.constructor.name;
if (value === null) {
typeName = 'null';
} else if (typeName === undefined) {
typeName = 'undefined';
}
LoggerProxy.error(
`Only strings can be passed to extractValue. Parameter "${parameterName}" passed "${typeName}"`,
);
throw new Error(
`ERROR: ${property.displayName} parameter's value is invalid. Please enter a valid value.`,
);
}
if (property.extractValue.type !== 'regex') {
throw new Error(
`Property "${parameterName}" has an unknown extractValue type "${
property.extractValue.type as string
}"`,
);
}
const regex = new RegExp(property.extractValue.regex);
return executeRegexExtractValue(value, regex, parameterName, property.displayName);
}
export function extractValue(
value: NodeParameterValueType | object,
parameterName: string,
node: INode,
nodeType: INodeType,
): NodeParameterValueType | object {
let property: INodePropertyOptions | INodeProperties | INodePropertyCollection;
try {
property = findPropertyFromParameterName(parameterName, nodeType, node, node.parameters);
// Definitely doesn't have value extractor
if (!('type' in property)) {
return value;
}
if (property.type === 'resourceLocator') {
return extractValueRLC(value, property, parameterName);
}
return extractValueOther(value, property, parameterName);
} catch (error) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
throw new NodeOperationError(node, error);
}
}

View File

@@ -59,7 +59,6 @@ import {
LoggerProxy as Logger,
IExecuteData,
OAuth2GrantType,
IGetNodeParameterOptions,
NodeParameterValueType,
NodeExecutionWithMetadata,
IPairedItemData,
@@ -100,7 +99,6 @@ import {
IWorkflowSettings,
PLACEHOLDER_EMPTY_EXECUTION_ID,
} from '.';
import { extractValue } from './ExtractValue';
axios.defaults.timeout = 300000;
// Prevent axios from adding x-form-www-urlencoded headers by default
@@ -1727,7 +1725,6 @@ export function getNodeParameter(
additionalKeys: IWorkflowDataProxyAdditionalKeys,
executeData?: IExecuteData,
fallbackValue?: any,
options?: IGetNodeParameterOptions,
): NodeParameterValueType | object {
const nodeType = workflow.nodeTypes.getByNameAndVersion(node.type, node.typeVersion);
if (nodeType === undefined) {
@@ -1762,11 +1759,6 @@ export function getNodeParameter(
throw e;
}
// This is outside the try/catch because it throws errors with proper messages
if (options?.extractValue) {
returnData = extractValue(returnData, parameterName, node, nodeType);
}
return returnData;
}
@@ -1939,7 +1931,6 @@ export function getExecutePollFunctions(
getNodeParameter: (
parameterName: string,
fallbackValue?: any,
options?: IGetNodeParameterOptions,
): NodeParameterValueType | object => {
const runExecutionData: IRunExecutionData | null = null;
const itemIndex = 0;
@@ -1959,7 +1950,6 @@ export function getExecutePollFunctions(
getAdditionalKeys(additionalData),
undefined,
fallbackValue,
options,
);
},
getRestApiUrl: (): string => {
@@ -2094,7 +2084,6 @@ export function getExecuteTriggerFunctions(
getNodeParameter: (
parameterName: string,
fallbackValue?: any,
options?: IGetNodeParameterOptions,
): NodeParameterValueType | object => {
const runExecutionData: IRunExecutionData | null = null;
const itemIndex = 0;
@@ -2114,7 +2103,6 @@ export function getExecuteTriggerFunctions(
getAdditionalKeys(additionalData),
undefined,
fallbackValue,
options,
);
},
getRestApiUrl: (): string => {
@@ -2314,7 +2302,6 @@ export function getExecuteFunctions(
parameterName: string,
itemIndex: number,
fallbackValue?: any,
options?: IGetNodeParameterOptions,
): NodeParameterValueType | object => {
return getNodeParameter(
workflow,
@@ -2329,7 +2316,6 @@ export function getExecuteFunctions(
getAdditionalKeys(additionalData),
executeData,
fallbackValue,
options,
);
},
getMode: (): WorkflowExecuteMode => {
@@ -2589,7 +2575,6 @@ export function getExecuteSingleFunctions(
getNodeParameter: (
parameterName: string,
fallbackValue?: any,
options?: IGetNodeParameterOptions,
): NodeParameterValueType | object => {
return getNodeParameter(
workflow,
@@ -2604,7 +2589,6 @@ export function getExecuteSingleFunctions(
getAdditionalKeys(additionalData),
executeData,
fallbackValue,
options,
);
},
getWorkflow: () => {
@@ -2758,7 +2742,6 @@ export function getLoadOptionsFunctions(
getNodeParameter: (
parameterName: string,
fallbackValue?: any,
options?: IGetNodeParameterOptions,
): NodeParameterValueType | object => {
const runExecutionData: IRunExecutionData | null = null;
const itemIndex = 0;
@@ -2778,7 +2761,6 @@ export function getLoadOptionsFunctions(
getAdditionalKeys(additionalData),
undefined,
fallbackValue,
options,
);
},
getTimezone: (): string => {
@@ -2886,7 +2868,6 @@ export function getExecuteHookFunctions(
getNodeParameter: (
parameterName: string,
fallbackValue?: any,
options?: IGetNodeParameterOptions,
): NodeParameterValueType | object => {
const runExecutionData: IRunExecutionData | null = null;
const itemIndex = 0;
@@ -2906,7 +2887,6 @@ export function getExecuteHookFunctions(
getAdditionalKeys(additionalData),
undefined,
fallbackValue,
options,
);
},
getNodeWebhookUrl: (name: string): string | undefined => {
@@ -3046,7 +3026,6 @@ export function getExecuteWebhookFunctions(
getNodeParameter: (
parameterName: string,
fallbackValue?: any,
options?: IGetNodeParameterOptions,
): NodeParameterValueType | object => {
const runExecutionData: IRunExecutionData | null = null;
const itemIndex = 0;
@@ -3066,7 +3045,6 @@ export function getExecuteWebhookFunctions(
getAdditionalKeys(additionalData),
undefined,
fallbackValue,
options,
);
},
getParamsData(): object {