import {z} from 'zod' export type GetApiApiV2OrgIdAlertsAlertIdSloPreviewArgs = {org_id: string, alert_id: string, window_secs: number, slice_interval_secs: number}; export type GetApiApiV2OrgIdAlertsAlertIdSloPreviewOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const alertSliPreviewInterval = z.object({frequency_secs: z.number().int(), from_us: z.number().int(), level: z.string().nullable().optional(), to_us: z.number().int()}); export const alertSliPreview = z.object({alert_id: z.string(), coverage: z.number(), expected_slices: z.number().int(), good_secs: z.number(), intervals: z.array(alertSliPreviewInterval), observed_slices: z.number().int(), range_end_secs: z.number().int(), range_start_secs: z.number().int(), sli: z.number().nullable().optional(), slice_interval_secs: z.number().int(), total_secs: z.number(), would_freeze: 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 }; /** * Preview the uptime an alert would produce as an SLI source * * GET /api/v2/{org_id}/alerts/{alert_id}/slo-preview */ export const getApiApiV2OrgIdAlertsAlertIdSloPreview = async (args: GetApiApiV2OrgIdAlertsAlertIdSloPreviewArgs, options: GetApiApiV2OrgIdAlertsAlertIdSloPreviewOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.window_secs != null) query.append("window_secs", String(args.window_secs)) if (args.slice_interval_secs != null) query.append("slice_interval_secs", String(args.slice_interval_secs)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/${encodeURIComponent(String(args.org_id))}/alerts/${encodeURIComponent(String(args.alert_id))}/slo-preview${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(alertSliPreview, await res.json()) };