Skip to content

Commit

Permalink
fix(engine): 🐛 Multi-line var parsing for webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed May 27, 2022
1 parent e72934d commit d02f267
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,15 @@ export const executeWebhook =
...basicAuth,
json:
contentType !== 'x-www-form-urlencoded' && body
? safeJsonParse(parseVariables(variables)(body))
? safeJsonParse(
parseVariables(variables, { escapeLineBreaks: true })(body)
)
: undefined,
form:
contentType === 'x-www-form-urlencoded' && body
? safeJsonParse(parseVariables(variables)(body))
? safeJsonParse(
parseVariables(variables, { escapeLineBreaks: true })(body)
)
: undefined,
}
try {
Expand Down
16 changes: 10 additions & 6 deletions packages/bot-engine/src/services/variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ export const stringContainsVariable = (str: string): boolean =>
export const parseVariables =
(
variables: Variable[],
options: { fieldToParse: 'value' | 'id' } = { fieldToParse: 'value' }
options: { fieldToParse?: 'value' | 'id'; escapeLineBreaks?: boolean } = {
fieldToParse: 'value',
escapeLineBreaks: false,
}
) =>
(text: string | undefined): string => {
if (!text || text === '') return ''
Expand All @@ -17,11 +20,12 @@ export const parseVariables =
return matchedVarName === v.name && isDefined(v.value)
})
if (!variable) return ''
return (
(options.fieldToParse === 'value'
? variable.value?.toString()
: variable.id) || ''
)
if (options.fieldToParse === 'id') return variable.id
const { value } = variable
if (!value) return ''
if (options.escapeLineBreaks)
return value.toString().replace(/\n/g, '\\n')
return value.toString()
})
}

Expand Down

4 comments on commit d02f267

@vercel
Copy link

@vercel vercel bot commented on d02f267 May 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on d02f267 May 27, 2022

@vercel
Copy link

@vercel vercel bot commented on d02f267 May 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

builder-v2-git-main-typebot-io.vercel.app
app.typebot.io
builder-v2-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on d02f267 May 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.