import {z} from 'zod' export type CreateApiApiOrgIdSyntheticsJobsAckArgs = {org_id: string, body: Record}; export type CreateApiApiOrgIdSyntheticsJobsAckOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const syntheticsAlertDecision = z.enum(["Silent", "Firing", "Recovered", "Flaky", "Degraded"]); export const syntheticsJobAckResponse = z.object({alert: syntheticsAlertDecision, consecutive_failures: z.number().int(), destinations: z.array(z.string()), failing_environments: z.array(z.string()), failing_locations: z.array(z.string()), job_count: z.number().int(), job_id: z.string(), location: z.string(), org_id: z.string(), passing_locations: z.array(z.string()), pool: z.string(), run_complete: z.boolean(), run_id: z.string(), run_status: z.string().nullable().optional(), status_reason: z.string().nullable().optional(), synthetic_type: z.string(), synthetics_id: z.string(), synthetics_name: z.string(), target: z.string(), trigger_type: z.string()}); export const jobAckResult = z.object({error: z.string().nullable().optional(), job_id: z.string(), ok: z.boolean(), run_complete: z.boolean().nullable().optional()}); export const jobAckAnswer = z.union([syntheticsJobAckResponse, z.object({results: z.array(jobAckResult)})]); 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 }; /** * Acknowledge a completed check — probe submits result (authenticated via o2syn_ token) * * POST /api/{org_id}/synthetics/jobs/ack */ export const createApiApiOrgIdSyntheticsJobsAck = async (args: CreateApiApiOrgIdSyntheticsJobsAckArgs, options: CreateApiApiOrgIdSyntheticsJobsAckOptions) => { 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))}/synthetics/jobs/ack${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(jobAckAnswer, await res.json()) };