import {z} from 'zod' export type SyntheticAuth = {password: string, type: 'basic', username: string} | {token: string, type: 'bearer'} | {secret_name: string, type: 'secret'}; export type SyntheticCookie = {domain: string, http_only?: boolean | undefined, name: string, path?: string | undefined, secure?: boolean | undefined, value: string}; export type SyntheticFrequencyType = 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'cron'; export type SyntheticFrequency = {cron?: string | undefined, interval?: number | undefined, timezone?: (string | null) | undefined, type?: SyntheticFrequencyType | undefined}; export type SyntheticStatus = 'passed' | 'warning' | 'failed' | 'unknown' | 'error'; export type SyntheticType = 'http' | 'tcp' | 'tls' | 'ssh' | 'browser'; export type SyntheticVariable = {example?: string | undefined, name: string, secure?: boolean | undefined, value: string}; export type Synthetic = {alert_if_fails?: number | undefined, auth?: (SyntheticAuth | null) | undefined, collect_rum_data?: boolean | undefined, config: unknown, cookies?: Array | undefined, cooldown_mins?: number | undefined, created_at?: number | undefined, description?: string | undefined, destinations?: Array | undefined, enabled: boolean, environments?: Array | undefined, folder_id?: string | undefined, frequency: SyntheticFrequency, id?: string | undefined, last_check_status?: SyntheticStatus | undefined, last_triggered_at?: number | undefined, locations: Array, name: string, next_run_at?: number | undefined, org_id?: string | undefined, owner?: (string | null) | undefined, retries?: number | undefined, session_replay?: boolean | undefined, start?: (number | null) | undefined, tags?: Array | undefined, target: string, template?: (string | null) | undefined, type: SyntheticType, tz_offset?: number | undefined, updated_at?: number | undefined, variables?: Array | undefined, wait_before_retry_secs?: number | undefined}; export type UpdateApiApiOrgIdSyntheticsIdArgs = {org_id: string, id: string, folder?: string | undefined, body: Synthetic}; export type UpdateApiApiOrgIdSyntheticsIdOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const syntheticAuth = z.union([z.object({password: z.string(), type: z.literal("basic"), username: z.string()}), z.object({token: z.string(), type: z.literal("bearer")}), z.object({secret_name: z.string(), type: z.literal("secret")})]); export const syntheticCookie = z.object({domain: z.string(), http_only: z.boolean().optional(), name: z.string(), path: z.string().optional(), secure: z.boolean().optional(), value: z.string()}); 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 syntheticStatus = z.enum(["passed", "warning", "failed", "unknown", "error"]); export const syntheticType = z.enum(["http", "tcp", "tls", "ssh", "browser"]); export const syntheticVariable = z.object({example: z.string().optional(), name: z.string(), secure: z.boolean().optional(), value: z.string()}); export const synthetic = z.object({alert_if_fails: z.number().int().optional(), auth: syntheticAuth.nullable().optional(), collect_rum_data: z.boolean().optional(), config: z.unknown(), cookies: z.array(syntheticCookie).optional(), cooldown_mins: z.number().int().optional(), created_at: z.number().int().optional(), description: z.string().optional(), destinations: z.array(z.string()).optional(), enabled: z.boolean(), environments: z.array(z.string()).optional(), folder_id: z.string().optional(), frequency: syntheticFrequency, id: z.string().optional(), last_check_status: syntheticStatus.optional(), last_triggered_at: z.number().int().optional(), locations: z.array(z.string()), name: z.string(), next_run_at: z.number().int().optional(), org_id: z.string().optional(), owner: z.string().nullable().optional(), retries: z.number().int().optional(), session_replay: z.boolean().optional(), start: z.number().int().nullable().optional(), tags: z.array(z.string()).optional(), target: z.string(), template: z.string().nullable().optional(), type: syntheticType, tz_offset: z.number().int().optional(), updated_at: z.number().int().optional(), variables: z.array(syntheticVariable).optional(), wait_before_retry_secs: z.number().int().optional()}); 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 }; /** * Update a synthetic * * PUT /api/{org_id}/synthetics/{id} */ export const updateApiApiOrgIdSyntheticsId = async (args: UpdateApiApiOrgIdSyntheticsIdArgs, options: UpdateApiApiOrgIdSyntheticsIdOptions) => { 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 (args.folder != null) query.append("folder", String(args.folder)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/synthetics/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(synthetic, await res.json()) };