import {z} from 'zod' export type GetApiV1ProjectsProjectIdAlertsArgs = {project_id: string}; export type GetApiV1ProjectsProjectIdAlertsOptions = {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 jSONBData: z.ZodType = z.union([z.record(z.string(), z.lazy(() => jSONBData)), z.array(z.lazy(() => jSONBData)), z.string(), z.number().int(), z.boolean(), z.number()]).nullable(); export const alertWithLastRun = 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(), last_run: z.string().nullable(), has_matches: z.boolean().nullable(), fired: z.boolean().nullable(), has_errors: z.boolean().nullable(), result_length: z.number().int().nullable(), result: jSONBData}); export const getApiV1ProjectsProjectIdAlertsResponse = z.array(alertWithLastRun); 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 Alerts * * GET /v1/projects/{project_id}/alerts/ */ export const getApiV1ProjectsProjectIdAlerts = async (args: GetApiV1ProjectsProjectIdAlertsArgs, options: GetApiV1ProjectsProjectIdAlertsOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2AuthorizationCodeBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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/${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(getApiV1ProjectsProjectIdAlertsResponse, await res.json()) };