Skip to content

Commit

Permalink
fix(webhooks): improve body parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Aug 29, 2022
1 parent 02bb7f5 commit 43891b0
Showing 1 changed file with 26 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import prisma from 'libs/prisma'
import {
defaultWebhookAttributes,
HttpMethod,
KeyValue,
PublicTypebot,
ResultValues,
Expand Down Expand Up @@ -84,7 +83,7 @@ const prepareWebhookAttributes = (
return webhook
}

const bodyIsSingleVariable = (body: string) => /{{.+}}/.test(body)
const checkIfBodyIsAVariable = (body: string) => /^{{.+}}$/.test(body)

export const executeWebhook =
(typebot: Typebot) =>
Expand Down Expand Up @@ -124,17 +123,21 @@ export const executeWebhook =
)
const contentType = headers ? headers['Content-Type'] : undefined
const linkedTypebots = await getLinkedTypebots(typebot)
const body =
webhook.method !== HttpMethod.GET
? await getBodyContent(
typebot,
linkedTypebots
)({
body: webhook.body,
resultValues,
groupId,
})
: undefined
const bodyContent = await getBodyContent(
typebot,
linkedTypebots
)({
body: webhook.body,
resultValues,
groupId,
})
const { data: body, isJson } = bodyContent
? safeJsonParse(
parseVariables(variables, {
escapeForJson: !checkIfBodyIsAVariable(bodyContent),
})(bodyContent)
)
: { data: undefined, isJson: false }
const request = {
url: parseVariables(variables)(
webhook.url + (queryParams !== '' ? `?${queryParams}` : '')
Expand All @@ -143,38 +146,28 @@ export const executeWebhook =
headers,
...basicAuth,
json:
contentType !== 'x-www-form-urlencoded' && body
? safeJsonParse(
parseVariables(variables, {
escapeForJson: !bodyIsSingleVariable(body),
})(body)
)
: undefined,
form:
contentType === 'x-www-form-urlencoded' && body
? safeJsonParse(
parseVariables(variables, {
escapeForJson: !bodyIsSingleVariable(body),
})(body)
)
contentType !== 'x-www-form-urlencoded' && body && isJson
? body
: undefined,
form: contentType === 'x-www-form-urlencoded' && body ? body : undefined,
body: body && !isJson ? body : undefined,
}
try {
const response = await got(request.url, omit(request, 'url'))
await saveSuccessLog(resultId, 'Webhook successfuly executed.', {
statusCode: response.statusCode,
request,
response: parseBody(response.body),
response: safeJsonParse(response.body).data,
})
return {
statusCode: response.statusCode,
data: parseBody(response.body),
data: safeJsonParse(response.body).data,
}
} catch (error) {
if (error instanceof HTTPError) {
const response = {
statusCode: error.response.statusCode,
data: parseBody(error.response.body as string),
data: safeJsonParse(error.response.body as string).data,
}
await saveErrorLog(resultId, 'Webhook returned an error', {
request,
Expand Down Expand Up @@ -228,14 +221,6 @@ const getBodyContent =
: body
}

const parseBody = (body: string) => {
try {
return JSON.parse(body)
} catch (err) {
return body
}
}

const convertKeyValueTableToObject = (
keyValues: KeyValue[] | undefined,
variables: Variable[]
Expand All @@ -251,11 +236,11 @@ const convertKeyValueTableToObject = (
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const safeJsonParse = (json: string): any => {
const safeJsonParse = (json: string): { data: any; isJson: boolean } => {
try {
return JSON.parse(json)
return { data: JSON.parse(json), isJson: true }
} catch (err) {
return json
return { data: json, isJson: false }
}
}

Expand Down

5 comments on commit 43891b0

@vercel
Copy link

@vercel vercel bot commented on 43891b0 Aug 29, 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:

viewer-v2-alpha – ./apps/viewer

247987.com
ns8.vn
bot.aipr.kr
yobot.me
bt.id8rs.com
8jours.top
finplex.be
bot.aws.bj
vhpage.cr8.ai
sat.cr8.ai
an.nigerias.io
bot.ageenda.com
ar.nigerias.io
apo.nigerias.io
am.nigerias.io
bot.lalmon.com
apr.nigerias.io
bot.artiweb.app
ticketfute.com
aso.nigerias.io
bot.tc-mail.com
games.klujo.com
eventhub.com.au
chat.sureb4.com
sakuranembro.it
typebot.aloe.do
bot.upfunnel.art
botc.ceox.com.br
bot.piccinato.co
clo.closeer.work
faqs.nigerias.io
feedback.ofx.one
kw.wpwakanda.com
stan.vselise.com
form.syncwin.com
app.chatforms.net
typebot.aloe.bot
voicehelp.cr8.ai
bot.maitempah.com
bot.agfunnel.tech
bot.phuonghub.com
cares.urlabout.me
fmm.wpwakanda.com
k1.kandabrand.com
bot.reviewzer.com
gentleman-shop.fr
ov1.wpwakanda.com
lb.ticketfute.com
ov2.wpwakanda.com
ov3.wpwakanda.com
1988.bouclidom.com
andreimayer.com.br
bot.neferlopez.com
bot.megafox.com.br
dicanatural.online
goalsettingbot.com
survey.digienge.io
bot.digitalbled.com
zap.techadviser.in
bot.eventhub.com.au
this-is-a-test.com
order.maitempah.com
forms.webisharp.com
carsalesenquiry.com
nutrisamirbayde.com
quest.wpwakanda.com
bium.gratirabbit.com
typebot.stillio.com
bot.ansuraniphone.my
chat.hayurihijab.com
bot.cotemeuplano.com
click.sevenoways.com
get.freebotoffer.xyz
connect.growthguy.in
abutton.wpwakanda.com
aidigitalmarketing.kr
bbutton.wpwakanda.com
bot.incusservices.com
bot.ramonmatos.com.br
bot.meuesocial.com.br
cdd.searchcube.com.sg
chat.missarkansas.org
sbutton.wpwakanda.com
bbutton.wpwwakanda.com
apply.ansuraniphone.my
form.searchcube.com.sg
c23111azqw.nigerias.io
felipewelington.com.br
gcase.barrettamario.it
info.clickasuransi.com
kodawariab736.skeep.it
resume.gratirabbit.com
report.gratirabbit.com
view.onlinebotdemo.xyz
83242573.actualizar.xyz
bot.upgradesolutions.eu
mainmenu.diddancing.com
signup.hypemarketing.in
bot.blackboxtips.com.br
subfooter.wpwakanda.com

@vercel
Copy link

@vercel vercel bot commented on 43891b0 Aug 29, 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 43891b0 Aug 29, 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 43891b0 Aug 29, 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
builder-v2-typebot-io.vercel.app
app.typebot.io

@vercel
Copy link

@vercel vercel bot commented on 43891b0 Aug 29, 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:

docs – ./apps/docs

docs-git-main-typebot-io.vercel.app
docs-typebot-io.vercel.app
docs.typebot.io

Please sign in to comment.