import {z} from 'zod' export type WebhookFormat = 'auto' | 'slack-blockkit' | 'slack-legacy' | 'raw-data'; export type Webhook = {type: 'webhook', format: WebhookFormat, url: string}; export type Opsgenie = {type: 'opsgenie', auth_key: string, base_url?: string | undefined}; export type PagerdutyRegion = 'us' | 'eu'; export type Pagerduty = {type: 'pagerduty', routing_key: string, region?: PagerdutyRegion | undefined}; export type PagerdutyIntegration = {type: 'pagerduty-integration', install_id: string, service_id: string}; export type Notification = {type: 'notification', recipients: Array}; export type SlackIntegration = {type: 'slack-integration', install_id: string, channel_id: string, include_agent_prompt?: boolean | undefined}; export type Email = {type: 'email', recipients: Array}; export type WebPush = {type: 'web-push', recipients: Array}; export type ChannelCreate = {label: string, config: Webhook | Opsgenie | Pagerduty | PagerdutyIntegration | Notification | SlackIntegration | Email | WebPush}; export type CreateApiV1ChannelsArgs = {body: ChannelCreate}; export type CreateApiV1ChannelsOptions = {baseUrl: string, OAuth2AuthorizationCodeBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookFormat = z.enum(["auto", "slack-blockkit", "slack-legacy", "raw-data"]); export const webhook = z.object({type: z.literal("webhook"), format: webhookFormat, url: z.string()}); export const opsgenie = z.object({type: z.literal("opsgenie"), auth_key: z.string(), base_url: z.string().optional()}); export const pagerdutyRegion = z.enum(["us", "eu"]); export const pagerduty = z.object({type: z.literal("pagerduty"), routing_key: z.string(), region: pagerdutyRegion.optional()}); export const pagerdutyIntegration = z.object({type: z.literal("pagerduty-integration"), install_id: z.string(), service_id: z.string()}); export const notification = z.object({type: z.literal("notification"), recipients: z.array(z.string())}); export const slackIntegration = z.object({type: z.literal("slack-integration"), install_id: z.string(), channel_id: z.string(), include_agent_prompt: z.boolean().optional()}); export const email = z.object({type: z.literal("email"), recipients: z.array(z.string())}); export const webPush = z.object({type: z.literal("web-push"), recipients: z.array(z.string())}); export const channelRead = z.object({id: z.string(), organization_id: z.string(), label: z.string(), active: z.boolean(), created_at: z.string(), updated_at: z.string().nullable(), created_by_name: z.string().nullable(), updated_by_name: z.string().nullable(), config: z.discriminatedUnion("type", [webhook, opsgenie, pagerduty, pagerdutyIntegration, notification, slackIntegration, email, webPush])}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create Channel * * POST /v1/channels/ */ export const createApiV1Channels = async (args: CreateApiV1ChannelsArgs, options: CreateApiV1ChannelsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2AuthorizationCodeBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2AuthorizationCodeBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2AuthorizationCodeBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/channels/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(channelRead, await res.json()) };