import {z} from 'zod' export type ChannelType = 'webhook' | 'email'; export type GetApiApiV1NotificationsChannelsArgs = {channel_type: ChannelType, enabled?: (boolean | null) | undefined, 'unstructured-api-key'?: (string | null) | undefined}; export type GetApiApiV1NotificationsChannelsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const webhookChannelResponse = z.object({channel_type: z.literal("webhook").optional(), id: z.string(), description: z.string().nullable().optional(), event_types: z.array(z.string()), enabled: z.boolean(), created_at: z.string(), updated_at: z.string(), url: z.string()}); export const emailChannelConfigResponse = z.object({cc: z.array(z.string()).nullable().optional(), reply_to: z.string().nullable().optional(), recipient_email: z.string(), provider: z.string()}); export const emailChannelResponse = z.object({channel_type: z.literal("email").optional(), id: z.string(), description: z.string().nullable().optional(), event_types: z.array(z.string()), enabled: z.boolean(), created_at: z.string(), updated_at: z.string(), email_config: emailChannelConfigResponse}); export const channelListResponse = z.object({items: z.array(z.discriminatedUnion("channel_type", [webhookChannelResponse, emailChannelResponse]))}); 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 /api/v1/notifications/channels */ export const getApiApiV1NotificationsChannels = async (args: GetApiApiV1NotificationsChannelsArgs, options: GetApiApiV1NotificationsChannelsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.channel_type != null) query.append("channel_type", String(args.channel_type)) if (args.enabled != null) query.append("enabled", String(args.enabled)) if (args['unstructured-api-key'] != null) headers.set("unstructured-api-key", String(args['unstructured-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/notifications/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(channelListResponse, await res.json()) };