From f73bc46208367e2a0db33dbd89f50108d33401e9 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Thu, 4 Jan 2024 14:09:06 +0100 Subject: [PATCH] :bug: (webhook) Fix test request execution invalid timeout --- .../blocks/[blockId]/executeWebhook.ts | 20 +++++++++++++++-- .../webhook/executeWebhookBlock.ts | 16 ++++++++------ .../integrations/webhook/parseSampleResult.ts | 22 ++++++++++--------- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/executeWebhook.ts b/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/executeWebhook.ts index f9b5c5565..3b5d04f6b 100644 --- a/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/executeWebhook.ts +++ b/apps/viewer/src/pages/api/typebots/[typebotId]/blocks/[blockId]/executeWebhook.ts @@ -9,7 +9,13 @@ import { } from '@typebot.io/schemas' import { NextApiRequest, NextApiResponse } from 'next' 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 { initMiddleware, methodNotAllowed, notFound } from '@typebot.io/lib/api' import { stringify } from 'qs' @@ -32,6 +38,7 @@ import { convertKeyValueTableToObject, longReqTimeoutWhitelist, } from '@typebot.io/bot-engine/blocks/integrations/webhook/executeWebhookBlock' +import { env } from '@typebot.io/env' const cors = initMiddleware(Cors()) @@ -78,6 +85,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { resultId, parentTypebotIds, isCustomBody: block.options?.isCustomBody, + timeout: block.options?.timeout, }) return res.status(200).send(result) } @@ -96,6 +104,7 @@ export const executeWebhook = resultId, parentTypebotIds = [], isCustomBody, + timeout, }: { webhook: Webhook variables: Variable[] @@ -104,6 +113,7 @@ export const executeWebhook = resultId?: string parentTypebotIds: string[] isCustomBody?: boolean + timeout?: number }): Promise => { if (!webhook.url) return { @@ -184,7 +194,13 @@ export const executeWebhook = : undefined, body: body && !isJson ? body : undefined, 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 { diff --git a/packages/bot-engine/blocks/integrations/webhook/executeWebhookBlock.ts b/packages/bot-engine/blocks/integrations/webhook/executeWebhookBlock.ts index 699e60b97..59cd1130e 100644 --- a/packages/bot-engine/blocks/integrations/webhook/executeWebhookBlock.ts +++ b/packages/bot-engine/blocks/integrations/webhook/executeWebhookBlock.ts @@ -13,7 +13,7 @@ import { AnswerInSessionState, } from '@typebot.io/schemas' 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 got, { Method, HTTPError, OptionsInit } from 'got' import { resumeWebhookExecution } from './resumeWebhookExecution' @@ -26,6 +26,7 @@ import { defaultWebhookAttributes, maxTimeout, } from '@typebot.io/schemas/features/blocks/integrations/webhook/constants' +import { env } from '@typebot.io/env' type ParsedWebhook = ExecutableWebhook & { basicAuth: { username?: string; password?: string } @@ -198,12 +199,13 @@ export const executeWebhook = async ( contentType?.includes('x-www-form-urlencoded') && body ? body : undefined, body: body && !isJson ? (body as string) : undefined, timeout: { - response: - params.timeout && params.timeout !== defaultTimeout - ? Math.min(params.timeout, maxTimeout) * 1000 - : isLongRequest - ? maxTimeout * 1000 - : defaultTimeout * 1000, + response: isNotDefined(env.CHAT_API_TIMEOUT) + ? undefined + : params.timeout && params.timeout !== defaultTimeout + ? Math.min(params.timeout, maxTimeout) * 1000 + : isLongRequest + ? maxTimeout * 1000 + : defaultTimeout * 1000, }, } satisfies OptionsInit diff --git a/packages/bot-engine/blocks/integrations/webhook/parseSampleResult.ts b/packages/bot-engine/blocks/integrations/webhook/parseSampleResult.ts index 7de9d9717..c534d7c29 100644 --- a/packages/bot-engine/blocks/integrations/webhook/parseSampleResult.ts +++ b/packages/bot-engine/blocks/integrations/webhook/parseSampleResult.ts @@ -54,16 +54,18 @@ const extractLinkedInputBlocks = const linkedBotInputs = previousLinkedTypebotBlocks.length > 0 ? await Promise.all( - previousLinkedTypebotBlocks.map((linkedBot) => - extractLinkedInputBlocks( - linkedTypebots.find((t) => - 'typebotId' in t - ? t.typebotId === linkedBot.options?.typebotId - : t.id === linkedBot.options?.typebotId - ) ?? typebot, - linkedTypebots - )(linkedBot.options?.groupId, 'forward') - ) + previousLinkedTypebotBlocks.map((linkedBot) => { + const linkedTypebot = linkedTypebots.find((t) => + 'typebotId' in t + ? t.typebotId === linkedBot.options?.typebotId + : t.id === linkedBot.options?.typebotId + ) + if (!linkedTypebot) return [] + return extractLinkedInputBlocks(linkedTypebot, linkedTypebots)( + linkedBot.options?.groupId, + 'forward' + ) + }) ) : []