2
0

feat(editor): Code step

This commit is contained in:
Baptiste Arnaud
2022-03-07 17:39:57 +01:00
parent b2784f19fd
commit e3e07ddd4d
13 changed files with 239 additions and 16 deletions

View File

@@ -21,8 +21,6 @@ import { sendRequest } from 'utils'
import { sendGaEvent } from '../../lib/gtag'
import { parseVariables, parseVariablesInObject } from './variable'
const safeEval = eval
type IntegrationContext = {
apiHost: string
typebotId: string
@@ -222,7 +220,9 @@ const executeWebhook = async (
})
step.options.responseVariableMapping.forEach((varMapping) => {
if (!varMapping?.bodyPath || !varMapping.variableId) return
const value = safeEval(`(${JSON.stringify(data)}).${varMapping?.bodyPath}`)
const value = Function(
`return (${JSON.stringify(data)}).${varMapping?.bodyPath}`
)()
updateVariableValue(varMapping.variableId, value)
})
return step.outgoingEdgeId

View File

@@ -8,6 +8,7 @@ import {
SetVariableStep,
RedirectStep,
Comparison,
CodeStep,
} from 'models'
import { isDefined, isNotDefined } from 'utils'
import { sanitizeUrl } from './utils'
@@ -27,6 +28,8 @@ export const executeLogic = (
return executeCondition(step, variables)
case LogicStepType.REDIRECT:
return executeRedirect(step, variables)
case LogicStepType.CODE:
return executeCode(step)
}
}
@@ -97,3 +100,9 @@ const executeRedirect = (
)
return step.outgoingEdgeId
}
const executeCode = (step: CodeStep) => {
if (!step.options.content) return
Function(step.options.content)()
return step.outgoingEdgeId
}

View File

@@ -1,8 +1,6 @@
import { Variable } from 'models'
import { isDefined, isNotDefined } from 'utils'
const safeEval = eval
export const stringContainsVariable = (str: string): boolean =>
/\{\{(.*?)\}\}/g.test(str)
@@ -22,7 +20,7 @@ export const parseVariables =
export const evaluateExpression = (str: string) => {
try {
const evaluatedResult = safeEval(str)
const evaluatedResult = Function('return' + str)()
return isNotDefined(evaluatedResult) ? '' : evaluatedResult.toString()
} catch (err) {
console.log(err)

View File

@@ -1,16 +1,24 @@
import { ItemType, StepBase } from '.'
import { ItemBase } from './item'
export type LogicStep = SetVariableStep | ConditionStep | RedirectStep
export type LogicStep =
| SetVariableStep
| ConditionStep
| RedirectStep
| CodeStep
export type LogicStepOptions =
| SetVariableOptions
| RedirectOptions
| CodeOptions
export enum LogicStepType {
SET_VARIABLE = 'Set variable',
CONDITION = 'Condition',
REDIRECT = 'Redirect',
CODE = 'Code',
}
export type LogicStepOptions = SetVariableOptions | RedirectOptions
export type SetVariableStep = StepBase & {
type: LogicStepType.SET_VARIABLE
options: SetVariableOptions
@@ -31,6 +39,11 @@ export type RedirectStep = StepBase & {
options: RedirectOptions
}
export type CodeStep = StepBase & {
type: LogicStepType.CODE
options: CodeOptions
}
export enum LogicalOperator {
OR = 'OR',
AND = 'AND',
@@ -67,6 +80,11 @@ export type RedirectOptions = {
isNewTab: boolean
}
export type CodeOptions = {
name: string
content?: string
}
export const defaultSetVariablesOptions: SetVariableOptions = {}
export const defaultConditionContent: ConditionContent = {
@@ -75,3 +93,5 @@ export const defaultConditionContent: ConditionContent = {
}
export const defaultRedirectOptions: RedirectOptions = { isNewTab: false }
export const defaultCodeOptions: CodeOptions = { name: 'Code snippet' }

View File

@@ -4,13 +4,11 @@ import {
IntegrationStepType,
Item,
LogicStepOptions,
RedirectStep,
SetVariableStep,
} from '.'
import { BubbleStep, BubbleStepType } from './bubble'
import { InputStep, InputStepType } from './inputs'
import { IntegrationStep } from './integration'
import { LogicStep, LogicStepType } from './logic'
import { ConditionStep, LogicStep, LogicStepType } from './logic'
export type Step =
| StartStep
@@ -36,14 +34,12 @@ export type DraggableStepType =
export type StepWithOptions =
| InputStep
| SetVariableStep
| RedirectStep
| Exclude<LogicStep, ConditionStep>
| IntegrationStep
export type StepWithOptionsType =
| InputStepType
| LogicStepType.REDIRECT
| LogicStepType.SET_VARIABLE
| Exclude<LogicStepType, LogicStepType.CONDITION>
| IntegrationStepType
export type StepOptions =