2
0

♻️ Remove @typebot.io/schemas from @typebot.io/lib

This commit is contained in:
Baptiste Arnaud
2024-03-15 16:32:29 +01:00
parent b53242ce6a
commit 5073be2439
186 changed files with 809 additions and 581 deletions

View File

@ -1,92 +0,0 @@
import { Prisma, PrismaClient } from '@typebot.io/prisma'
import { Block, Typebot } from '@typebot.io/schemas'
import { deleteFilesFromBucket } from '../../s3/deleteFilesFromBucket'
import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/constants'
type ArchiveResultsProps = {
typebot: Pick<Typebot, 'groups'>
resultsFilter?: Omit<Prisma.ResultWhereInput, 'typebotId'> & {
typebotId: string
}
}
export const archiveResults =
(prisma: PrismaClient) =>
async ({ typebot, resultsFilter }: ArchiveResultsProps) => {
const batchSize = 100
const fileUploadBlockIds = typebot.groups
.flatMap<Block>((group) => group.blocks)
.filter((block) => block.type === InputBlockType.FILE)
.map((block) => block.id)
let currentTotalResults = 0
const resultsCount = await prisma.result.count({
where: {
...resultsFilter,
OR: [{ isArchived: false }, { isArchived: null }],
},
})
if (resultsCount === 0) return { success: true }
let progress = 0
do {
progress += batchSize
console.log(`Archiving ${progress} / ${resultsCount} results...`)
const resultsToDelete = await prisma.result.findMany({
where: {
...resultsFilter,
OR: [{ isArchived: false }, { isArchived: null }],
},
select: {
id: true,
},
take: batchSize,
})
if (resultsToDelete.length === 0) break
currentTotalResults = resultsToDelete.length
const resultIds = resultsToDelete.map((result) => result.id)
if (fileUploadBlockIds.length > 0) {
const filesToDelete = await prisma.answer.findMany({
where: {
resultId: { in: resultIds },
blockId: { in: fileUploadBlockIds },
},
})
if (filesToDelete.length > 0)
await deleteFilesFromBucket({
urls: filesToDelete.flatMap((a) => a.content.split(', ')),
})
}
await prisma.$transaction([
prisma.log.deleteMany({
where: {
resultId: { in: resultIds },
},
}),
prisma.answer.deleteMany({
where: {
resultId: { in: resultIds },
},
}),
prisma.result.updateMany({
where: {
id: { in: resultIds },
},
data: {
isArchived: true,
variables: [],
},
}),
])
} while (currentTotalResults >= batchSize)
return { success: true }
}

View File

@ -1,167 +0,0 @@
import { Plan } from '@typebot.io/prisma'
import type { Stripe } from 'stripe'
export const prices = {
[Plan.STARTER]: 39,
[Plan.PRO]: 89,
} as const
export const chatsLimits = {
[Plan.FREE]: 200,
[Plan.STARTER]: 2000,
[Plan.PRO]: 10000,
} as const
export const seatsLimits = {
[Plan.FREE]: 1,
[Plan.OFFERED]: 1,
[Plan.STARTER]: 2,
[Plan.PRO]: 5,
[Plan.LIFETIME]: 8,
} as const
export const starterChatTiers = [
{
up_to: 2000,
flat_amount: 0,
},
{
up_to: 2500,
flat_amount: 1000,
},
{
up_to: 3000,
flat_amount: 2000,
},
{
up_to: 3500,
flat_amount: 3000,
},
{
up_to: 4000,
flat_amount: 4000,
},
{
up_to: 'inf',
unit_amount: 2,
},
] satisfies Stripe.PriceCreateParams.Tier[]
export const proChatTiers = [
{
up_to: 10000,
flat_amount: 0,
},
{
up_to: 15000,
flat_amount: 5000,
},
{
up_to: 20000,
flat_amount: 9500,
},
{
up_to: 30000,
flat_amount: 18000,
},
{
up_to: 40000,
flat_amount: 26000,
},
{
up_to: 50000,
flat_amount: 33500,
},
{
up_to: 60000,
flat_amount: 40700,
},
{
up_to: 70000,
flat_amount: 47700,
},
{
up_to: 80000,
flat_amount: 54500,
},
{
up_to: 90000,
flat_amount: 61100,
},
{
up_to: 100000,
flat_amount: 67500,
},
{
up_to: 120000,
flat_amount: 79900,
},
{
up_to: 140000,
flat_amount: 91900,
},
{
up_to: 160000,
flat_amount: 103700,
},
{
up_to: 180000,
flat_amount: 115300,
},
{
up_to: 200000,
flat_amount: 126700,
},
{
up_to: 300000,
flat_amount: 181700,
},
{
up_to: 400000,
flat_amount: 234700,
},
{
up_to: 500000,
flat_amount: 285700,
},
{
up_to: 600000,
flat_amount: 335700,
},
{
up_to: 700000,
flat_amount: 384700,
},
{
up_to: 800000,
flat_amount: 432700,
},
{
up_to: 900000,
flat_amount: 479700,
},
{
up_to: 1000000,
flat_amount: 525700,
},
{
up_to: 1200000,
flat_amount: 617100,
},
{
up_to: 1400000,
flat_amount: 707900,
},
{
up_to: 1600000,
flat_amount: 797900,
},
{
up_to: 1800000,
flat_amount: 887300,
},
{
up_to: 'inf',
unit_amount_decimal: '0.442',
},
] satisfies Stripe.PriceCreateParams.Tier[]

View File

@ -1,21 +0,0 @@
import { guessIfUserIsEuropean } from './guessIfUserIsEuropean'
type FormatPriceParams = {
currency?: 'eur' | 'usd'
maxFractionDigits?: number
}
export const formatPrice = (
price: number,
{ currency, maxFractionDigits = 0 }: FormatPriceParams = {
maxFractionDigits: 0,
}
) => {
const isEuropean = guessIfUserIsEuropean()
const formatter = new Intl.NumberFormat(isEuropean ? 'fr-FR' : 'en-US', {
style: 'currency',
currency: currency?.toUpperCase() ?? (isEuropean ? 'EUR' : 'USD'),
maximumFractionDigits: maxFractionDigits,
})
return formatter.format(price)
}

View File

@ -1,19 +0,0 @@
import { Plan } from '@typebot.io/prisma'
import { chatsLimits } from './constants'
import { Workspace } from '@typebot.io/schemas'
export const getChatsLimit = ({
plan,
customChatsLimit,
}: Pick<Workspace, 'plan'> & {
customChatsLimit?: Workspace['customChatsLimit']
}) => {
if (
plan === Plan.UNLIMITED ||
plan === Plan.LIFETIME ||
plan === Plan.OFFERED
)
return 'inf'
if (plan === Plan.CUSTOM) return customChatsLimit ?? 'inf'
return chatsLimits[plan]
}

View File

@ -1,12 +0,0 @@
import { Workspace } from '@typebot.io/schemas'
import { seatsLimits } from './constants'
import { Plan } from '@typebot.io/prisma'
export const getSeatsLimit = ({
plan,
customSeatsLimit,
}: Pick<Workspace, 'plan' | 'customSeatsLimit'>) => {
if (plan === Plan.UNLIMITED) return 'inf'
if (plan === Plan.CUSTOM) return customSeatsLimit ? customSeatsLimit : 'inf'
return seatsLimits[plan]
}

View File

@ -1,56 +0,0 @@
const europeanUnionCountryCodes = [
'AT',
'BE',
'BG',
'CY',
'CZ',
'DE',
'DK',
'EE',
'ES',
'FI',
'FR',
'GR',
'HR',
'HU',
'IE',
'IT',
'LT',
'LU',
'LV',
'MT',
'NL',
'PL',
'PT',
'RO',
'SE',
'SI',
'SK',
]
const europeanUnionExclusiveLanguageCodes = [
'fr',
'de',
'it',
'el',
'pl',
'fi',
'nl',
'hr',
'cs',
'hu',
'ro',
'sl',
'sv',
'bg',
]
export const guessIfUserIsEuropean = () => {
if (typeof window === 'undefined') return false
return window.navigator.languages.some((language) => {
const [languageCode, countryCode] = language.split('-')
return countryCode
? europeanUnionCountryCodes.includes(countryCode)
: europeanUnionExclusiveLanguageCodes.includes(languageCode)
})
}

View File

@ -1,24 +0,0 @@
import { Block, Group } from '@typebot.io/schemas'
export const getBlockById = (
blockId: string,
groups: Group[]
): { block: Block; group: Group; blockIndex: number; groupIndex: number } => {
for (let groupIndex = 0; groupIndex < groups.length; groupIndex++) {
for (
let blockIndex = 0;
blockIndex < (groups.at(groupIndex)?.blocks?.length ?? 0);
blockIndex++
) {
if (groups.at(groupIndex)?.blocks?.at(blockIndex)?.id === blockId) {
return {
block: groups[groupIndex].blocks[blockIndex],
group: groups[groupIndex],
blockIndex,
groupIndex,
}
}
}
}
throw new Error(`Block with id ${blockId} was not found`)
}

View File

@ -1,6 +1,5 @@
import { env } from '@typebot.io/env'
import { Credentials as CredentialsFromDb } from '@typebot.io/prisma'
import { GoogleSheetsCredentials } from '@typebot.io/schemas'
import { decrypt } from './api/encryption/decrypt'
import { encrypt } from './api/encryption/encrypt'
import prisma from './prisma'
@ -14,10 +13,7 @@ export const getAuthenticatedGoogleClient = async (
where: { id: credentialsId },
})) as CredentialsFromDb | undefined
if (!credentials) return
const data = (await decrypt(
credentials.data,
credentials.iv
)) as GoogleSheetsCredentials['data']
const data = await decrypt(credentials.data, credentials.iv)
const oauth2Client = new OAuth2Client(
env.GOOGLE_CLIENT_ID,
@ -30,17 +26,14 @@ export const getAuthenticatedGoogleClient = async (
}
const updateTokens =
(
credentialsId: string,
existingCredentials: GoogleSheetsCredentials['data']
) =>
(credentialsId: string, existingCredentials: any) =>
async (credentials: Credentials) => {
if (
isDefined(existingCredentials.id_token) &&
credentials.id_token !== existingCredentials.id_token
)
return
const newCredentials: GoogleSheetsCredentials['data'] = {
const newCredentials = {
...existingCredentials,
expiry_date: credentials.expiry_date,
access_token: credentials.access_token,

View File

@ -1,15 +0,0 @@
import { PublicTypebot, PublicTypebotV6 } from '@typebot.io/schemas'
import { migrateTypebotFromV3ToV4 } from './migrateTypebotFromV3ToV4'
import { migrateTypebotFromV5ToV6 } from './migrateTypebotFromV5ToV6'
export const migrateTypebot = async (
typebot: PublicTypebot
): Promise<PublicTypebotV6> => {
if (typebot.version === '6') return typebot
let migratedTypebot: any = typebot
if (migratedTypebot.version === '3')
migratedTypebot = await migrateTypebotFromV3ToV4(typebot)
if (migratedTypebot.version === '4' || migratedTypebot.version === '5')
migratedTypebot = migrateTypebotFromV5ToV6(migratedTypebot)
return migratedTypebot
}

View File

@ -1,30 +0,0 @@
import {
PublicTypebot,
PublicTypebotV6,
Typebot,
TypebotV6,
} from '@typebot.io/schemas'
import { migrateTypebotFromV3ToV4 } from './migrateTypebotFromV3ToV4'
import { migrateTypebotFromV5ToV6 } from './migrateTypebotFromV5ToV6'
export const migrateTypebot = async (typebot: Typebot): Promise<TypebotV6> => {
if (typebot.version === '6') return typebot
let migratedTypebot: any = typebot
if (migratedTypebot.version === '3')
migratedTypebot = await migrateTypebotFromV3ToV4(typebot)
if (migratedTypebot.version === '4' || migratedTypebot.version === '5')
migratedTypebot = migrateTypebotFromV5ToV6(migratedTypebot)
return migratedTypebot
}
export const migratePublicTypebot = async (
typebot: PublicTypebot
): Promise<PublicTypebotV6> => {
if (typebot.version === '6') return typebot
let migratedTypebot: any = typebot
if (migratedTypebot.version === '3')
migratedTypebot = await migrateTypebotFromV3ToV4(typebot)
if (migratedTypebot.version === '4' || migratedTypebot.version === '5')
migratedTypebot = migrateTypebotFromV5ToV6(migratedTypebot)
return migratedTypebot
}

View File

@ -1,69 +0,0 @@
import { Webhook as WebhookFromDb } from '@typebot.io/prisma'
import {
BlockV5,
PublicTypebotV5,
TypebotV5,
HttpRequest,
} from '@typebot.io/schemas'
import { isWebhookBlock, isDefined } from '../utils'
import prisma from '../prisma'
import {
HttpMethod,
defaultWebhookAttributes,
} from '@typebot.io/schemas/features/blocks/integrations/webhook/constants'
export const migrateTypebotFromV3ToV4 = async (
typebot: TypebotV5 | PublicTypebotV5
): Promise<Omit<TypebotV5 | PublicTypebotV5, 'version'> & { version: '4' }> => {
if (typebot.version === '4')
return typebot as Omit<TypebotV5, 'version'> & { version: '4' }
const webhookBlocks = typebot.groups
.flatMap((group) => group.blocks)
.filter(isWebhookBlock)
const webhooks = await prisma.webhook.findMany({
where: {
id: {
in: webhookBlocks
.map((block) => ('webhookId' in block ? block.webhookId : undefined))
.filter(isDefined),
},
},
})
return {
...typebot,
version: '4',
groups: typebot.groups.map((group) => ({
...group,
blocks: group.blocks.map(migrateWebhookBlock(webhooks)),
})),
}
}
const migrateWebhookBlock =
(webhooks: WebhookFromDb[]) =>
(block: BlockV5): BlockV5 => {
if (!isWebhookBlock(block)) return block
const webhook = webhooks.find((webhook) => webhook.id === block.webhookId)
return {
...block,
webhookId: undefined,
options: {
...block.options,
webhook: webhook
? {
id: webhook.id,
url: webhook.url ?? undefined,
method:
(webhook.method as HttpRequest['method']) ?? HttpMethod.POST,
headers: (webhook.headers as HttpRequest['headers']) ?? [],
queryParams:
(webhook.queryParams as HttpRequest['headers']) ?? [],
body: webhook.body ?? undefined,
}
: {
...defaultWebhookAttributes,
id: 'webhookId' in block ? block.webhookId ?? '' : '',
},
},
}
}

View File

@ -1,125 +0,0 @@
import {
BlockV5,
BlockV6,
GoogleSheetsBlockV5,
GoogleSheetsBlockV6,
PublicTypebotV5,
PublicTypebotV6,
TypebotV5,
TypebotV6,
} from '@typebot.io/schemas'
import { IntegrationBlockType } from '@typebot.io/schemas/features/blocks/integrations/constants'
import { GoogleSheetsAction } from '@typebot.io/schemas/features/blocks/integrations/googleSheets/constants'
import { ComparisonOperators } from '@typebot.io/schemas/features/blocks/logic/condition/constants'
import { createId } from '@paralleldrive/cuid2'
import { EventType } from '@typebot.io/schemas/features/events/constants'
import { byId } from '../utils'
export const migrateTypebotFromV5ToV6 = async (
typebot: TypebotV5 | PublicTypebotV5
): Promise<TypebotV6 | PublicTypebotV6> => {
const startGroup = typebot.groups.find((group) =>
group.blocks.some((b) => b.type === 'start')
)
if (!startGroup) throw new Error('Start group not found')
const startBlock = startGroup?.blocks.find((b) => b.type === 'start')
if (!startBlock) throw new Error('Start block not found')
const startOutgoingEdge = typebot.edges.find(byId(startBlock.outgoingEdgeId))
return {
...typebot,
groups: migrateGroups(
typebot.groups.filter((g) => g.blocks.some((b) => b.type !== 'start'))
),
version: '6',
events: [
{
id: startGroup.id,
type: EventType.START,
graphCoordinates: startGroup.graphCoordinates,
outgoingEdgeId: startBlock.outgoingEdgeId,
},
],
edges: startOutgoingEdge
? [
{
...startOutgoingEdge,
from: {
eventId: startGroup.id,
},
},
...typebot.edges.filter((e) => e.id !== startOutgoingEdge.id),
]
: typebot.edges,
}
}
const migrateGroups = (groups: TypebotV5['groups']): TypebotV6['groups'] =>
groups.map((group) => ({
...group,
blocks: migrateBlocksFromV1ToV2(group.blocks),
}))
const migrateBlocksFromV1ToV2 = (
blocks: TypebotV5['groups'][0]['blocks']
): BlockV6[] =>
(
blocks.filter((block) => block.type !== 'start') as Exclude<
BlockV5,
{ type: 'start' }
>[]
).map((block) => {
if (block.type === IntegrationBlockType.GOOGLE_SHEETS) {
return {
...block,
options: migrateGoogleSheetsOptions(block.options),
}
}
return block
})
const migrateGoogleSheetsOptions = (
options: GoogleSheetsBlockV5['options']
): GoogleSheetsBlockV6['options'] => {
if (!options) return
if (options.action === GoogleSheetsAction.GET) {
if (options.filter || !options.referenceCell) return options
return {
...options,
filter: {
comparisons: [
{
id: createId(),
column: options.referenceCell?.column,
comparisonOperator: ComparisonOperators.EQUAL,
value: options.referenceCell?.value,
},
],
},
}
}
if (options.action === GoogleSheetsAction.INSERT_ROW) {
return options
}
if (options.action === GoogleSheetsAction.UPDATE_ROW) {
if (options.filter || !options.referenceCell) return options
return {
...options,
filter: {
comparisons: [
{
id: createId(),
column: options.referenceCell?.column,
comparisonOperator: ComparisonOperators.EQUAL,
value: options.referenceCell?.value,
},
],
},
}
}
return options
}

View File

@ -1,5 +1,4 @@
import { User } from '@typebot.io/prisma'
import { graphGestureNotficationKey } from '@typebot.io/schemas/features/user/constants'
export const mockedUser: User = {
id: 'userId',
@ -15,7 +14,7 @@ export const mockedUser: User = {
onboardingCategories: [],
updatedAt: new Date('2022-01-01'),
displayedInAppNotifications: {
[graphGestureNotficationKey]: true,
['graphGestureNotification']: true,
},
referral: null,
}

View File

@ -11,7 +11,6 @@
"@typebot.io/env": "workspace:*",
"@typebot.io/forge-repository": "workspace:*",
"@typebot.io/prisma": "workspace:*",
"@typebot.io/schemas": "workspace:*",
"@typebot.io/tsconfig": "workspace:*",
"@types/escape-html": "^1.0.4",
"@types/nodemailer": "6.4.8",

View File

@ -1,83 +0,0 @@
import { VideoBubbleBlock } from '@typebot.io/schemas'
import {
VideoBubbleContentType,
gumletRegex,
horizontalVideoSuggestionSize,
oneDriveRegex,
tiktokRegex,
verticalVideoSuggestionSize,
vimeoRegex,
youtubeRegex,
} from '@typebot.io/schemas/features/blocks/bubbles/video/constants'
export const parseVideoUrl = (
url: string
): {
type: VideoBubbleContentType
url: string
id?: string
videoSizeSuggestion?: Pick<
NonNullable<VideoBubbleBlock['content']>,
'aspectRatio' | 'maxWidth'
>
} => {
if (youtubeRegex.test(url)) {
const match = url.match(youtubeRegex)
const id = match?.at(2) ?? match?.at(3)
const parsedUrl = match?.at(0) ?? url
if (!id) return { type: VideoBubbleContentType.URL, url: parsedUrl }
return {
type: VideoBubbleContentType.YOUTUBE,
url: parsedUrl,
id,
videoSizeSuggestion: url.includes('shorts')
? verticalVideoSuggestionSize
: horizontalVideoSuggestionSize,
}
}
if (vimeoRegex.test(url)) {
const match = url.match(vimeoRegex)
const id = match?.at(1)
const parsedUrl = match?.at(0) ?? url
if (!id) return { type: VideoBubbleContentType.URL, url: parsedUrl }
return {
type: VideoBubbleContentType.VIMEO,
url: parsedUrl,
id,
videoSizeSuggestion: horizontalVideoSuggestionSize,
}
}
if (tiktokRegex.test(url)) {
const match = url.match(tiktokRegex)
const id = url.match(tiktokRegex)?.at(1)
const parsedUrl = match?.at(0) ?? url
if (!id) return { type: VideoBubbleContentType.URL, url: parsedUrl }
return {
type: VideoBubbleContentType.TIKTOK,
url: parsedUrl,
id,
videoSizeSuggestion: verticalVideoSuggestionSize,
}
}
if (gumletRegex.test(url)) {
const match = url.match(gumletRegex)
const id = match?.at(1)
const parsedUrl = match?.at(0) ?? url
if (!id) return { type: VideoBubbleContentType.URL, url: parsedUrl }
return {
type: VideoBubbleContentType.GUMLET,
url: parsedUrl,
id,
videoSizeSuggestion: horizontalVideoSuggestionSize,
}
}
if (oneDriveRegex.test(url)) {
const match = url.match(oneDriveRegex)
const parsedUrl = match?.at(0) ?? url
return {
type: VideoBubbleContentType.URL,
url: parsedUrl.replace('/embed', '/download'),
}
}
return { type: VideoBubbleContentType.URL, url }
}

View File

@ -1,32 +0,0 @@
import { PlaywrightTestConfig } from '@playwright/test'
import path from 'path'
export const playwrightBaseConfig: PlaywrightTestConfig = {
globalSetup: require.resolve(path.join(__dirname, 'globalSetup')),
timeout: process.env.CI ? 50 * 1000 : 40 * 1000,
expect: {
timeout: process.env.CI ? 10 * 1000 : 5 * 1000,
},
retries: 0,
workers: process.env.CI ? 2 : 3,
reporter: [
[process.env.CI ? 'github' : 'list'],
['html', { outputFolder: 'src/test/reporters' }],
],
maxFailures: process.env.CI ? 10 : undefined,
webServer: process.env.CI
? {
command: 'pnpm run start',
timeout: 60_000,
reuseExistingServer: true,
}
: undefined,
outputDir: './src/test/results',
use: {
trace: 'on-first-retry',
video: 'retain-on-failure',
locale: 'en-US',
browserName: 'chromium',
viewport: { width: 1400, height: 1000 },
},
}

View File

@ -1,236 +0,0 @@
import {
Plan,
Prisma,
PrismaClient,
User,
Workspace,
WorkspaceRole,
} from '@typebot.io/prisma'
import { createId } from '@paralleldrive/cuid2'
import { Typebot, TypebotV6, HttpRequest } from '@typebot.io/schemas'
import { readFileSync } from 'fs'
import { proWorkspaceId, userId } from './databaseSetup'
import {
parseTestTypebot,
parseTypebotToPublicTypebot,
} from './databaseHelpers'
const prisma = new PrismaClient()
type CreateFakeResultsProps = {
typebotId: string
count: number
customResultIdPrefix?: string
isChronological?: boolean
}
export const injectFakeResults = async ({
count,
customResultIdPrefix,
typebotId,
isChronological,
}: CreateFakeResultsProps) => {
const resultIdPrefix = customResultIdPrefix ?? createId()
await prisma.result.createMany({
data: [
...Array.from(Array(count)).map((_, idx) => {
const today = new Date()
const rand = Math.random()
return {
id: `${resultIdPrefix}-result${idx}`,
typebotId,
createdAt: isChronological
? new Date(
today.setTime(today.getTime() + 1000 * 60 * 60 * 24 * idx)
)
: new Date(),
isCompleted: rand > 0.5,
hasStarted: true,
variables: [],
} satisfies Prisma.ResultCreateManyInput
}),
],
})
return createAnswers({ resultIdPrefix, count })
}
const createAnswers = ({
count,
resultIdPrefix,
}: { resultIdPrefix: string } & Pick<CreateFakeResultsProps, 'count'>) => {
return prisma.answer.createMany({
data: [
...Array.from(Array(count)).map((_, idx) => ({
resultId: `${resultIdPrefix}-result${idx}`,
content: `content${idx}`,
blockId: 'block1',
groupId: 'group1',
})),
],
})
}
export const importTypebotInDatabase = async (
path: string,
updates?: Partial<Typebot>
) => {
const typebotFile = JSON.parse(readFileSync(path).toString())
const typebot = {
events: null,
...typebotFile,
workspaceId: proWorkspaceId,
...updates,
}
await prisma.typebot.create({
data: parseCreateTypebot(typebot),
})
return prisma.publicTypebot.create({
data: {
...parseTypebotToPublicTypebot(
updates?.id ? `${updates?.id}-public` : 'publicBot',
typebot
),
events: typebot.events === null ? Prisma.DbNull : typebot.events,
},
})
}
export const deleteWorkspaces = async (workspaceIds: string[]) => {
await prisma.workspace.deleteMany({
where: { id: { in: workspaceIds } },
})
}
export const deleteTypebots = async (typebotIds: string[]) => {
await prisma.typebot.deleteMany({
where: { id: { in: typebotIds } },
})
}
export const deleteCredentials = async (credentialIds: string[]) => {
await prisma.credentials.deleteMany({
where: { id: { in: credentialIds } },
})
}
export const deleteWebhooks = async (webhookIds: string[]) => {
await prisma.webhook.deleteMany({
where: { id: { in: webhookIds } },
})
}
export const createWorkspaces = async (workspaces: Partial<Workspace>[]) => {
const workspaceIds = workspaces.map((workspace) => workspace.id ?? createId())
await prisma.workspace.createMany({
data: workspaces.map((workspace, index) => ({
id: workspaceIds[index],
name: 'Free workspace',
plan: Plan.FREE,
...workspace,
})),
})
await prisma.memberInWorkspace.createMany({
data: workspaces.map((_, index) => ({
userId,
workspaceId: workspaceIds[index],
role: WorkspaceRole.ADMIN,
})),
})
return workspaceIds
}
export const updateUser = (data: Partial<User>) =>
prisma.user.update({
data: {
...data,
onboardingCategories: data.onboardingCategories ?? [],
displayedInAppNotifications:
data.displayedInAppNotifications ?? Prisma.DbNull,
},
where: {
id: userId,
},
})
export const createWebhook = async (
typebotId: string,
webhookProps?: Partial<HttpRequest>
) => {
try {
await prisma.webhook.delete({ where: { id: 'webhook1' } })
} catch {}
return prisma.webhook.create({
data: {
method: 'GET',
typebotId,
id: 'webhook1',
...webhookProps,
queryParams: webhookProps?.queryParams ?? [],
headers: webhookProps?.headers ?? [],
},
})
}
export const createTypebots = async (partialTypebots: Partial<TypebotV6>[]) => {
const typebotsWithId = partialTypebots.map((typebot) => {
const typebotId = typebot.id ?? createId()
return {
...typebot,
id: typebotId,
publicId: typebot.publicId ?? typebotId + '-public',
}
})
await prisma.typebot.createMany({
data: typebotsWithId.map(parseTestTypebot).map(parseCreateTypebot),
})
return prisma.publicTypebot.createMany({
data: typebotsWithId.map((t) => ({
...parseTypebotToPublicTypebot(t.publicId, parseTestTypebot(t)),
})) as any,
})
}
export const updateTypebot = async (
partialTypebot: Partial<Typebot> & { id: string }
) => {
await prisma.typebot.updateMany({
where: { id: partialTypebot.id },
data: parseUpdateTypebot(partialTypebot),
})
return prisma.publicTypebot.updateMany({
where: { typebotId: partialTypebot.id },
data: {
...partialTypebot,
events:
partialTypebot.events === null ? Prisma.DbNull : partialTypebot.events,
},
})
}
export const updateWorkspace = async (
id: string,
data: Prisma.WorkspaceUncheckedUpdateManyInput
) => {
await prisma.workspace.updateMany({
where: { id: proWorkspaceId },
data,
})
}
export const parseCreateTypebot = (typebot: Typebot) => ({
...typebot,
resultsTablePreferences:
typebot.resultsTablePreferences === null
? Prisma.DbNull
: typebot.resultsTablePreferences,
events: typebot.events === null ? Prisma.DbNull : typebot.events,
})
const parseUpdateTypebot = (typebot: Partial<Typebot>) => ({
...typebot,
resultsTablePreferences:
typebot.resultsTablePreferences === null
? Prisma.DbNull
: typebot.resultsTablePreferences,
events: typebot.events === null ? Prisma.DbNull : typebot.events,
})

View File

@ -1,129 +0,0 @@
import { createId } from '@paralleldrive/cuid2'
import {
BlockV5,
BlockV6,
Group,
PublicTypebot,
Typebot,
TypebotV6,
} from '@typebot.io/schemas'
import { isDefined } from '../utils'
import { proWorkspaceId } from './databaseSetup'
import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/constants'
import { EventType } from '@typebot.io/schemas/features/events/constants'
export const parseTestTypebot = (partialTypebot: Partial<Typebot>): Typebot => {
const version = partialTypebot.version ?? ('3' as any)
return {
id: createId(),
version,
workspaceId: proWorkspaceId,
folderId: null,
name: 'My typebot',
theme: {},
settings: {},
publicId: null,
updatedAt: new Date(),
createdAt: new Date(),
customDomain: null,
icon: null,
selectedThemeTemplateId: null,
isArchived: false,
isClosed: false,
resultsTablePreferences: null,
whatsAppCredentialsId: null,
riskLevel: null,
events:
version === '6'
? [
{
id: 'group1',
type: EventType.START,
graphCoordinates: { x: 0, y: 0 },
outgoingEdgeId: 'edge1',
},
]
: null,
variables: [{ id: 'var1', name: 'var1' }],
...partialTypebot,
edges: [
{
id: 'edge1',
from: { blockId: 'block0' },
to: { groupId: 'group1' },
},
],
groups: (version === '6'
? partialTypebot.groups ?? []
: [
{
id: 'group0',
title: 'Group #0',
blocks: [
{
id: 'block0',
type: 'start',
label: 'Start',
outgoingEdgeId: 'edge1',
},
],
graphCoordinates: { x: 0, y: 0 },
},
...(partialTypebot.groups ?? []),
]) as any[],
}
}
export const parseTypebotToPublicTypebot = (
id: string,
typebot: Typebot
): Omit<PublicTypebot, 'createdAt' | 'updatedAt'> => ({
id,
version: typebot.version,
groups: typebot.groups,
typebotId: typebot.id,
theme: typebot.theme,
settings: typebot.settings,
variables: typebot.variables,
edges: typebot.edges,
events: typebot.events,
})
type Options = {
withGoButton?: boolean
}
export const parseDefaultGroupWithBlock = (
block: Partial<BlockV6>,
options?: Options
): Pick<TypebotV6, 'groups'> => ({
groups: [
{
graphCoordinates: { x: 200, y: 200 },
id: 'group1',
blocks: [
options?.withGoButton
? {
id: 'block1',
groupId: 'group1',
type: InputBlockType.CHOICE,
items: [
{
id: 'item1',
blockId: 'block1',
content: 'Go',
},
],
options: {},
}
: undefined,
{
id: 'block2',
...block,
} as BlockV5,
].filter(isDefined) as BlockV6[],
title: 'Group #1',
},
],
})

View File

@ -1,191 +0,0 @@
import {
GraphNavigation,
Plan,
PrismaClient,
WorkspaceRole,
} from '@typebot.io/prisma'
import { encrypt } from '../api/encryption/encrypt'
const prisma = new PrismaClient()
export const apiToken = 'jirowjgrwGREHE'
export const userId = 'userId'
export const otherUserId = 'otherUserId'
export const proWorkspaceId = 'proWorkspace'
export const freeWorkspaceId = 'freeWorkspace'
export const starterWorkspaceId = 'starterWorkspace'
export const lifetimeWorkspaceId = 'lifetimeWorkspaceId'
export const customWorkspaceId = 'customWorkspaceId'
const setupWorkspaces = async () => {
await prisma.workspace.createMany({
data: [
{
id: freeWorkspaceId,
name: 'Free workspace',
plan: Plan.FREE,
},
{
id: starterWorkspaceId,
name: 'Starter workspace',
stripeId: 'cus_LnPDugJfa18N41',
plan: Plan.STARTER,
},
{
id: proWorkspaceId,
name: 'Pro workspace',
plan: Plan.PRO,
},
{
id: lifetimeWorkspaceId,
name: 'Lifetime workspace',
plan: Plan.LIFETIME,
},
{
id: customWorkspaceId,
name: 'Custom workspace',
plan: Plan.CUSTOM,
customChatsLimit: 100000,
customStorageLimit: 50,
customSeatsLimit: 20,
},
],
})
}
export const setupUsers = async () => {
await prisma.user.create({
data: {
id: userId,
email: 'user@email.com',
name: 'John Doe',
graphNavigation: GraphNavigation.TRACKPAD,
onboardingCategories: [],
apiTokens: {
createMany: {
data: [
{
name: 'Token 1',
token: apiToken,
createdAt: new Date(2022, 1, 1),
},
{
name: 'Github',
token: 'jirowjgrwGREHEgdrgithub',
createdAt: new Date(2022, 1, 2),
},
{
name: 'N8n',
token: 'jirowjgrwGREHrgwhrwn8n',
createdAt: new Date(2022, 1, 3),
},
],
},
},
},
})
await prisma.user.create({
data: {
id: otherUserId,
email: 'other-user@email.com',
name: 'James Doe',
onboardingCategories: [],
},
})
return prisma.memberInWorkspace.createMany({
data: [
{
role: WorkspaceRole.ADMIN,
userId,
workspaceId: freeWorkspaceId,
},
{
role: WorkspaceRole.ADMIN,
userId,
workspaceId: starterWorkspaceId,
},
{
role: WorkspaceRole.ADMIN,
userId,
workspaceId: proWorkspaceId,
},
{
role: WorkspaceRole.ADMIN,
userId,
workspaceId: lifetimeWorkspaceId,
},
{
role: WorkspaceRole.ADMIN,
userId,
workspaceId: customWorkspaceId,
},
],
})
}
const setupCredentials = async () => {
const { encryptedData, iv } = await encrypt({
expiry_date: 1642441058842,
access_token:
'ya29.A0ARrdaM--PV_87ebjywDJpXKb77NBFJl16meVUapYdfNv6W6ZzqqC47fNaPaRjbDbOIIcp6f49cMaX5ndK9TAFnKwlVqz3nrK9nLKqgyDIhYsIq47smcAIZkK56SWPx3X3DwAFqRu2UPojpd2upWwo-3uJrod',
// This token is linked to a test Google account (typebot.test.user@gmail.com)
refresh_token:
'1//039xWRt8YaYa3CgYIARAAGAMSNwF-L9Iru9FyuTrDSa7lkSceggPho83kJt2J29G69iEhT1C6XV1vmo6bQS9puL_R2t8FIwR3gek',
})
return prisma.credentials.createMany({
data: [
{
name: 'pro-user@email.com',
type: 'google sheets',
data: encryptedData,
workspaceId: proWorkspaceId,
iv,
},
],
})
}
export const setupDatabase = async () => {
await setupWorkspaces()
await setupUsers()
return setupCredentials()
}
export const teardownDatabase = async () => {
await prisma.webhook.deleteMany({
where: {
typebot: {
workspace: {
members: {
some: { userId: { in: [userId, otherUserId] } },
},
},
},
},
})
await prisma.workspace.deleteMany({
where: {
members: {
some: { userId: { in: [userId, otherUserId] } },
},
},
})
await prisma.workspace.deleteMany({
where: {
id: {
in: [
proWorkspaceId,
freeWorkspaceId,
starterWorkspaceId,
lifetimeWorkspaceId,
customWorkspaceId,
],
},
},
})
await prisma.user.deleteMany({
where: { id: { in: [userId, otherUserId] } },
})
}

View File

@ -1,11 +0,0 @@
import { FullConfig } from '@playwright/test'
import { setupDatabase, teardownDatabase } from './databaseSetup'
async function globalSetup(config: FullConfig) {
const { baseURL } = config.projects[0].use
if (!baseURL) throw new Error('baseURL is missing')
await teardownDatabase()
await setupDatabase()
}
export default globalSetup

View File

@ -1,30 +0,0 @@
import type { Page } from '@playwright/test'
export const mockSessionResponsesToOtherUser = async (page: Page) =>
page.route('/api/auth/session', (route) => {
if (route.request().method() === 'GET') {
return route.fulfill({
status: 200,
body: '{"user":{"id":"otherUserId","name":"James Doe","email":"other-user@email.com","emailVerified":null,"image":"https://avatars.githubusercontent.com/u/16015833?v=4","stripeId":null,"graphNavigation": "TRACKPAD"}}',
})
}
return route.continue()
})
export const typebotViewer = (page: Page) =>
page.frameLocator('#typebot-iframe')
export const waitForSuccessfulPutRequest = (page: Page) =>
page.waitForResponse(
(resp) => resp.request().method() === 'PUT' && resp.status() === 200
)
export const waitForSuccessfulPostRequest = (page: Page) =>
page.waitForResponse(
(resp) => resp.request().method() === 'POST' && resp.status() === 200
)
export const waitForSuccessfulDeleteRequest = (page: Page) =>
page.waitForResponse(
(resp) => resp.request().method() === 'DELETE' && resp.status() === 200
)

View File

@ -1,88 +0,0 @@
import {
ResultWithAnswers,
ResultHeaderCell,
VariableWithValue,
Answer,
TableData,
} from '@typebot.io/schemas'
import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/constants'
import { isDefined } from '../utils'
type CellParser = (
content: VariableWithValue['value'],
blockType?: InputBlockType
) => { element?: React.JSX.Element; plainText: string }
const defaultCellParser: CellParser = (content, blockType) => {
if (!content) return { plainText: '' }
if (Array.isArray(content))
return {
plainText: content.join(', '),
}
return blockType === InputBlockType.FILE
? { plainText: content }
: { plainText: content.toString() }
}
export const convertResultsToTableData = (
results: ResultWithAnswers[] | undefined,
headerCells: ResultHeaderCell[],
cellParser: CellParser = defaultCellParser
): TableData[] =>
(results ?? []).map((result) => ({
id: { plainText: result.id },
date: {
plainText: convertDateToReadable(result.createdAt),
},
...[...result.answers, ...result.variables].reduce<{
[key: string]: { element?: JSX.Element; plainText: string }
}>((tableData, answerOrVariable) => {
if ('groupId' in answerOrVariable) {
const answer = answerOrVariable satisfies Answer
const header = answer.variableId
? headerCells.find((headerCell) =>
headerCell.variableIds?.includes(answer.variableId as string)
)
: headerCells.find((headerCell) =>
headerCell.blocks?.some((block) => block.id === answer.blockId)
)
if (!header || !header.blocks || !header.blockType) return tableData
const variableValue = result.variables.find(
(variable) => variable.id === answer.variableId
)?.value
const content = variableValue ?? answer.content
return {
...tableData,
[header.id]: cellParser(content, header.blockType),
}
}
const variable = answerOrVariable satisfies VariableWithValue
if (variable.value === null) return tableData
const headerId = headerCells.find((headerCell) =>
headerCell.variableIds?.includes(variable.id)
)?.id
if (!headerId) return tableData
if (isDefined(tableData[headerId])) return tableData
return {
...tableData,
[headerId]: cellParser(variable.value),
}
}, {}),
}))
const convertDateToReadable = (date: Date): string => {
const isThisYear = new Date().getFullYear() === date.getFullYear()
const dateString = date.toLocaleDateString('default', {
month: 'short',
day: 'numeric',
year: isThisYear ? undefined : 'numeric', // Only show the year if it's not the current year
})
const timeString = date.toLocaleTimeString('default', {
hour: '2-digit',
minute: '2-digit',
})
return `${dateString}, ${timeString}`
}

View File

@ -1,39 +0,0 @@
import {
AnswerInSessionState,
Variable,
VariableWithValue,
} from '@typebot.io/schemas'
import { isDefined, isEmpty } from '../utils'
export const parseAnswers = ({
answers,
variables: resultVariables,
}: {
answers: AnswerInSessionState[]
variables: Variable[]
}): {
[key: string]: string
} => {
const variablesWithValues = resultVariables.filter((variable) =>
isDefined(variable.value)
) as VariableWithValue[]
return {
submittedAt: new Date().toISOString(),
...[...answers, ...variablesWithValues].reduce<{
[key: string]: string
}>((o, answerOrVariable) => {
if ('id' in answerOrVariable) {
const variable = answerOrVariable
if (variable.value === null) return o
return { ...o, [variable.name]: variable.value.toString() }
}
const answer = answerOrVariable as AnswerInSessionState
if (isEmpty(answer.key)) return o
return {
...o,
[answer.key]: answer.value,
}
}, {}),
}
}

View File

@ -1,15 +0,0 @@
import { ResultHeaderCell } from '@typebot.io/schemas'
export const parseColumnsOrder = (
existingOrder: string[] | undefined,
resultHeader: ResultHeaderCell[]
) =>
existingOrder
? [
...existingOrder.slice(0, -1),
...resultHeader
.filter((header) => !existingOrder.includes(header.id))
.map((h) => h.id),
'logs',
]
: ['select', ...resultHeader.map((h) => h.id), 'logs']

View File

@ -1,212 +0,0 @@
import {
ResultWithAnswers,
ResultHeaderCell,
Group,
Variable,
InputBlock,
Typebot,
} from '@typebot.io/schemas'
import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/constants'
import { isInputBlock, byId, isNotEmpty } from '../utils'
export const parseResultHeader = (
typebot: Pick<Typebot, 'groups' | 'variables'>,
linkedTypebots: Pick<Typebot, 'groups' | 'variables'>[] | undefined,
results?: ResultWithAnswers[]
): ResultHeaderCell[] => {
const parsedGroups = [
...typebot.groups,
...(linkedTypebots ?? []).flatMap(
(linkedTypebot) => linkedTypebot.groups as Group[]
),
]
const parsedVariables = [
...typebot.variables,
...(linkedTypebots ?? []).flatMap(
(linkedTypebot) => linkedTypebot.variables
),
]
const inputsResultHeader = parseInputsResultHeader({
groups: parsedGroups,
variables: parsedVariables,
})
return [
{ label: 'Submitted at', id: 'date' },
...inputsResultHeader,
...parseVariablesHeaders(parsedVariables, inputsResultHeader),
...parseResultsFromPreviousBotVersions(results ?? [], inputsResultHeader),
]
}
type ResultHeaderCellWithBlock = Omit<ResultHeaderCell, 'blocks'> & {
blocks: NonNullable<ResultHeaderCell['blocks']>
}
const parseInputsResultHeader = ({
groups,
variables,
}: {
groups: Group[]
variables: Variable[]
}): ResultHeaderCellWithBlock[] =>
(
groups
.flatMap((group) =>
group.blocks.map((block) => ({
...block,
groupTitle: group.title,
groupId: group.id,
}))
)
.filter((block) => isInputBlock(block)) as (InputBlock & {
groupId: string
groupTitle: string
})[]
).reduce<ResultHeaderCellWithBlock[]>((existingHeaders, inputBlock) => {
if (
existingHeaders.some(
(existingHeader) =>
inputBlock.options?.variableId &&
existingHeader.variableIds?.includes(inputBlock.options.variableId)
)
)
return existingHeaders
const matchedVariableName =
inputBlock.options?.variableId &&
variables.find(byId(inputBlock.options.variableId))?.name
let label = matchedVariableName ?? inputBlock.groupTitle
const headerWithSameLabel = existingHeaders.find((h) => h.label === label)
if (headerWithSameLabel) {
const shouldMerge = headerWithSameLabel.blocks?.some(
(block) => block.id === inputBlock.id
)
if (shouldMerge) {
const updatedHeaderCell: ResultHeaderCellWithBlock = {
...headerWithSameLabel,
variableIds:
headerWithSameLabel.variableIds && inputBlock.options?.variableId
? headerWithSameLabel.variableIds.concat([
inputBlock.options.variableId,
])
: undefined,
blocks: headerWithSameLabel.blocks.concat({
id: inputBlock.id,
groupId: inputBlock.groupId,
}),
}
return [
...existingHeaders.filter(
(existingHeader) => existingHeader.label !== label
),
updatedHeaderCell,
]
}
const totalPrevious = existingHeaders.filter((h) =>
h.label.includes(label)
).length
const newHeaderCell: ResultHeaderCellWithBlock = {
id: inputBlock.id,
label: label + ` (${totalPrevious})`,
blocks: [
{
id: inputBlock.id,
groupId: inputBlock.groupId,
},
],
blockType: inputBlock.type,
variableIds: inputBlock.options?.variableId
? [inputBlock.options.variableId]
: undefined,
}
return [...existingHeaders, newHeaderCell]
}
const newHeaderCell: ResultHeaderCellWithBlock = {
id: inputBlock.id,
label,
blocks: [
{
id: inputBlock.id,
groupId: inputBlock.groupId,
},
],
blockType: inputBlock.type,
variableIds: inputBlock.options?.variableId
? [inputBlock.options.variableId]
: undefined,
}
return [...existingHeaders, newHeaderCell]
}, [])
const parseVariablesHeaders = (
variables: Variable[],
existingInputResultHeaders: ResultHeaderCell[]
) =>
variables.reduce<ResultHeaderCell[]>((existingHeaders, variable) => {
if (
existingInputResultHeaders.some((existingInputResultHeader) =>
existingInputResultHeader.variableIds?.includes(variable.id)
)
)
return existingHeaders
const headerCellWithSameLabel = existingHeaders.find(
(existingHeader) => existingHeader.label === variable.name
)
if (headerCellWithSameLabel) {
const updatedHeaderCell: ResultHeaderCell = {
...headerCellWithSameLabel,
variableIds: headerCellWithSameLabel.variableIds?.concat([variable.id]),
}
return [
...existingHeaders.filter((h) => h.label !== variable.name),
updatedHeaderCell,
]
}
const newHeaderCell: ResultHeaderCell = {
id: variable.id,
label: variable.name,
variableIds: [variable.id],
}
return [...existingHeaders, newHeaderCell]
}, [])
const parseResultsFromPreviousBotVersions = (
results: ResultWithAnswers[],
existingInputResultHeaders: ResultHeaderCell[]
): ResultHeaderCell[] =>
results
.flatMap((result) => result.answers)
.filter(
(answer) =>
!answer.variableId &&
existingInputResultHeaders.every(
(header) => header.id !== answer.blockId
) &&
isNotEmpty(answer.content)
)
.reduce<ResultHeaderCell[]>((existingHeaders, answer) => {
if (
existingHeaders.some(
(existingHeader) => existingHeader.id === answer.blockId
)
)
return existingHeaders
return [
...existingHeaders,
{
id: answer.blockId,
label: `${answer.blockId} (deleted block)`,
blocks: [
{
id: answer.blockId,
groupId: answer.groupId,
},
],
blockType: InputBlockType.TEXT,
},
]
}, [])

View File

@ -1,69 +0,0 @@
import { env } from '@typebot.io/env'
import { TelemetryEvent } from '@typebot.io/schemas/features/telemetry'
import { PostHog } from 'posthog-node'
import got from 'got'
export const trackEvents = async (events: TelemetryEvent[]) => {
if (!env.NEXT_PUBLIC_POSTHOG_KEY) return
const client = new PostHog(env.NEXT_PUBLIC_POSTHOG_KEY, {
host: env.NEXT_PUBLIC_POSTHOG_HOST,
})
events.forEach(async (event) => {
if (event.name === 'User created') {
client.identify({
distinctId: event.userId,
properties: event.data,
})
if (env.USER_CREATED_WEBHOOK_URL) {
try {
await got.post(env.USER_CREATED_WEBHOOK_URL, {
json: {
email: event.data.email,
name: event.data.name ? event.data.name.split(' ')[0] : undefined,
},
})
} catch (e) {
console.error('Failed to call user created webhook', e)
}
}
}
if (
event.name === 'Workspace created' ||
event.name === 'Subscription updated'
)
client.groupIdentify({
groupType: 'workspace',
groupKey: event.workspaceId,
properties: event.data,
})
if (event.name === 'Typebot created' || event.name === 'Typebot published')
client.groupIdentify({
groupType: 'typebot',
groupKey: event.typebotId,
properties: { name: event.data.name },
})
const groups: { workspace?: string; typebot?: string } = {}
if ('workspaceId' in event) groups['workspace'] = event.workspaceId
if ('typebotId' in event) groups['typebot'] = event.typebotId
client.capture({
distinctId: event.userId,
event: event.name,
properties:
event.name === 'User updated'
? { $set: event.data }
: event.name === 'User logged in'
? {
$set: {
lastActivityAt: new Date().toISOString(),
},
}
: 'data' in event
? event.data
: undefined,
groups,
})
})
await client.shutdownAsync()
}

View File

@ -1,26 +1,3 @@
import type {
BubbleBlock,
ChoiceInputBlock,
ConditionBlock,
InputBlock,
IntegrationBlock,
LogicBlock,
Block,
TextInputBlock,
TextBubbleBlock,
HttpRequestBlock,
ImageBubbleBlock,
VideoBubbleBlock,
BlockWithOptionsType,
} from '@typebot.io/schemas'
import { BubbleBlockType } from '@typebot.io/schemas/features/blocks/bubbles/constants'
import { InputBlockType } from '@typebot.io/schemas/features/blocks/inputs/constants'
import { PictureChoiceBlock } from '@typebot.io/schemas/features/blocks/inputs/pictureChoice'
import { IntegrationBlockType } from '@typebot.io/schemas/features/blocks/integrations/constants'
import { LogicBlockType } from '@typebot.io/schemas/features/blocks/logic/constants'
import { defaultChoiceInputOptions } from '@typebot.io/schemas/features/blocks/inputs/choice/constants'
import { enabledBlocks } from '@typebot.io/forge-repository'
export const sendRequest = async <ResponseData>(
params:
| {
@ -72,88 +49,6 @@ export const isEmpty = (
export const isNotEmpty = (value: string | undefined | null): value is string =>
value !== undefined && value !== null && value !== ''
export const isInputBlock = (block: Block): block is InputBlock =>
(Object.values(InputBlockType) as string[]).includes(block.type)
export const isBubbleBlock = (block: Block): block is BubbleBlock =>
(Object.values(BubbleBlockType) as string[]).includes(block.type)
export const isLogicBlock = (block: Block): block is LogicBlock =>
(Object.values(LogicBlockType) as string[]).includes(block.type)
export const isTextBubbleBlock = (block: Block): block is TextBubbleBlock =>
block.type === BubbleBlockType.TEXT
export const isMediaBubbleBlock = (
block: Block
): block is ImageBubbleBlock | VideoBubbleBlock =>
block.type === BubbleBlockType.IMAGE || block.type === BubbleBlockType.VIDEO
export const isTextInputBlock = (block: Block): block is TextInputBlock =>
block.type === InputBlockType.TEXT
export const isChoiceInput = (block: Block): block is ChoiceInputBlock =>
block.type === InputBlockType.CHOICE
export const isPictureChoiceInput = (
block: Block
): block is PictureChoiceBlock => block.type === InputBlockType.PICTURE_CHOICE
export const isSingleChoiceInput = (block: Block): block is ChoiceInputBlock =>
block.type === InputBlockType.CHOICE &&
'options' in block &&
!(
block.options?.isMultipleChoice ??
defaultChoiceInputOptions.isMultipleChoice
)
export const isConditionBlock = (block: Block): block is ConditionBlock =>
block.type === LogicBlockType.CONDITION
export const isIntegrationBlock = (block: Block): block is IntegrationBlock =>
(
Object.values(IntegrationBlockType).concat(
enabledBlocks as readonly any[]
) as any[]
).includes(block.type)
export const isWebhookBlock = (block: Block): block is HttpRequestBlock =>
[
IntegrationBlockType.WEBHOOK,
IntegrationBlockType.PABBLY_CONNECT,
IntegrationBlockType.ZAPIER,
IntegrationBlockType.MAKE_COM,
].includes(block.type as IntegrationBlockType)
export const isBubbleBlockType = (
type: Block['type']
): type is BubbleBlockType =>
(Object.values(BubbleBlockType) as string[]).includes(type)
export const blockTypeHasOption = (
type: Block['type']
): type is BlockWithOptionsType =>
(Object.values(InputBlockType) as string[])
.concat(Object.values(LogicBlockType))
.concat(Object.values(IntegrationBlockType))
.includes(type)
export const blockTypeHasItems = (
type: Block['type']
): type is
| LogicBlockType.CONDITION
| InputBlockType.CHOICE
| LogicBlockType.AB_TEST =>
type === LogicBlockType.CONDITION ||
type === InputBlockType.CHOICE ||
type === LogicBlockType.AB_TEST ||
type === InputBlockType.PICTURE_CHOICE
export const blockHasItems = (
block: Block
): block is ConditionBlock | ChoiceInputBlock =>
'items' in block && isDefined(block.items)
export const byId = (id?: string) => (obj: { id: string }) => obj.id === id
export const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1)