feat(engine): ✨ Improve variables in executed codes
This commit is contained in:
@ -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 }]
|
||||
}, [])
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user