2023-08-06 10:03:45 +02:00
|
|
|
import { z } from 'zod'
|
2023-11-08 15:34:16 +01:00
|
|
|
import { blockBaseSchema } from '../../shared'
|
|
|
|
import { IntegrationBlockType } from '../constants'
|
|
|
|
import { HttpMethod } from './constants'
|
2023-08-06 10:03:45 +02:00
|
|
|
|
|
|
|
const variableForTestSchema = z.object({
|
|
|
|
id: z.string(),
|
|
|
|
variableId: z.string().optional(),
|
|
|
|
value: z.string().optional(),
|
|
|
|
})
|
|
|
|
|
|
|
|
const responseVariableMappingSchema = z.object({
|
|
|
|
id: z.string(),
|
|
|
|
variableId: z.string().optional(),
|
|
|
|
bodyPath: z.string().optional(),
|
|
|
|
})
|
|
|
|
|
|
|
|
const keyValueSchema = z.object({
|
|
|
|
id: z.string(),
|
|
|
|
key: z.string().optional(),
|
|
|
|
value: z.string().optional(),
|
|
|
|
})
|
|
|
|
|
2023-11-08 15:34:16 +01:00
|
|
|
export const webhookV5Schema = z.object({
|
2023-08-06 10:03:45 +02:00
|
|
|
id: z.string(),
|
2023-11-08 15:34:16 +01:00
|
|
|
queryParams: keyValueSchema.array().optional(),
|
|
|
|
headers: keyValueSchema.array().optional(),
|
|
|
|
method: z.nativeEnum(HttpMethod).optional(),
|
2023-08-06 10:03:45 +02:00
|
|
|
url: z.string().optional(),
|
|
|
|
body: z.string().optional(),
|
|
|
|
})
|
|
|
|
|
2023-11-08 15:34:16 +01:00
|
|
|
const webhookSchemas = {
|
|
|
|
v5: webhookV5Schema,
|
|
|
|
v6: webhookV5Schema.omit({
|
|
|
|
id: true,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
const webhookSchema = z.union([webhookSchemas.v5, webhookSchemas.v6])
|
|
|
|
|
|
|
|
export const webhookOptionsV5Schema = z.object({
|
|
|
|
variablesForTest: z.array(variableForTestSchema).optional(),
|
|
|
|
responseVariableMapping: z.array(responseVariableMappingSchema).optional(),
|
2023-08-06 10:03:45 +02:00
|
|
|
isAdvancedConfig: z.boolean().optional(),
|
|
|
|
isCustomBody: z.boolean().optional(),
|
|
|
|
isExecutedOnClient: z.boolean().optional(),
|
2023-11-08 15:34:16 +01:00
|
|
|
webhook: webhookSchemas.v5.optional(),
|
2023-08-06 10:03:45 +02:00
|
|
|
})
|
|
|
|
|
2023-11-08 15:34:16 +01:00
|
|
|
const webhookOptionsSchemas = {
|
|
|
|
v5: webhookOptionsV5Schema,
|
|
|
|
v6: webhookOptionsV5Schema.merge(
|
|
|
|
z.object({
|
|
|
|
webhook: webhookSchemas.v6.optional(),
|
|
|
|
})
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
const webhookBlockV5Schema = blockBaseSchema.merge(
|
2023-08-06 10:03:45 +02:00
|
|
|
z.object({
|
|
|
|
type: z.enum([IntegrationBlockType.WEBHOOK]),
|
2023-11-08 15:34:16 +01:00
|
|
|
options: webhookOptionsSchemas.v5.optional(),
|
|
|
|
webhookId: z.string().optional(),
|
2023-08-06 10:03:45 +02:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2023-11-08 15:34:16 +01:00
|
|
|
export const webhookBlockSchemas = {
|
|
|
|
v5: webhookBlockV5Schema,
|
|
|
|
v6: webhookBlockV5Schema
|
|
|
|
.omit({
|
|
|
|
webhookId: true,
|
|
|
|
})
|
|
|
|
.merge(
|
|
|
|
z.object({
|
|
|
|
options: webhookOptionsSchemas.v6.optional(),
|
|
|
|
})
|
|
|
|
),
|
2023-08-06 10:03:45 +02:00
|
|
|
}
|
|
|
|
|
2023-11-08 15:34:16 +01:00
|
|
|
const webhookBlockSchema = z.union([
|
|
|
|
webhookBlockSchemas.v5,
|
|
|
|
webhookBlockSchemas.v6,
|
|
|
|
])
|
2023-08-06 10:03:45 +02:00
|
|
|
|
|
|
|
export const executableWebhookSchema = z.object({
|
|
|
|
url: z.string(),
|
|
|
|
headers: z.record(z.string()).optional(),
|
|
|
|
body: z.unknown().optional(),
|
|
|
|
method: z.nativeEnum(HttpMethod).optional(),
|
|
|
|
})
|
|
|
|
|
|
|
|
export type KeyValue = { id: string; key?: string; value?: string }
|
|
|
|
|
|
|
|
export type WebhookResponse = {
|
|
|
|
statusCode: number
|
|
|
|
data?: unknown
|
|
|
|
}
|
|
|
|
|
|
|
|
export type ExecutableWebhook = z.infer<typeof executableWebhookSchema>
|
|
|
|
|
|
|
|
export type Webhook = z.infer<typeof webhookSchema>
|
|
|
|
export type WebhookBlock = z.infer<typeof webhookBlockSchema>
|
2023-11-08 15:34:16 +01:00
|
|
|
export type WebhookBlockV6 = z.infer<typeof webhookBlockSchemas.v6>
|
2023-08-06 10:03:45 +02:00
|
|
|
export type ResponseVariableMapping = z.infer<
|
|
|
|
typeof responseVariableMappingSchema
|
|
|
|
>
|
|
|
|
export type VariableForTest = z.infer<typeof variableForTestSchema>
|