2023-02-21 15:25:14 +01:00
|
|
|
import { BotProps } from '@typebot.io/js'
|
2023-02-25 10:16:57 +01:00
|
|
|
import parserBabel from 'prettier/parser-babel'
|
|
|
|
import prettier from 'prettier/standalone'
|
2023-02-21 15:25:14 +01:00
|
|
|
import { isDefined } from 'utils'
|
|
|
|
|
|
|
|
export const parseStringParam = (fieldName: string, fieldValue?: string) =>
|
|
|
|
fieldValue ? `${fieldName}: "${fieldValue}",` : ``
|
|
|
|
|
|
|
|
export const parseNumberOrBoolParam = (
|
|
|
|
fieldName: string,
|
|
|
|
fieldValue?: number | boolean
|
|
|
|
) => (isDefined(fieldValue) ? `${fieldName}: ${fieldValue},` : ``)
|
|
|
|
|
|
|
|
export const parseBotProps = ({ typebot, apiHost }: BotProps) => {
|
|
|
|
const typebotLine = parseStringParam('typebot', typebot as string)
|
|
|
|
const apiHostLine = parseStringParam('apiHost', apiHost)
|
|
|
|
return `${typebotLine}${apiHostLine}`
|
|
|
|
}
|
|
|
|
|
|
|
|
export const parseReactStringParam = (fieldName: string, fieldValue?: string) =>
|
|
|
|
fieldValue ? `${fieldName}="${fieldValue}"` : ``
|
|
|
|
|
|
|
|
export const parseReactNumberOrBoolParam = (
|
|
|
|
fieldName: string,
|
|
|
|
fieldValue?: number | boolean
|
|
|
|
) => (isDefined(fieldValue) ? `${fieldName}={${fieldValue}}` : ``)
|
|
|
|
|
|
|
|
export const parseReactBotProps = ({ typebot, apiHost }: BotProps) => {
|
|
|
|
const typebotLine = parseReactStringParam('typebot', typebot as string)
|
|
|
|
const apiHostLine = parseReactStringParam('apiHost', apiHost)
|
|
|
|
return `${typebotLine} ${apiHostLine}`
|
|
|
|
}
|
|
|
|
|
2023-02-22 07:46:30 +01:00
|
|
|
export const typebotImportUrl = `https://cdn.jsdelivr.net/npm/@typebot.io/js@0.0.14/dist/web.js`
|
2023-02-25 10:16:57 +01:00
|
|
|
|
|
|
|
export const parseInlineScript = (script: string) =>
|
|
|
|
prettier.format(
|
|
|
|
`const typebotInitScript = document.createElement("script");
|
|
|
|
typebotInitScript.type = "module";
|
|
|
|
typebotInitScript.innerHTML = \`${script}\`;
|
|
|
|
document.body.append(typebotInitScript);`,
|
|
|
|
{ parser: 'babel', plugins: [parserBabel] }
|
|
|
|
)
|