import {z} from 'zod' export type GetApiApiOrgIdSyntheticsLocationsArgs = {org_id: string}; export type GetApiApiOrgIdSyntheticsLocationsOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const syntheticsDevice = z.object({height: z.number().int().gte(0), id: z.string(), label: z.string(), width: z.number().int().gte(0)}); export const locationEntry = z.object({agent_names: z.array(z.string()), agents_total: z.number().int().gte(0), checks_count: z.number().int().gte(0), checks_per_min: z.number(), enabled: z.boolean(), id: z.string(), kind: z.string(), label: z.string(), last_seen_at: z.number().int().nullable().optional(), live_agents: z.number().int().gte(0), live_status_unknown: z.boolean().optional(), pool: z.string(), provider: z.string(), region: z.string(), status: z.string(), types: z.array(z.string()), version: z.string().nullable().optional()}); export const syntheticsCapabilitiesV2 = z.object({browsers: z.array(z.string()), devices: z.array(syntheticsDevice), locations: z.array(locationEntry)}); 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 available probe locations * * GET /api/{org_id}/synthetics/locations */ export const getApiApiOrgIdSyntheticsLocations = async (args: GetApiApiOrgIdSyntheticsLocationsArgs, options: GetApiApiOrgIdSyntheticsLocationsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/synthetics/locations${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(syntheticsCapabilitiesV2, await res.json()) };