import {z} from 'zod' export type AutoInvestigate = 'off' | 'critical'; export type MentionRule = 'default' | 'here' | 'channel' | 'none' | {subteam: {handle: string, subteam_id: string}}; export type HTTPType = 'post' | 'put' | 'get'; export type HTTPOutputFormat = 'json' | 'ndjson' | 'nestedevent' | {esbulk: {index: string}} | {stringseparated: {separator: string}}; export type DestinationType = 'http' | 'email' | 'sns' | 'slack_app'; export type DestinationConsumerKind = 'alert' | 'composite_alert' | 'pipeline' | 'synthetic_check' | 'oncall_policy' | 'oncall_team_channel' | 'workflow' | 'anomaly_detection' | 'incident_integration' | 'downtime'; export type DestinationUseResponse = {consumer: DestinationConsumerKind, folder_id?: (string | null) | undefined, id: string, name: string}; export type UpdateApiApiOrgIdAlertsDestinationsDestinationNameArgs = {org_id: string, destination_name: string, module?: string | undefined, body: {auto_investigate?: (AutoInvestigate | null) | undefined, buttons?: (boolean | null) | undefined, channel_id?: (string | null) | undefined, channel_name?: (string | null) | undefined, installation_id?: (string | null) | undefined, mention?: (MentionRule | null) | undefined, oncall_room_confirmed?: (boolean | null) | undefined, previous_webhook?: (Record | null) | undefined, refire_reuse_minutes?: (number | null) | undefined, shared_channel_confirmed?: (boolean | null) | undefined, aws_region?: (string | null) | undefined, destination_type_name?: (string | null) | undefined, emails?: Array | undefined, headers?: ({[key: string]: string} | null) | undefined, metadata?: {[key: string]: string} | undefined, method?: HTTPType | undefined, name?: string | undefined, output_format?: (HTTPOutputFormat | null) | undefined, skip_tls_verify?: boolean | undefined, sns_topic_arn?: (string | null) | undefined, template?: (string | null) | undefined, type?: DestinationType | undefined, url?: string | undefined, uses?: (Array | null) | undefined}}; export type UpdateApiApiOrgIdAlertsDestinationsDestinationNameOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const httpResponse = z.object({code: z.number().int().gte(0), error_detail: z.string().nullable().optional(), hint: z.string().nullable().optional(), id: z.string().nullable().optional(), message: z.string(), name: z.string().nullable().optional(), suggestions: z.array(z.string()).nullable().optional(), trace_id: z.string().nullable().optional(), warnings: z.array(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 alert or pipeline destination * * PUT /api/{org_id}/alerts/destinations/{destination_name} */ export const updateApiApiOrgIdAlertsDestinationsDestinationName = async (args: UpdateApiApiOrgIdAlertsDestinationsDestinationNameArgs, options: UpdateApiApiOrgIdAlertsDestinationsDestinationNameOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.module != null) query.append("module", String(args.module)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/alerts/destinations/${encodeURIComponent(String(args.destination_name))}${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(httpResponse, await res.json()) };