2
0

🐛 (forge) Fix incompatible auth schemas when finding fetchers

Closes #1758
This commit is contained in:
Baptiste Arnaud
2024-09-04 11:43:02 +02:00
parent 2a767e02b0
commit 18c6381a77
4 changed files with 31 additions and 21 deletions

View File

@ -6,6 +6,7 @@ import { isReadWorkspaceFobidden } from '@/features/workspace/helpers/isReadWork
import { forgedBlocks } from '@typebot.io/forge-repository/definitions' import { forgedBlocks } from '@typebot.io/forge-repository/definitions'
import { forgedBlockIds } from '@typebot.io/forge-repository/constants' import { forgedBlockIds } from '@typebot.io/forge-repository/constants'
import { decrypt } from '@typebot.io/lib/api/encryption/decrypt' import { decrypt } from '@typebot.io/lib/api/encryption/decrypt'
import { getFetchers } from '../helpers/getFetchers'
export const fetchSelectItems = authenticatedProcedure export const fetchSelectItems = authenticatedProcedure
.input( .input(
@ -58,10 +59,9 @@ export const fetchSelectItems = authenticatedProcedure
const blockDef = forgedBlocks[integrationId] const blockDef = forgedBlocks[integrationId]
const fetchers = (blockDef?.fetchers ?? []).concat( const fetcher = getFetchers(blockDef).find(
blockDef?.actions.flatMap((action) => action.fetchers ?? []) ?? [] (fetcher) => fetcher.id === fetcherId
) )
const fetcher = fetchers.find((fetcher) => fetcher.id === fetcherId)
if (!fetcher) return { items: [] } if (!fetcher) return { items: [] }

View File

@ -17,6 +17,7 @@ import {
ForgedBlock, ForgedBlock,
} from '@typebot.io/forge-repository/types' } from '@typebot.io/forge-repository/types'
import { ReactNode, useMemo } from 'react' import { ReactNode, useMemo } from 'react'
import { findFetcher } from '../helpers/findFetcher'
type Props = { type Props = {
blockDef: ForgedBlockDefinition blockDef: ForgedBlockDefinition
@ -51,32 +52,25 @@ export const ForgeSelectInput = ({
const { workspace } = useWorkspace() const { workspace } = useWorkspace()
const { showToast } = useToast() const { showToast } = useToast()
const baseFetcher = useMemo(() => { const fetcher = useMemo(
const fetchers = blockDef.fetchers ?? [] () => findFetcher(blockDef, fetcherId),
return fetchers.find((fetcher) => fetcher.id === fetcherId) [blockDef, fetcherId]
}, [blockDef.fetchers, fetcherId]) )
const actionFetcher = useMemo(() => {
if (baseFetcher) return
const fetchers = blockDef.actions.flatMap((action) => action.fetchers ?? [])
return fetchers.find((fetcher) => fetcher.id === fetcherId)
}, [baseFetcher, blockDef.actions, fetcherId])
const { data } = trpc.forge.fetchSelectItems.useQuery( const { data } = trpc.forge.fetchSelectItems.useQuery(
{ {
integrationId: blockDef.id, integrationId: blockDef.id,
options: pick(options, [ options: pick(
...(actionFetcher ? ['action'] : []), options,
...(blockDef.auth ? ['credentialsId'] : []), (blockDef.auth ? ['credentialsId'] : []).concat(
...((baseFetcher fetcher?.dependencies ?? []
? baseFetcher.dependencies )
: actionFetcher?.dependencies) ?? []), ),
]),
workspaceId: workspace?.id as string, workspaceId: workspace?.id as string,
fetcherId, fetcherId,
}, },
{ {
enabled: !!workspace?.id && (!!baseFetcher || !!actionFetcher), enabled: !!workspace?.id && !!fetcher,
onError: (error) => { onError: (error) => {
showToast({ showToast({
description: error.message, description: error.message,

View File

@ -0,0 +1,7 @@
import { ForgedBlockDefinition } from '@typebot.io/forge-repository/types'
import { getFetchers } from './getFetchers'
export const findFetcher = (
blockDef: ForgedBlockDefinition,
fetcherId: string
) => getFetchers(blockDef).find((fetcher) => fetcher.id === fetcherId)

View File

@ -0,0 +1,9 @@
import { FetcherDefinition, AuthDefinition } from '@typebot.io/forge'
import { ForgedBlockDefinition } from '@typebot.io/forge-repository/types'
export const getFetchers = (blockDef: ForgedBlockDefinition) =>
blockDef.fetchers?.concat(
blockDef.actions.flatMap(
(action) => (action.fetchers ?? []) as FetcherDefinition<AuthDefinition>[]
)
) ?? []