2
0

feat(engine): Improve variables in executed codes

This commit is contained in:
Baptiste Arnaud
2022-03-31 16:41:18 +02:00
parent 82f7bf0ed6
commit db10f1ee89
11 changed files with 155 additions and 60 deletions

View File

@ -247,9 +247,8 @@ const executeWebhook = async (
if (!varMapping?.bodyPath || !varMapping.variableId) return newVariables
const existingVariable = variables.find(byId(varMapping.variableId))
if (!existingVariable) return newVariables
const value = Function(
`return (${JSON.stringify(data)}).${varMapping?.bodyPath}`
)()
const func = Function('data', `return data.${varMapping?.bodyPath}`)
const value = func(JSON.stringify(data))
updateVariableValue(existingVariable?.id, value)
return [...newVariables, { ...existingVariable, value }]
}, [])

View File

@ -63,9 +63,8 @@ const executeSetVariable = (
): EdgeId | undefined => {
if (!step.options?.variableId || !step.options.expressionToEvaluate)
return step.outgoingEdgeId
const expression = step.options.expressionToEvaluate
const evaluatedExpression = evaluateExpression(
parseVariables(variables)(expression)
const evaluatedExpression = evaluateExpression(variables)(
step.options.expressionToEvaluate
)
const existingVariable = variables.find(byId(step.options.variableId))
if (!existingVariable) return step.outgoingEdgeId
@ -132,7 +131,11 @@ const executeCode = async (
{ typebot: { variables } }: LogicContext
) => {
if (!step.options.content) return
await Function(parseVariables(variables)(step.options.content))()
const func = Function(
...variables.map((v) => v.id),
parseVariables(variables, { fieldToParse: 'id' })(step.options.content)
)
await func(...variables.map((v) => v.value))
return step.outgoingEdgeId
}

View File

@ -5,22 +5,33 @@ export const stringContainsVariable = (str: string): boolean =>
/\{\{(.*?)\}\}/g.test(str)
export const parseVariables =
(variables: Variable[]) =>
(text?: string): string => {
(
variables: Variable[],
options: { fieldToParse: 'value' | 'id' } = { fieldToParse: 'value' }
) =>
(text: string | undefined): string => {
if (!text || text === '') return ''
return text.replace(/\{\{(.*?)\}\}/g, (_, fullVariableString) => {
const matchedVarName = fullVariableString.replace(/{{|}}/g, '')
const variable = variables.find((v) => {
return matchedVarName === v.name && isDefined(v.value)
})
if (!variable) return ''
return (
variables.find((v) => {
return matchedVarName === v.name && isDefined(v.value)
})?.value ?? ''
(options.fieldToParse === 'value' ? variable.value : variable.id) || ''
)
})
}
export const evaluateExpression = (str: string) => {
export const evaluateExpression = (variables: Variable[]) => (str: string) => {
try {
const evaluatedResult = Function('return ' + str)()
const func = Function(
...variables.map((v) => v.id),
parseVariables(variables, { fieldToParse: 'id' })(
str.includes('return ') ? str : `return ${str}`
)
)
const evaluatedResult = func(...variables.map((v) => v.value))
return isNotDefined(evaluatedResult) ? '' : evaluatedResult.toString()
} catch (err) {
console.log(err)