2022-01-08 07:40:55 +01:00
|
|
|
import {
|
2022-06-11 07:27:38 +02:00
|
|
|
BubbleBlock,
|
|
|
|
|
BubbleBlockType,
|
|
|
|
|
ChoiceInputBlock,
|
|
|
|
|
ConditionBlock,
|
|
|
|
|
InputBlock,
|
|
|
|
|
InputBlockType,
|
|
|
|
|
IntegrationBlock,
|
|
|
|
|
IntegrationBlockType,
|
|
|
|
|
LogicBlock,
|
|
|
|
|
LogicBlockType,
|
|
|
|
|
Block,
|
|
|
|
|
TextInputBlock,
|
|
|
|
|
TextBubbleBlock,
|
|
|
|
|
WebhookBlock,
|
|
|
|
|
BlockType,
|
|
|
|
|
BlockWithOptionsType,
|
|
|
|
|
ImageBubbleBlock,
|
|
|
|
|
VideoBubbleBlock,
|
2022-01-08 07:40:55 +01:00
|
|
|
} from 'models'
|
2022-01-06 09:40:56 +01:00
|
|
|
|
2022-02-07 07:13:16 +01:00
|
|
|
export const sendRequest = async <ResponseData>(
|
|
|
|
|
params:
|
|
|
|
|
| {
|
|
|
|
|
url: string
|
|
|
|
|
method: string
|
|
|
|
|
body?: Record<string, unknown>
|
|
|
|
|
}
|
|
|
|
|
| string
|
|
|
|
|
): Promise<{ data?: ResponseData; error?: Error }> => {
|
2021-12-29 10:22:26 +01:00
|
|
|
try {
|
2022-02-07 07:13:16 +01:00
|
|
|
const url = typeof params === 'string' ? params : params.url
|
2021-12-29 10:22:26 +01:00
|
|
|
const response = await fetch(url, {
|
2022-02-07 07:13:16 +01:00
|
|
|
method: typeof params === 'string' ? 'GET' : params.method,
|
2021-12-29 10:22:26 +01:00
|
|
|
mode: 'cors',
|
2022-02-22 10:16:35 +01:00
|
|
|
headers:
|
|
|
|
|
typeof params !== 'string' && isDefined(params.body)
|
|
|
|
|
? {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
}
|
|
|
|
|
: undefined,
|
2022-02-07 07:13:16 +01:00
|
|
|
body:
|
|
|
|
|
typeof params !== 'string' && isDefined(params.body)
|
|
|
|
|
? JSON.stringify(params.body)
|
|
|
|
|
: undefined,
|
2021-12-29 10:22:26 +01:00
|
|
|
})
|
|
|
|
|
const data = await response.json()
|
2022-06-01 08:47:15 +02:00
|
|
|
if (!response.ok) throw 'error' in data ? data.error : data
|
2021-12-29 10:22:26 +01:00
|
|
|
return { data }
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e)
|
|
|
|
|
return { error: e as Error }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-15 17:30:20 +01:00
|
|
|
export const isDefined = <T>(
|
|
|
|
|
value: T | undefined | null
|
|
|
|
|
): value is NonNullable<T> => value !== undefined && value !== null
|
2022-01-06 09:40:56 +01:00
|
|
|
|
2022-02-02 08:05:02 +01:00
|
|
|
export const isNotDefined = <T>(
|
|
|
|
|
value: T | undefined | null
|
|
|
|
|
): value is undefined | null => value === undefined || value === null
|
|
|
|
|
|
2022-05-25 08:13:35 -07:00
|
|
|
export const isEmpty = (value: string | undefined | null): value is undefined =>
|
|
|
|
|
value === undefined || value === null || value === ''
|
|
|
|
|
|
|
|
|
|
export const isNotEmpty = (value: string | undefined | null): value is string =>
|
|
|
|
|
value !== undefined && value !== null && value !== ''
|
|
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export const isInputBlock = (block: Block): block is InputBlock =>
|
|
|
|
|
(Object.values(InputBlockType) as string[]).includes(block.type)
|
2022-01-08 07:40:55 +01:00
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export const isBubbleBlock = (block: Block): block is BubbleBlock =>
|
|
|
|
|
(Object.values(BubbleBlockType) as string[]).includes(block.type)
|
2022-01-14 07:49:24 +01:00
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export const isLogicBlock = (block: Block): block is LogicBlock =>
|
|
|
|
|
(Object.values(LogicBlockType) as string[]).includes(block.type)
|
2022-01-14 07:49:24 +01:00
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export const isTextBubbleBlock = (block: Block): block is TextBubbleBlock =>
|
|
|
|
|
block.type === BubbleBlockType.TEXT
|
2022-01-08 07:40:55 +01:00
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export const isMediaBubbleBlock = (
|
|
|
|
|
block: Block
|
|
|
|
|
): block is ImageBubbleBlock | VideoBubbleBlock =>
|
|
|
|
|
block.type === BubbleBlockType.IMAGE || block.type === BubbleBlockType.VIDEO
|
2022-02-07 07:13:16 +01:00
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export const isTextInputBlock = (block: Block): block is TextInputBlock =>
|
|
|
|
|
block.type === InputBlockType.TEXT
|
2022-01-12 09:10:59 +01:00
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export const isChoiceInput = (block: Block): block is ChoiceInputBlock =>
|
|
|
|
|
block.type === InputBlockType.CHOICE
|
2022-01-12 09:10:59 +01:00
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export const isSingleChoiceInput = (block: Block): block is ChoiceInputBlock =>
|
|
|
|
|
block.type === InputBlockType.CHOICE &&
|
|
|
|
|
'options' in block &&
|
|
|
|
|
!block.options.isMultipleChoice
|
2022-01-15 17:30:20 +01:00
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export const isConditionBlock = (block: Block): block is ConditionBlock =>
|
|
|
|
|
block.type === LogicBlockType.CONDITION
|
2022-01-18 18:25:18 +01:00
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export const isIntegrationBlock = (block: Block): block is IntegrationBlock =>
|
|
|
|
|
(Object.values(IntegrationBlockType) as string[]).includes(block.type)
|
2022-01-22 18:24:57 +01:00
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export const isWebhookBlock = (block: Block): block is WebhookBlock =>
|
2022-04-19 08:57:36 -07:00
|
|
|
[
|
2022-06-11 07:27:38 +02:00
|
|
|
IntegrationBlockType.WEBHOOK,
|
|
|
|
|
IntegrationBlockType.PABBLY_CONNECT,
|
|
|
|
|
IntegrationBlockType.ZAPIER,
|
|
|
|
|
IntegrationBlockType.MAKE_COM,
|
|
|
|
|
].includes(block.type as IntegrationBlockType)
|
|
|
|
|
|
|
|
|
|
export const isBubbleBlockType = (type: BlockType): type is BubbleBlockType =>
|
|
|
|
|
(Object.values(BubbleBlockType) as string[]).includes(type)
|
|
|
|
|
|
|
|
|
|
export const blockTypeHasOption = (
|
|
|
|
|
type: BlockType
|
|
|
|
|
): type is BlockWithOptionsType =>
|
|
|
|
|
(Object.values(InputBlockType) as string[])
|
|
|
|
|
.concat(Object.values(LogicBlockType))
|
|
|
|
|
.concat(Object.values(IntegrationBlockType))
|
2022-01-25 18:19:37 +01:00
|
|
|
.includes(type)
|
2022-02-04 19:00:08 +01:00
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export const blockTypeHasWebhook = (
|
|
|
|
|
type: BlockType
|
|
|
|
|
): type is IntegrationBlockType.WEBHOOK =>
|
2022-02-22 08:03:38 +01:00
|
|
|
Object.values([
|
2022-06-11 07:27:38 +02:00
|
|
|
IntegrationBlockType.WEBHOOK,
|
|
|
|
|
IntegrationBlockType.ZAPIER,
|
|
|
|
|
IntegrationBlockType.MAKE_COM,
|
|
|
|
|
IntegrationBlockType.PABBLY_CONNECT,
|
2022-02-22 08:03:38 +01:00
|
|
|
] as string[]).includes(type)
|
2022-02-04 19:00:08 +01:00
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export const blockTypeHasItems = (
|
|
|
|
|
type: BlockType
|
|
|
|
|
): type is LogicBlockType.CONDITION | InputBlockType.CHOICE =>
|
|
|
|
|
type === LogicBlockType.CONDITION || type === InputBlockType.CHOICE
|
2022-02-04 19:00:08 +01:00
|
|
|
|
2022-06-11 07:27:38 +02:00
|
|
|
export const blockHasItems = (
|
|
|
|
|
block: Block
|
|
|
|
|
): block is ConditionBlock | ChoiceInputBlock =>
|
|
|
|
|
'items' in block && isDefined(block.items)
|
2022-02-04 19:00:08 +01:00
|
|
|
|
|
|
|
|
export const byId = (id?: string) => (obj: { id: string }) => obj.id === id
|
2022-02-09 17:41:45 +01:00
|
|
|
|
|
|
|
|
export const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1)
|
2022-03-01 07:13:09 +01:00
|
|
|
|
|
|
|
|
interface Omit {
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
|
|
|
<T extends object, K extends [...(keyof T)[]]>(obj: T, ...keys: K): {
|
|
|
|
|
[K2 in Exclude<keyof T, K[number]>]: T[K2]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const omit: Omit = (obj, ...keys) => {
|
|
|
|
|
const ret = {} as {
|
|
|
|
|
[K in keyof typeof obj]: typeof obj[K]
|
|
|
|
|
}
|
|
|
|
|
let key: keyof typeof obj
|
|
|
|
|
for (key in obj) {
|
|
|
|
|
if (!keys.includes(key)) {
|
|
|
|
|
ret[key] = obj[key]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret
|
|
|
|
|
}
|
2022-03-23 12:01:35 +01:00
|
|
|
|
|
|
|
|
export const sanitizeUrl = (url: string): string =>
|
|
|
|
|
url.startsWith('http') ||
|
|
|
|
|
url.startsWith('mailto:') ||
|
|
|
|
|
url.startsWith('tel:') ||
|
|
|
|
|
url.startsWith('sms:')
|
|
|
|
|
? url
|
|
|
|
|
: `https://${url}`
|
2022-06-01 12:09:45 +02:00
|
|
|
|
|
|
|
|
export const toTitleCase = (str: string) =>
|
|
|
|
|
str.replace(
|
|
|
|
|
/\w\S*/g,
|
|
|
|
|
(txt) => txt.charAt(0).toUpperCase() + txt.slice(1).toLowerCase()
|
|
|
|
|
)
|
2022-06-03 13:20:19 +02:00
|
|
|
|
|
|
|
|
export const generateId = (idDesiredLength: number): string => {
|
|
|
|
|
const getRandomCharFromAlphabet = (alphabet: string): string => {
|
|
|
|
|
return alphabet.charAt(Math.floor(Math.random() * alphabet.length))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Array.from({ length: idDesiredLength })
|
|
|
|
|
.map(() => {
|
|
|
|
|
return getRandomCharFromAlphabet(
|
|
|
|
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.join('')
|
|
|
|
|
}
|