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

@ -0,0 +1,83 @@
import {
VideoBubbleContentType,
youtubeRegex,
verticalVideoSuggestionSize,
horizontalVideoSuggestionSize,
vimeoRegex,
tiktokRegex,
gumletRegex,
oneDriveRegex,
} from './constants'
import { VideoBubbleBlock } from './schema'
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 }
}

128
packages/schemas/helpers.ts Normal file
View File

@ -0,0 +1,128 @@
import { enabledBlocks } from '@typebot.io/forge-repository'
import {
Block,
InputBlock,
BubbleBlock,
LogicBlock,
TextBubbleBlock,
ImageBubbleBlock,
VideoBubbleBlock,
TextInputBlock,
ChoiceInputBlock,
PictureChoiceBlock,
ConditionBlock,
IntegrationBlock,
HttpRequestBlock,
BlockWithOptionsType,
} from './features/blocks'
import { BubbleBlockType } from './features/blocks/bubbles/constants'
import { defaultChoiceInputOptions } from './features/blocks/inputs/choice/constants'
import { InputBlockType } from './features/blocks/inputs/constants'
import { IntegrationBlockType } from './features/blocks/integrations/constants'
import { LogicBlockType } from './features/blocks/logic/constants'
import { Group } from './features/typebot/types'
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 && block.items !== undefined && block.items !== null
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`)
}