import {z} from 'zod' export type GetApiApiOrgIdSyntheticsArgs = {org_id: string, folder?: string | undefined, type?: string | undefined, enabled?: boolean | undefined, location?: string | undefined, tag?: string | undefined, page?: number | undefined, page_size?: number | undefined}; export type GetApiApiOrgIdSyntheticsOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const alertGroupRef = z.object({alert_id: z.string(), group_key: z.string().optional(), group_labels: z.record(z.string(), z.string()).nullable().optional()}); export const endOnClear = z.object({alert_id: z.string(), group_key: z.string().nullable().optional()}); export const incidentMode = z.enum(["muted", "none"]); export const muteSource = z.enum(["slack", "openobserve", "api"]); export const activeDowntime = z.object({alert_groups: z.array(alertGroupRef).nullable().optional(), end_on_clear: endOnClear.nullable().optional(), ends_at: z.number().int(), id: z.string(), incident_mode: incidentMode.optional(), name: z.string(), source: muteSource.nullable().optional(), version: z.number().int().optional()}); export const syntheticFrequencyType = z.enum(["seconds", "minutes", "hours", "days", "weeks", "months", "cron"]); export const syntheticFrequency = z.object({cron: z.string().optional(), interval: z.number().int().optional(), timezone: z.string().nullable().optional(), type: syntheticFrequencyType.optional()}); export const referenceState = z.enum(["ok", "missing", "nested"]); export const syntheticStatus = z.enum(["passed", "warning", "failed", "unknown", "error"]); export const syntheticType = z.enum(["http", "tcp", "tls", "ssh", "browser"]); export const syntheticListItem = z.object({active_downtime: activeDowntime.nullable().optional(), created_at: z.number().int(), description: z.string(), enabled: z.boolean(), folder_id: z.string(), frequency: syntheticFrequency, id: z.string(), last_check_at: z.number().int().nullable().optional(), last_response_ms: z.number().nullable().optional(), last_triggered_at: z.number().int(), locations: z.array(z.string()), name: z.string(), org_id: z.string(), reference_state: referenceState.nullable().optional(), referenced_by: z.number().int(), references: z.number().int().nullable().optional(), status: syntheticStatus, steps: z.number().int().nullable().optional(), tags: z.array(z.string()), target: z.string(), type: syntheticType, updated_at: z.number().int()}); export const syntheticListResponse = z.object({checks: z.array(syntheticListItem), total: z.number().int()}); 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 synthetics * * GET /api/{org_id}/synthetics */ export const getApiApiOrgIdSynthetics = async (args: GetApiApiOrgIdSyntheticsArgs, options: GetApiApiOrgIdSyntheticsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.folder != null) query.append("folder", String(args.folder)) if (args.type != null) query.append("type", String(args.type)) if (args.enabled != null) query.append("enabled", String(args.enabled)) if (args.location != null) query.append("location", String(args.location)) if (args.tag != null) query.append("tag", String(args.tag)) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/synthetics${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(syntheticListResponse, await res.json()) };