import {z} from 'zod' export type GetApiV1ChannelsOptions = {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 getApiV1ChannelsResponse = z.array(channelRead); 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 }; /** * List Channels * * GET /v1/channels/ */ export const getApiV1Channels = async (options: GetApiV1ChannelsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2AuthorizationCodeBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1ChannelsResponse, await res.json()) };