import {z} from 'zod' export type GetApiApiOrgIdAlertsDestinationsPrebuiltArgs = {org_id: string}; export type GetApiApiOrgIdAlertsDestinationsPrebuiltOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const autoInvestigate = z.enum(["off", "critical"]); export const mentionRule = z.union([z.literal("default"), z.literal("here"), z.literal("channel"), z.literal("none"), z.object({subteam: z.object({handle: z.string(), subteam_id: z.string()})})]); export const hTTPType = z.enum(["post", "put", "get"]); export const hTTPOutputFormat = z.union([z.literal("json"), z.literal("ndjson"), z.literal("nestedevent"), z.object({esbulk: z.object({index: z.string()})}), z.object({stringseparated: z.object({separator: z.string()})})]); export const destinationType = z.enum(["http", "email", "sns", "slack_app"]); export const destinationConsumerKind = z.enum(["alert", "composite_alert", "pipeline", "synthetic_check", "oncall_policy", "oncall_team_channel", "workflow", "anomaly_detection", "incident_integration", "downtime"]); export const destinationUseResponse = z.object({consumer: destinationConsumerKind, folder_id: z.string().nullable().optional(), id: z.string(), name: z.string()}); export const getApiApiOrgIdAlertsDestinationsPrebuiltResponse = z.array(z.object({auto_investigate: autoInvestigate.nullable().optional(), buttons: z.boolean().nullable().optional(), channel_id: z.string().nullable().optional(), channel_name: z.string().nullable().optional(), installation_id: z.string().nullable().optional(), mention: mentionRule.nullable().optional(), oncall_room_confirmed: z.boolean().nullable().optional(), previous_webhook: z.object({}).nullable().optional(), refire_reuse_minutes: z.number().int().gte(0).nullable().optional(), shared_channel_confirmed: z.boolean().nullable().optional(), aws_region: z.string().nullable().optional(), destination_type_name: z.string().nullable().optional(), emails: z.array(z.string()).optional(), headers: z.record(z.string(), z.string()).nullable().optional(), metadata: z.record(z.string(), z.string()).optional(), method: hTTPType.optional(), name: z.string().optional(), output_format: hTTPOutputFormat.nullable().optional(), skip_tls_verify: z.boolean().optional(), sns_topic_arn: z.string().nullable().optional(), template: z.string().nullable().optional(), type: destinationType.optional(), url: z.string().optional(), uses: z.array(destinationUseResponse).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 }; /** * List prebuilt alert destination templates * * GET /api/{org_id}/alerts/destinations/prebuilt */ export const getApiApiOrgIdAlertsDestinationsPrebuilt = async (args: GetApiApiOrgIdAlertsDestinationsPrebuiltArgs, options: GetApiApiOrgIdAlertsDestinationsPrebuiltOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/alerts/destinations/prebuilt${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(getApiApiOrgIdAlertsDestinationsPrebuiltResponse, await res.json()) };