2
0

(setVariable) Add client-side set variable execution

Closes #461
This commit is contained in:
Baptiste Arnaud
2023-04-14 12:11:42 +02:00
parent 397a33afc6
commit 03cc067418
17 changed files with 207 additions and 69 deletions

View File

@ -0,0 +1,33 @@
import { isNotDefined } from '@typebot.io/lib'
import type { ScriptToExecute } from '@typebot.io/schemas'
export const executeSetVariable = async ({
content,
args,
}: ScriptToExecute): Promise<{ replyToSend: string | undefined }> => {
try {
const func = Function(
...args.map((arg) => arg.id),
content.includes('return ') ? content : `return ${content}`
)
const replyToSend = await func(...args.map((arg) => arg.value))
return {
replyToSend: safeStringify(replyToSend),
}
} catch (err) {
return {
replyToSend: safeStringify(content),
}
}
}
export const safeStringify = (val: unknown): string | undefined => {
if (isNotDefined(val)) return
if (typeof val === 'string') return val
try {
return JSON.stringify(val)
} catch {
console.warn('Failed to safely stringify variable value', val)
return
}
}