2023-03-15 12:21:52 +01:00
|
|
|
import { Variable } from '@typebot.io/schemas'
|
2023-03-15 17:47:05 +01:00
|
|
|
import {
|
|
|
|
|
defaultParseVariablesOptions,
|
|
|
|
|
parseVariables,
|
|
|
|
|
ParseVariablesOptions,
|
|
|
|
|
} from './parseVariables'
|
2023-03-15 12:21:52 +01:00
|
|
|
|
|
|
|
|
export const deepParseVariables =
|
2023-03-15 17:47:05 +01:00
|
|
|
(
|
|
|
|
|
variables: Variable[],
|
2023-05-05 11:32:58 -04:00
|
|
|
options: ParseVariablesOptions = defaultParseVariablesOptions
|
2023-03-15 17:47:05 +01:00
|
|
|
) =>
|
2023-03-15 12:21:52 +01:00
|
|
|
<T extends Record<string, unknown>>(object: T): T =>
|
|
|
|
|
Object.keys(object).reduce<T>((newObj, key) => {
|
|
|
|
|
const currentValue = object[key]
|
|
|
|
|
|
2023-05-03 18:09:25 -04:00
|
|
|
if (typeof currentValue === 'string') {
|
|
|
|
|
const parsedVariable = parseVariables(variables, options)(currentValue)
|
2023-03-15 17:47:05 +01:00
|
|
|
return {
|
|
|
|
|
...newObj,
|
2023-05-05 11:32:58 -04:00
|
|
|
[key]: parsedVariable,
|
2023-03-15 17:47:05 +01:00
|
|
|
}
|
2023-05-03 18:09:25 -04:00
|
|
|
}
|
2023-03-15 12:21:52 +01:00
|
|
|
|
|
|
|
|
if (currentValue instanceof Object && currentValue.constructor === Object)
|
|
|
|
|
return {
|
|
|
|
|
...newObj,
|
2023-03-15 17:47:05 +01:00
|
|
|
[key]: deepParseVariables(
|
|
|
|
|
variables,
|
|
|
|
|
options
|
|
|
|
|
)(currentValue as Record<string, unknown>),
|
2023-03-15 12:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentValue instanceof Array)
|
|
|
|
|
return {
|
|
|
|
|
...newObj,
|
2023-03-15 17:47:05 +01:00
|
|
|
[key]: currentValue.map(deepParseVariables(variables, options)),
|
2023-03-15 12:21:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { ...newObj, [key]: currentValue }
|
|
|
|
|
}, {} as T)
|