@@ -55,10 +55,10 @@ export const continueBotFlow =
|
||||
const existingVariable = state.typebot.variables.find(
|
||||
byId(block.options.variableId)
|
||||
)
|
||||
if (existingVariable) {
|
||||
if (existingVariable && reply) {
|
||||
const newVariable = {
|
||||
...existingVariable,
|
||||
value: reply,
|
||||
value: safeJsonParse(reply),
|
||||
}
|
||||
newSessionState = updateVariables(state)([newVariable])
|
||||
}
|
||||
@@ -318,3 +318,11 @@ export const isReplyValid = (inputValue: string, block: Block): boolean => {
|
||||
|
||||
export const canSkip = (inputType: InputBlockType) =>
|
||||
inputType === InputBlockType.FILE
|
||||
|
||||
export const safeJsonParse = (value: string): unknown => {
|
||||
try {
|
||||
return JSON.parse(value)
|
||||
} catch {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { isDefined } from '@typebot.io/lib'
|
||||
import { Variable, VariableWithValue } from '@typebot.io/schemas'
|
||||
import { safeStringify } from './safeStringify'
|
||||
import { safeStringify } from '@typebot.io/lib/safeStringify'
|
||||
|
||||
export type ParseVariablesOptions = {
|
||||
fieldToParse?: 'value' | 'id'
|
||||
@@ -42,7 +42,9 @@ export const parseVariables =
|
||||
if (options.escapeForJson)
|
||||
return (
|
||||
dollarSign +
|
||||
jsonParse(typeof value !== 'string' ? JSON.stringify(value) : value)
|
||||
(typeof value === 'string'
|
||||
? jsonParse(value)
|
||||
: JSON.stringify(value))
|
||||
)
|
||||
const parsedValue =
|
||||
dollarSign +
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { StartParams, Variable } from '@typebot.io/schemas'
|
||||
import { safeStringify } from './safeStringify'
|
||||
import { safeStringify } from '@typebot.io/lib/safeStringify'
|
||||
|
||||
export const prefillVariables = (
|
||||
variables: Variable[],
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
import { isNotDefined } from '@typebot.io/lib'
|
||||
|
||||
export const safeStringify = (val: unknown): string | null => {
|
||||
if (isNotDefined(val)) return null
|
||||
if (typeof val === 'string') return val
|
||||
try {
|
||||
return JSON.stringify(val)
|
||||
} catch {
|
||||
console.warn('Failed to safely stringify variable value', val)
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
VariableWithValue,
|
||||
Variable,
|
||||
} from '@typebot.io/schemas'
|
||||
import { safeStringify } from './safeStringify'
|
||||
import { safeStringify } from '@typebot.io/lib/safeStringify'
|
||||
|
||||
export const updateVariables =
|
||||
(state: SessionState) =>
|
||||
|
||||
Reference in New Issue
Block a user