2
0
Files
bot/packages/embeds/js/src/features/blocks/logic/script/executeScript.ts
2023-08-28 14:50:58 +02:00

24 lines
672 B
TypeScript

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) => {
try {
const func = AsyncFunction(
...args.map((arg) => arg.id),
parseContent(content)
)
await func(...args.map((arg) => arg.value))
} catch (err) {
console.error(err)
}
}
const parseContent = (content: string) => {
const contentWithoutScriptTags = content
.replace(/<script>/g, '')
.replace(/<\/script>/g, '')
return contentWithoutScriptTags
}