import {z} from 'zod' export type GetApiApiOrgIdSyntheticsIdResolvedVariablesArgs = {org_id: string, id: string, envs?: string | undefined}; export type GetApiApiOrgIdSyntheticsIdResolvedVariablesOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const syntheticsVariableKind = z.enum(["plain", "secret"]); export const resolvedVariableView = z.object({description: z.string(), example: z.string(), has_value: z.boolean(), kind: syntheticsVariableKind, name: z.string(), overridden: z.boolean(), scope: z.string()}); export const resolvedVariablesGrouped = z.object({environments: z.array(z.string()), resolved: z.record(z.string(), z.array(resolvedVariableView))}); export const resolvedVariablesAnswer = z.union([z.array(resolvedVariableView), resolvedVariablesGrouped]); 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 }; /** * The check's resolved variable set, with the scope each name comes from * * GET /api/{org_id}/synthetics/{id}/resolved-variables */ export const getApiApiOrgIdSyntheticsIdResolvedVariables = async (args: GetApiApiOrgIdSyntheticsIdResolvedVariablesArgs, options: GetApiApiOrgIdSyntheticsIdResolvedVariablesOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.envs != null) query.append("envs", String(args.envs)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/synthetics/${encodeURIComponent(String(args.id))}/resolved-variables${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(resolvedVariablesAnswer, await res.json()) };