import {z} from 'zod' export type AlertChannelAssignment = {channel_id: string, schedule_id?: (string | null) | undefined}; export type AlertInstancePolicy = {identity_columns: Array, pending_runs: number, missing_runs: number, missing_behavior: 'no_longer_matching' | 'expire', continuity_runs: number}; export type AlertUpdate = {name?: string | undefined, description?: string | undefined, time_window?: unknown | undefined, frequency?: unknown | undefined, watermark?: unknown | undefined, active?: boolean | undefined, query?: string | undefined, channel_ids?: (Array | null) | undefined, channel_assignments?: Array | undefined, notify_when?: ('has_matches' | 'has_matches_changed' | 'matches_changed' | 'starts_having_matches') | undefined, environments?: Array | undefined, instance_policy?: (AlertInstancePolicy | null) | undefined}; export type UpdateApiV1ProjectsProjectIdAlertsAlertIdArgs = {alert_id: string, project_id: string, body: AlertUpdate}; export type UpdateApiV1ProjectsProjectIdAlertsAlertIdOptions = {baseUrl: string, OAuth2AuthorizationCodeBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const webhookFormat = z.enum(["auto", "slack-blockkit", "slack-legacy", "raw-data"]); export const webhook = z.object({type: z.literal("webhook"), format: webhookFormat, url: z.string()}); export const opsgenie = z.object({type: z.literal("opsgenie"), auth_key: z.string(), base_url: z.string().optional()}); export const pagerdutyRegion = z.enum(["us", "eu"]); export const pagerduty = z.object({type: z.literal("pagerduty"), routing_key: z.string(), region: pagerdutyRegion.optional()}); export const pagerdutyIntegration = z.object({type: z.literal("pagerduty-integration"), install_id: z.string(), service_id: z.string()}); export const notification = z.object({type: z.literal("notification"), recipients: z.array(z.string())}); export const slackIntegration = z.object({type: z.literal("slack-integration"), install_id: z.string(), channel_id: z.string(), include_agent_prompt: z.boolean().optional()}); export const email = z.object({type: z.literal("email"), recipients: z.array(z.string())}); export const webPush = z.object({type: z.literal("web-push"), recipients: z.array(z.string())}); export const alertChannelRead = z.object({id: z.string(), organization_id: z.string(), label: z.string(), active: z.boolean(), created_at: z.string(), updated_at: z.string().nullable(), created_by_name: z.string().nullable(), updated_by_name: z.string().nullable(), config: z.discriminatedUnion("type", [webhook, opsgenie, pagerduty, pagerdutyIntegration, notification, slackIntegration, email, webPush]), schedule_id: z.string().nullable()}); export const alertChannelAssignmentRead = z.object({channel_id: z.string(), schedule_id: z.string().nullable()}); export const alertInstancePolicy = z.object({identity_columns: z.array(z.string().min(1).max(128)), pending_runs: z.number().int().gte(1).lte(100), missing_runs: z.number().int().gte(1).lte(100), missing_behavior: z.enum(["no_longer_matching", "expire"]), continuity_runs: z.number().int().gte(1).lte(100)}); export const alertSuppression = z.object({reason: z.enum(["paused", "snoozed", "target_too_low"]), until: z.string().nullable(), evaluated: z.boolean()}); export const alertQueryCheck = z.object({status: z.enum(["ok", "slow", "timed_out", "not_checked"]), duration_ms: z.number().int().nullable(), timed_out: z.boolean(), message: z.string()}); export const alertSaveResult = z.object({id: z.string(), organization_id: z.string(), project_id: z.string(), created_at: z.string(), updated_at: z.string().nullable(), created_by_name: z.string().nullable(), updated_by_name: z.string().nullable(), name: z.string(), description: z.string().nullable(), query: z.string(), time_window: z.string(), frequency: z.string(), watermark: z.string(), environments: z.array(z.string()), channels: z.array(alertChannelRead), channel_assignments: z.array(alertChannelAssignmentRead), notify_when: z.enum(["has_matches", "has_matches_changed", "matches_changed", "starts_having_matches"]), active: z.boolean(), slo_id: z.string().nullable().optional(), instance_policy: alertInstancePolicy.nullable().optional(), integration_id: z.string().nullable().optional(), schedule_warning: z.string().nullable().optional(), slo_tier: z.number().int().nullable().optional(), slo_tier_viable: z.boolean().nullable().optional(), slo_bucket_tier: z.number().int().nullable().optional(), snoozed_until: z.string().nullable(), suppression: alertSuppression.nullable(), query_check: alertQueryCheck.nullable()}); 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 * * PUT /v1/projects/{project_id}/alerts/{alert_id}/ */ export const updateApiV1ProjectsProjectIdAlertsAlertId = async (args: UpdateApiV1ProjectsProjectIdAlertsAlertIdArgs, options: UpdateApiV1ProjectsProjectIdAlertsAlertIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2AuthorizationCodeBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2AuthorizationCodeBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2AuthorizationCodeBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(String(args.project_id))}/alerts/${encodeURIComponent(String(args.alert_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(alertSaveResult, await res.json()) };