2
0

🐛 (webhook) Fix test request execution invalid timeout

This commit is contained in:
Baptiste Arnaud
2024-01-04 14:09:06 +01:00
parent 5266be1a51
commit f73bc46208
3 changed files with 39 additions and 19 deletions

View File

@ -9,7 +9,13 @@ import {
} from '@typebot.io/schemas' } from '@typebot.io/schemas'
import { NextApiRequest, NextApiResponse } from 'next' import { NextApiRequest, NextApiResponse } from 'next'
import got, { Method, Headers, HTTPError } from 'got' import got, { Method, Headers, HTTPError } from 'got'
import { byId, isEmpty, isWebhookBlock, omit } from '@typebot.io/lib' import {
byId,
isEmpty,
isNotDefined,
isWebhookBlock,
omit,
} from '@typebot.io/lib'
import { parseAnswers } from '@typebot.io/lib/results' import { parseAnswers } from '@typebot.io/lib/results'
import { initMiddleware, methodNotAllowed, notFound } from '@typebot.io/lib/api' import { initMiddleware, methodNotAllowed, notFound } from '@typebot.io/lib/api'
import { stringify } from 'qs' import { stringify } from 'qs'
@ -32,6 +38,7 @@ import {
convertKeyValueTableToObject, convertKeyValueTableToObject,
longReqTimeoutWhitelist, longReqTimeoutWhitelist,
} from '@typebot.io/bot-engine/blocks/integrations/webhook/executeWebhookBlock' } from '@typebot.io/bot-engine/blocks/integrations/webhook/executeWebhookBlock'
import { env } from '@typebot.io/env'
const cors = initMiddleware(Cors()) const cors = initMiddleware(Cors())
@ -78,6 +85,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
resultId, resultId,
parentTypebotIds, parentTypebotIds,
isCustomBody: block.options?.isCustomBody, isCustomBody: block.options?.isCustomBody,
timeout: block.options?.timeout,
}) })
return res.status(200).send(result) return res.status(200).send(result)
} }
@ -96,6 +104,7 @@ export const executeWebhook =
resultId, resultId,
parentTypebotIds = [], parentTypebotIds = [],
isCustomBody, isCustomBody,
timeout,
}: { }: {
webhook: Webhook webhook: Webhook
variables: Variable[] variables: Variable[]
@ -104,6 +113,7 @@ export const executeWebhook =
resultId?: string resultId?: string
parentTypebotIds: string[] parentTypebotIds: string[]
isCustomBody?: boolean isCustomBody?: boolean
timeout?: number
}): Promise<WebhookResponse> => { }): Promise<WebhookResponse> => {
if (!webhook.url) if (!webhook.url)
return { return {
@ -184,7 +194,13 @@ export const executeWebhook =
: undefined, : undefined,
body: body && !isJson ? body : undefined, body: body && !isJson ? body : undefined,
timeout: { timeout: {
response: isLongRequest ? maxTimeout : defaultTimeout, response: isNotDefined(env.CHAT_API_TIMEOUT)
? undefined
: timeout && timeout !== defaultTimeout
? Math.min(timeout, maxTimeout) * 1000
: isLongRequest
? maxTimeout * 1000
: defaultTimeout * 1000,
}, },
} }
try { try {

View File

@ -13,7 +13,7 @@ import {
AnswerInSessionState, AnswerInSessionState,
} from '@typebot.io/schemas' } from '@typebot.io/schemas'
import { stringify } from 'qs' import { stringify } from 'qs'
import { isDefined, isEmpty, omit } from '@typebot.io/lib' import { isDefined, isEmpty, isNotDefined, omit } from '@typebot.io/lib'
import { getDefinedVariables, parseAnswers } from '@typebot.io/lib/results' import { getDefinedVariables, parseAnswers } from '@typebot.io/lib/results'
import got, { Method, HTTPError, OptionsInit } from 'got' import got, { Method, HTTPError, OptionsInit } from 'got'
import { resumeWebhookExecution } from './resumeWebhookExecution' import { resumeWebhookExecution } from './resumeWebhookExecution'
@ -26,6 +26,7 @@ import {
defaultWebhookAttributes, defaultWebhookAttributes,
maxTimeout, maxTimeout,
} from '@typebot.io/schemas/features/blocks/integrations/webhook/constants' } from '@typebot.io/schemas/features/blocks/integrations/webhook/constants'
import { env } from '@typebot.io/env'
type ParsedWebhook = ExecutableWebhook & { type ParsedWebhook = ExecutableWebhook & {
basicAuth: { username?: string; password?: string } basicAuth: { username?: string; password?: string }
@ -198,12 +199,13 @@ export const executeWebhook = async (
contentType?.includes('x-www-form-urlencoded') && body ? body : undefined, contentType?.includes('x-www-form-urlencoded') && body ? body : undefined,
body: body && !isJson ? (body as string) : undefined, body: body && !isJson ? (body as string) : undefined,
timeout: { timeout: {
response: response: isNotDefined(env.CHAT_API_TIMEOUT)
params.timeout && params.timeout !== defaultTimeout ? undefined
? Math.min(params.timeout, maxTimeout) * 1000 : params.timeout && params.timeout !== defaultTimeout
: isLongRequest ? Math.min(params.timeout, maxTimeout) * 1000
? maxTimeout * 1000 : isLongRequest
: defaultTimeout * 1000, ? maxTimeout * 1000
: defaultTimeout * 1000,
}, },
} satisfies OptionsInit } satisfies OptionsInit

View File

@ -54,16 +54,18 @@ const extractLinkedInputBlocks =
const linkedBotInputs = const linkedBotInputs =
previousLinkedTypebotBlocks.length > 0 previousLinkedTypebotBlocks.length > 0
? await Promise.all( ? await Promise.all(
previousLinkedTypebotBlocks.map((linkedBot) => previousLinkedTypebotBlocks.map((linkedBot) => {
extractLinkedInputBlocks( const linkedTypebot = linkedTypebots.find((t) =>
linkedTypebots.find((t) => 'typebotId' in t
'typebotId' in t ? t.typebotId === linkedBot.options?.typebotId
? t.typebotId === linkedBot.options?.typebotId : t.id === linkedBot.options?.typebotId
: t.id === linkedBot.options?.typebotId )
) ?? typebot, if (!linkedTypebot) return []
linkedTypebots return extractLinkedInputBlocks(linkedTypebot, linkedTypebots)(
)(linkedBot.options?.groupId, 'forward') linkedBot.options?.groupId,
) 'forward'
)
})
) )
: [] : []