import {z} from 'zod' export type NotificationEventType = 'job.scheduled' | 'job.in_progress' | 'job.progress' | 'job.first_file_failed' | 'job.completed' | 'job.stopped' | 'job.failed' | 'job.node.finished'; export type CreateWebhookChannelRequest = {channel_type?: 'webhook' | undefined, description?: (string | null) | undefined, event_types: Array, enabled?: boolean | undefined, url: string, secret?: (string | null) | undefined}; export type EmailChannelConfig = {cc?: (Array | null) | undefined, reply_to?: (string | null) | undefined, recipient_email: string}; export type CreateEmailChannelRequest = {channel_type?: 'email' | undefined, description?: (string | null) | undefined, event_types: Array, enabled?: boolean | undefined, email_config: EmailChannelConfig}; export type CreateApiApiV1NotificationsChannelsArgs = {'unstructured-api-key'?: (string | null) | undefined, body: CreateWebhookChannelRequest | CreateEmailChannelRequest}; export type CreateApiApiV1NotificationsChannelsOptions = {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 createApiApiV1NotificationsChannelsResponse = 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 }; /** * Create Channel * * POST /api/v1/notifications/channels */ export const createApiApiV1NotificationsChannels = async (args: CreateApiApiV1NotificationsChannelsArgs, options: CreateApiApiV1NotificationsChannelsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") 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: "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(createApiApiV1NotificationsChannelsResponse, await res.json()) };