import {z} from 'zod' export type AutoInvestigate = 'off' | 'critical'; export type MentionRule = 'default' | 'here' | 'channel' | 'none' | {subteam: {handle: string, subteam_id: string}}; export type SlackAppFields = {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}; export type DestinationType = 'http' | 'email' | 'sns' | 'slack_app'; export type CreateApiApiOrgIdAlertsDestinationsTestArgs = {org_id: string, body: {body?: (string | null) | undefined, headers?: ({[key: string]: string} | null) | undefined, method?: (string | null) | undefined, recipients?: (Array | null) | undefined, skipTlsVerify?: (boolean | null) | undefined, slackApp?: (SlackAppFields | null) | undefined, template?: (string | null) | undefined, type?: DestinationType | undefined, url?: string | undefined}}; export type CreateApiApiOrgIdAlertsDestinationsTestOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiOrgIdAlertsDestinationsTestResponse = z.object({error: z.string().nullable().optional(), responseBody: z.string().nullable().optional(), statusCode: z.number().int().gte(0).nullable().optional(), success: z.boolean()}); 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 }; /** * Test alert destination * * POST /api/{org_id}/alerts/destinations/test */ export const createApiApiOrgIdAlertsDestinationsTest = async (args: CreateApiApiOrgIdAlertsDestinationsTestArgs, options: CreateApiApiOrgIdAlertsDestinationsTestOptions) => { 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 (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/alerts/destinations/test${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(createApiApiOrgIdAlertsDestinationsTestResponse, await res.json()) };