♻️ Remove @typebot.io/schemas from @typebot.io/lib
This commit is contained in:
15
packages/migrations/migratePublicTypebot.ts
Normal file
15
packages/migrations/migratePublicTypebot.ts
Normal file
@ -0,0 +1,15 @@
|
||||
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
|
||||
}
|
30
packages/migrations/migrateTypebot.ts
Normal file
30
packages/migrations/migrateTypebot.ts
Normal file
@ -0,0 +1,30 @@
|
||||
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
|
||||
}
|
70
packages/migrations/migrateTypebotFromV3ToV4.ts
Normal file
70
packages/migrations/migrateTypebotFromV3ToV4.ts
Normal file
@ -0,0 +1,70 @@
|
||||
import { Webhook as WebhookFromDb } from '@typebot.io/prisma'
|
||||
import {
|
||||
BlockV5,
|
||||
PublicTypebotV5,
|
||||
TypebotV5,
|
||||
HttpRequest,
|
||||
} from '@typebot.io/schemas'
|
||||
import { isWebhookBlock } from '@typebot.io/schemas/helpers'
|
||||
import { isDefined } from '@typebot.io/lib/utils'
|
||||
import prisma from '@typebot.io/lib/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 ?? '' : '',
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
125
packages/migrations/migrateTypebotFromV5ToV6.ts
Normal file
125
packages/migrations/migrateTypebotFromV5ToV6.ts
Normal file
@ -0,0 +1,125 @@
|
||||
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 '@typebot.io/lib/createId'
|
||||
import { EventType } from '@typebot.io/schemas/features/events/constants'
|
||||
import { byId } from '@typebot.io/lib/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
|
||||
}
|
17
packages/migrations/package.json
Normal file
17
packages/migrations/package.json
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "@typebot.io/migrations",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"scripts": {},
|
||||
"keywords": [],
|
||||
"author": "Baptiste Arnaud",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@typebot.io/schemas": "workspace:*",
|
||||
"@typebot.io/lib": "workspace:*",
|
||||
"@typebot.io/prisma": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@typebot.io/tsconfig": "workspace:*"
|
||||
}
|
||||
}
|
8
packages/migrations/tsconfig.json
Normal file
8
packages/migrations/tsconfig.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "@typebot.io/tsconfig/base.json",
|
||||
"include": ["**/*.ts"],
|
||||
"exclude": ["node_modules"],
|
||||
"compilerOptions": {
|
||||
"lib": ["ES2021", "DOM"]
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user