import {z} from 'zod' export type AlertChanelSubscription = {id?: number | undefined, checkId?: (string | null) | undefined, groupId?: (number | null) | undefined, activated: boolean}; export type AlertChanelSubscriptionList = Array; export type Type = 'EMAIL' | 'SLACK' | 'SLACK_APP' | 'WEBHOOK' | 'SMS' | 'PAGERDUTY' | 'OPSGENIE' | 'CALL'; export type AlertChannelCreateConfig = Record; export type AlertChannelCreate = {subscriptions?: AlertChanelSubscriptionList | undefined, type: Type, config: AlertChannelCreateConfig, sendRecovery?: boolean | undefined, sendFailure?: boolean | undefined, sendDegraded?: boolean | undefined, sslExpiry?: boolean | undefined, sslExpiryThreshold?: number | undefined, autoSubscribe?: boolean | undefined}; export type UpdateApiV1AlertChannelsIdArgs = {'x-checkly-account'?: string | undefined, id: number, body: AlertChannelCreate}; export type UpdateApiV1AlertChannelsIdOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const type = z.enum(["EMAIL", "SLACK", "SLACK_APP", "WEBHOOK", "SMS", "PAGERDUTY", "OPSGENIE", "CALL"]); export const alertChannelConfig = z.object({}); export const alertChanelSubscription = z.object({id: z.number().optional(), checkId: z.string().nullable().optional(), groupId: z.number().nullable().optional(), activated: z.boolean()}); export const alertChanelSubscriptionList = z.array(alertChanelSubscription); export const alertChannel = z.object({id: z.number(), type: type, config: alertChannelConfig, subscriptions: alertChanelSubscriptionList.optional(), sendRecovery: z.boolean().optional(), sendFailure: z.boolean().optional(), sendDegraded: z.boolean().optional(), sslExpiry: z.boolean().optional(), sslExpiryThreshold: z.number().int().gte(1).lte(30).optional(), autoSubscribe: z.boolean().optional(), created_at: z.string().optional(), updated_at: z.string().nullable().optional()}); 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 }; /** * Update an alert channel * * PUT /v1/alert-channels/{id} */ export const updateApiV1AlertChannelsId = async (args: UpdateApiV1AlertChannelsIdArgs, options?: UpdateApiV1AlertChannelsIdOptions) => { const {baseUrl = "https://api.checklyhq.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-checkly-account'] != null) headers.set("x-checkly-account", String(args['x-checkly-account'])) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/alert-channels/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(alertChannel, await res.json()) };