Skip to content

Commit

Permalink
🐛 Await support in set variable and script code
Browse files Browse the repository at this point in the history
Closes #467
  • Loading branch information
baptisteArno committed Apr 17, 2023
1 parent 5610fe0 commit 918dffb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
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) => {
const func = Function(...args.map((arg) => arg.id), parseContent(content))
const func = AsyncFunction(
...args.map((arg) => arg.id),
parseContent(content)
)
try {
await func(...args.map((arg) => arg.value))
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { isNotDefined } from '@typebot.io/lib'
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 ({
content,
args,
}: ScriptToExecute): Promise<{ replyToSend: string | undefined }> => {
try {
const func = Function(
const func = AsyncFunction(
...args.map((arg) => arg.id),
content.includes('return ') ? content : `return ${content}`
)
Expand All @@ -15,6 +18,7 @@ export const executeSetVariable = async ({
replyToSend: safeStringify(replyToSend),
}
} catch (err) {
console.error(err)
return {
replyToSend: safeStringify(content),
}
Expand Down

0 comments on commit 918dffb

Please sign in to comment.