@ -1,7 +1,13 @@
|
|||||||
import type { ScriptToExecute } from '@typebot.io/schemas'
|
import type { ScriptToExecute } from '@typebot.io/schemas'
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||||
|
const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor
|
||||||
|
|
||||||
export const executeScript = async ({ content, args }: ScriptToExecute) => {
|
export const executeScript = async ({ content, args }: ScriptToExecute) => {
|
||||||
const func = Function(...args.map((arg) => arg.id), parseContent(content))
|
const func = AsyncFunction(
|
||||||
|
...args.map((arg) => arg.id),
|
||||||
|
parseContent(content)
|
||||||
|
)
|
||||||
try {
|
try {
|
||||||
await func(...args.map((arg) => arg.value))
|
await func(...args.map((arg) => arg.value))
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
import { isNotDefined } from '@typebot.io/lib'
|
import { isNotDefined } from '@typebot.io/lib'
|
||||||
import type { ScriptToExecute } from '@typebot.io/schemas'
|
import type { ScriptToExecute } from '@typebot.io/schemas'
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||||
|
const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor
|
||||||
|
|
||||||
export const executeSetVariable = async ({
|
export const executeSetVariable = async ({
|
||||||
content,
|
content,
|
||||||
args,
|
args,
|
||||||
}: ScriptToExecute): Promise<{ replyToSend: string | undefined }> => {
|
}: ScriptToExecute): Promise<{ replyToSend: string | undefined }> => {
|
||||||
try {
|
try {
|
||||||
const func = Function(
|
const func = AsyncFunction(
|
||||||
...args.map((arg) => arg.id),
|
...args.map((arg) => arg.id),
|
||||||
content.includes('return ') ? content : `return ${content}`
|
content.includes('return ') ? content : `return ${content}`
|
||||||
)
|
)
|
||||||
@ -15,6 +18,7 @@ export const executeSetVariable = async ({
|
|||||||
replyToSend: safeStringify(replyToSend),
|
replyToSend: safeStringify(replyToSend),
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.error(err)
|
||||||
return {
|
return {
|
||||||
replyToSend: safeStringify(content),
|
replyToSend: safeStringify(content),
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user