2
0

♻️ Include forged blocks schema in typebot schema

Closes #1364
This commit is contained in:
Baptiste Arnaud
2024-03-18 16:09:19 +01:00
parent 26a9282c20
commit ed5096e2b6
93 changed files with 8599 additions and 4965 deletions

View File

@@ -99,7 +99,6 @@
"@playwright/test": "1.41.2",
"@typebot.io/forge": "workspace:*",
"@typebot.io/forge-repository": "workspace:*",
"@typebot.io/forge-schemas": "workspace:*",
"@typebot.io/lib": "workspace:*",
"@typebot.io/prisma": "workspace:*",
"@typebot.io/radar": "workspace:*",

View File

@@ -12,9 +12,9 @@ import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/const
import { IntegrationBlockType } from '@typebot.io/schemas/features/blocks/integrations/constants'
import { LogicBlockType } from '@typebot.io/schemas/features/blocks/logic/constants'
import { BlockV6 } from '@typebot.io/schemas'
import { enabledBlocks } from '@typebot.io/forge-repository'
import { BlockCardLayout } from './BlockCardLayout'
import { ForgedBlockCard } from '@/features/forge/ForgedBlockCard'
import { isForgedBlockType } from '@typebot.io/schemas/features/blocks/forged/helpers'
type Props = {
type: BlockV6['type']
@@ -30,13 +30,8 @@ export const BlockCard = (
const { t } = useTranslate()
const { workspace } = useWorkspace()
if (enabledBlocks.includes(props.type as (typeof enabledBlocks)[number])) {
return (
<ForgedBlockCard
type={props.type as (typeof enabledBlocks)[number]}
onMouseDown={props.onMouseDown}
/>
)
if (isForgedBlockType(props.type)) {
return <ForgedBlockCard type={props.type} onMouseDown={props.onMouseDown} />
}
switch (props.type) {
case BubbleBlockType.EMBED:

View File

@@ -23,8 +23,8 @@ import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/const
import { IntegrationBlockType } from '@typebot.io/schemas/features/blocks/integrations/constants'
import { LogicBlockType } from '@typebot.io/schemas/features/blocks/logic/constants'
import { BlockV6 } from '@typebot.io/schemas'
import { enabledBlocks } from '@typebot.io/forge-repository'
import { useDebouncedCallback } from 'use-debounce'
import { forgedBlockIds } from '@typebot.io/forge-repository/constants'
// Integration blocks migrated to forged blocks
const legacyIntegrationBlocks = [
@@ -174,7 +174,7 @@ export const BlocksSideBar = () => {
</Text>
<SimpleGrid columns={2} spacing="3">
{Object.values(IntegrationBlockType)
.concat(enabledBlocks as any)
.concat(forgedBlockIds as any)
.filter((type) => !legacyIntegrationBlocks.includes(type))
.map((type) => (
<BlockCard

View File

@@ -1,4 +1,4 @@
import { ForgedBlock } from '@typebot.io/forge-schemas'
import { ForgedBlock } from '@typebot.io/forge-repository/types'
import { BlockV6 } from '@typebot.io/schemas'
import { BlockIcon } from '../editor/components/BlockIcon'
import { BlockLabel } from '../editor/components/BlockLabel'

View File

@@ -1,5 +1,5 @@
import { useColorMode } from '@chakra-ui/react'
import { ForgedBlock } from '@typebot.io/forge-schemas'
import { ForgedBlock } from '@typebot.io/forge-repository/types'
import { useForgedBlock } from './hooks/useForgedBlock'
export const ForgedBlockIcon = ({

View File

@@ -1,4 +1,4 @@
import { ForgedBlock } from '@typebot.io/forge-schemas'
import { ForgedBlock } from '@typebot.io/forge-repository/types'
import { useForgedBlock } from './hooks/useForgedBlock'
import { Text } from '@chakra-ui/react'

View File

@@ -4,7 +4,8 @@ import { TRPCError } from '@trpc/server'
import { encrypt } from '@typebot.io/lib/api/encryption/encrypt'
import { z } from 'zod'
import { isWriteWorkspaceForbidden } from '@/features/workspace/helpers/isWriteWorkspaceForbidden'
import { forgedCredentialsSchemas } from '@typebot.io/forge-schemas'
import { forgedCredentialsSchemas } from '@typebot.io/forge-repository/credentials'
import { isDefined } from '@typebot.io/lib'
const inputShape = {
data: true,
@@ -20,7 +21,9 @@ export const createCredentials = authenticatedProcedure
'type',
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
forgedCredentialsSchemas.map((i) => i.pick(inputShape))
Object.values(forgedCredentialsSchemas)
.filter(isDefined)
.map((i) => i.pick(inputShape))
),
})
)

View File

@@ -3,13 +3,13 @@ import { authenticatedProcedure } from '@/helpers/server/trpc'
import { TRPCError } from '@trpc/server'
import { z } from 'zod'
import { isReadWorkspaceFobidden } from '@/features/workspace/helpers/isReadWorkspaceFobidden'
import { enabledBlocks } from '@typebot.io/forge-repository'
import { forgedBlockIds } from '@typebot.io/forge-repository/constants'
export const listCredentials = authenticatedProcedure
.input(
z.object({
workspaceId: z.string(),
type: z.enum(enabledBlocks),
type: z.enum(forgedBlockIds),
})
)
.query(async ({ input: { workspaceId, type }, ctx: { user } }) => {

View File

@@ -3,13 +3,14 @@ import { authenticatedProcedure } from '@/helpers/server/trpc'
import { TRPCError } from '@trpc/server'
import { z } from 'zod'
import { isReadWorkspaceFobidden } from '@/features/workspace/helpers/isReadWorkspaceFobidden'
import { forgedBlocks } from '@typebot.io/forge-schemas'
import { forgedBlocks } from '@typebot.io/forge-repository/definitions'
import { forgedBlockIds } from '@typebot.io/forge-repository/constants'
import { decrypt } from '@typebot.io/lib/api/encryption/decrypt'
export const fetchSelectItems = authenticatedProcedure
.input(
z.object({
integrationId: z.string(),
integrationId: z.enum(forgedBlockIds),
fetcherId: z.string(),
options: z.any(),
workspaceId: z.string(),
@@ -55,7 +56,7 @@ export const fetchSelectItems = authenticatedProcedure
const credentialsData = await decrypt(credentials.data, credentials.iv)
const blockDef = forgedBlocks.find((b) => b.id === integrationId)
const blockDef = forgedBlocks[integrationId]
const fetchers = (blockDef?.fetchers ?? []).concat(
blockDef?.actions.flatMap((action) => action.fetchers ?? []) ?? []

View File

@@ -11,7 +11,10 @@ import {
HStack,
Stack,
} from '@chakra-ui/react'
import { ForgedBlockDefinition, ForgedBlock } from '@typebot.io/forge-schemas'
import {
ForgedBlockDefinition,
ForgedBlock,
} from '@typebot.io/forge-repository/types'
import { ReactNode, useMemo } from 'react'
type Props = {

View File

@@ -2,7 +2,7 @@ import { SetVariableLabel } from '@/components/SetVariableLabel'
import { useTypebot } from '@/features/editor/providers/TypebotProvider'
import { Stack, Text } from '@chakra-ui/react'
import { useForgedBlock } from '../hooks/useForgedBlock'
import { ForgedBlock } from '@typebot.io/forge-schemas'
import { ForgedBlock } from '@typebot.io/forge-repository/types'
type Props = {
block: ForgedBlock

View File

@@ -5,7 +5,7 @@ import { ForgedCredentialsModal } from './credentials/ForgedCredentialsModal'
import { ZodObjectLayout } from './zodLayouts/ZodObjectLayout'
import { ZodActionDiscriminatedUnion } from './zodLayouts/ZodActionDiscriminatedUnion'
import { useForgedBlock } from '../hooks/useForgedBlock'
import { ForgedBlock } from '@typebot.io/forge-schemas'
import { ForgedBlock } from '@typebot.io/forge-repository/types'
type Props = {
block: ForgedBlock

View File

@@ -11,7 +11,7 @@ import { RepeatIcon, ChevronRightIcon } from '@/components/icons'
import { useDebouncedCallback } from 'use-debounce'
import { useForgedBlock } from '@/features/forge/hooks/useForgedBlock'
import { ForgedBlockIcon } from '@/features/forge/ForgedBlockIcon'
import { ForgedBlock } from '@typebot.io/forge-schemas'
import { ForgedBlock } from '@typebot.io/forge-repository/types'
import { TurnableIntoParam } from '@typebot.io/forge'
import { ZodObject } from 'zod'
import { BlockV6 } from '@typebot.io/schemas'
@@ -64,8 +64,8 @@ export const ForgedBlockTurnIntoMenu = ({ block, onTurnIntoClick }: Props) => {
>
{actionDef.turnableInto.map((params) => (
<TurnIntoMenuItem
key={params.blockType}
blockType={params.blockType}
key={params.blockId}
blockType={params.blockId as ForgedBlock['type']}
onClick={(blockSchema) => onTurnIntoClick(params, blockSchema)}
/>
))}

View File

@@ -14,7 +14,7 @@ import React, { useCallback, useEffect, useState } from 'react'
import { useRouter } from 'next/router'
import { trpc } from '@/lib/trpc'
import { useWorkspace } from '@/features/workspace/WorkspaceProvider'
import { ForgedBlockDefinition } from '@typebot.io/forge-schemas'
import { ForgedBlockDefinition } from '@typebot.io/forge-repository/types'
import { useToast } from '@/hooks/useToast'
type Props = Omit<ButtonProps, 'type'> & {
@@ -109,7 +109,7 @@ export const ForgedCredentialsDropdown = ({
isLoading={isLoading}
{...props}
>
Add {blockDef.auth.name}
Add {blockDef.auth?.name}
</Button>
)
}
@@ -130,7 +130,7 @@ export const ForgedCredentialsDropdown = ({
>
{currentCredential
? currentCredential.name
: `Select ${blockDef.auth.name}`}
: `Select ${blockDef.auth?.name}`}
</Text>
</MenuButton>
<MenuList>

View File

@@ -15,7 +15,7 @@ import {
} from '@chakra-ui/react'
import React, { useState } from 'react'
import { ZodObjectLayout } from '../zodLayouts/ZodObjectLayout'
import { ForgedBlockDefinition } from '@typebot.io/forge-schemas'
import { ForgedBlockDefinition } from '@typebot.io/forge-repository/types'
type Props = {
blockDef: ForgedBlockDefinition

View File

@@ -4,7 +4,10 @@ import { z } from '@typebot.io/forge/zod'
import { useMemo } from 'react'
import { ZodObjectLayout } from './ZodObjectLayout'
import { isDefined } from '@typebot.io/lib'
import { ForgedBlockDefinition, ForgedBlock } from '@typebot.io/forge-schemas'
import {
ForgedBlockDefinition,
ForgedBlock,
} from '@typebot.io/forge-repository/types'
type Props = {
blockDef?: ForgedBlockDefinition

View File

@@ -2,7 +2,10 @@ import { DropdownList } from '@/components/DropdownList'
import { z } from '@typebot.io/forge/zod'
import { ZodObjectLayout } from './ZodObjectLayout'
import { isDefined } from '@typebot.io/lib'
import { ForgedBlockDefinition, ForgedBlock } from '@typebot.io/forge-schemas'
import {
ForgedBlockDefinition,
ForgedBlock,
} from '@typebot.io/forge-repository/types'
/* eslint-disable @typescript-eslint/no-explicit-any */
export const ZodDiscriminatedUnionLayout = ({

View File

@@ -19,7 +19,10 @@ import {
} from '@chakra-ui/react'
import { VariableSearchInput } from '@/components/inputs/VariableSearchInput'
import { DropdownList } from '@/components/DropdownList'
import { ForgedBlockDefinition, ForgedBlock } from '@typebot.io/forge-schemas'
import {
ForgedBlockDefinition,
ForgedBlock,
} from '@typebot.io/forge-repository/types'
import { PrimitiveList } from '@/components/PrimitiveList'
import { SwitchWithLabel } from '@/components/inputs/SwitchWithLabel'
import { CodeEditor } from '@/components/inputs/CodeEditor'

View File

@@ -14,7 +14,10 @@ import { ZodLayoutMetadata } from '@typebot.io/forge/zod'
import { ReactNode } from 'react'
import { ZodTypeAny } from 'zod'
import { ZodFieldLayout } from './ZodFieldLayout'
import { ForgedBlockDefinition, ForgedBlock } from '@typebot.io/forge-schemas'
import {
ForgedBlockDefinition,
ForgedBlock,
} from '@typebot.io/forge-repository/types'
import { getZodInnerSchema } from '../../helpers/getZodInnerSchema'
export const ZodObjectLayout = ({

View File

@@ -1,22 +1,16 @@
import { useMemo } from 'react'
import { forgedBlockSchemas, forgedBlocks } from '@typebot.io/forge-schemas'
import { enabledBlocks } from '@typebot.io/forge-repository'
import { forgedBlocks } from '@typebot.io/forge-repository/definitions'
import { forgedBlockSchemas } from '@typebot.io/forge-repository/schemas'
import { BlockV6 } from '@typebot.io/schemas'
import { isForgedBlockType } from '@typebot.io/schemas/features/blocks/forged/helpers'
export const useForgedBlock = (blockType: BlockV6['type'], action?: string) =>
useMemo(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((enabledBlocks as any).includes(blockType) === false) return {}
const blockDef = forgedBlocks.find(
(b) => enabledBlocks.includes(b.id) && b.id === blockType
)
if (!isForgedBlockType(blockType)) return {}
const blockDef = forgedBlocks[blockType]
return {
blockDef,
blockSchema: forgedBlockSchemas.find(
(b) =>
enabledBlocks.includes(b.shape.type.value) &&
b.shape.type.value === blockType
),
blockSchema: forgedBlockSchemas[blockType],
actionDef: action
? blockDef?.actions.find((a) => a.name === action)
: undefined,

View File

@@ -194,7 +194,7 @@ export const BlockNode = ({
indices,
targetBlockSchema.parse({
...block,
type: turnIntoParams.blockType,
type: turnIntoParams.blockId,
options: {
...convertedBlockOptions,
credentialsId: undefined,

View File

@@ -1,4 +1,4 @@
import { ForgedBlockDefinition } from '@typebot.io/forge-schemas'
import { ForgedBlockDefinition } from '@typebot.io/forge-repository/types'
import { BlockWithOptions } from '@typebot.io/schemas'
import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/constants'
import { IntegrationBlockType } from '@typebot.io/schemas/features/blocks/integrations/constants'

View File

@@ -1,5 +1,3 @@
import { forgedBlockSchemas } from '@typebot.io/forge-schemas'
import { enabledBlocks } from '@typebot.io/forge-repository'
import prisma from '@typebot.io/lib/prisma'
import { Plan } from '@typebot.io/prisma'
import { Block, Typebot } from '@typebot.io/schemas'
@@ -51,25 +49,6 @@ const sanitizeBlock =
async (block: Block): Promise<Block> => {
if (!('options' in block) || !block.options) return block
if (enabledBlocks.includes(block.type as (typeof enabledBlocks)[number])) {
const schema = forgedBlockSchemas.find(
(s) => s.shape.type.value === block.type
)
if (!schema)
throw new Error(
`Integration block schema not found for block type ${block.type}`
)
return schema.parse({
...block,
options: {
...block.options,
credentialsId: await sanitizeCredentialsId(workspaceId)(
block.options.credentialsId
),
},
})
}
if (!('credentialsId' in block.options) || !block.options.credentialsId)
return block