import {z} from 'zod' export type GetApiV1DataPathArgs = {input?: {[key: string]: unknown} | undefined, pretty?: boolean | undefined, provenance?: boolean | undefined, explain?: string | undefined, metrics?: boolean | undefined, instrument?: boolean | undefined}; export type GetApiV1DataPathOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const getApiV1DataPathResponse = z.object({result: z.array(z.object({id: z.string().min(1).optional(), raw: z.string().min(1).optional(), ast: z.object({package: z.object({path: z.array(z.object({type: z.enum(["import", "package"]).optional(), value: z.string().optional()})).optional()}).optional(), rules: z.array(z.object({head: z.object({name: z.string().optional(), key: z.object({type: z.string().optional(), value: z.string().optional()}).optional()}).optional(), body: z.array(z.object({index: z.number().optional(), terms: z.array(z.object({type: z.string().optional(), value: z.array(z.object({type: z.string().optional(), value: z.string().optional()})).optional()})).optional()})).optional()})).optional()}).optional()})).optional(), provenance: z.object({version: z.string().optional(), build_commit: z.string().optional(), build_timestamp: z.string().optional(), build_hostname: z.string().optional(), bundles: z.record(z.string(), z.unknown()).optional()}).optional(), metrics: z.object({timer_rego_input_parse_ns: z.number().optional(), timer_rego_query_parse_ns: z.number().optional(), timer_rego_query_compile_ns: z.number().optional(), timer_rego_query_eval_ns: z.number().optional(), timer_rego_module_parse_ns: z.number().optional(), timer_rego_module_compile_ns: z.number().optional(), timer_server_handler_ns: z.number().optional(), timer_server_read_bytes_ns: z.number().optional(), counter_server_query_cache_hit: z.number().optional(), timer_query_compile_stage_build_comprehension_index_ns: z.number().optional(), timer_query_compile_stage_check_safety_ns: z.number().optional(), timer_query_compile_stage_check_types_ns: z.number().optional(), timer_query_compile_stage_check_undefined_funcs_ns: z.number().optional(), timer_query_compile_stage_check_unsafe_builtins_ns: z.number().optional(), timer_query_compile_stage_resolve_refs_ns: z.number().optional(), timer_query_compile_stage_rewrite_comprehension_terms_ns: z.number().optional(), timer_query_compile_stage_rewrite_dynamic_terms_ns: z.number().optional(), timer_query_compile_stage_rewrite_expr_terms_ns: z.number().optional(), timer_query_compile_stage_rewrite_local_vars_ns: z.number().optional(), timer_query_compile_stage_rewrite_to_capture_value_ns: z.number().optional(), timer_query_compile_stage_rewrite_with_values_ns: z.number().optional()}).optional(), explanation: z.array(z.object({op: z.enum(["enter", "exit", "eval", "fail", "redo"]).optional(), query_id: z.number().gte(0).optional(), parent_id: z.number().gte(0).optional(), type: z.enum(["expr", "rule", "body"]).optional(), node: z.object({index: z.number().optional(), terms: z.array(z.object({type: z.string().optional(), value: z.string().optional()})).optional()}).optional(), locals: z.array(z.object({key: z.object({type: z.string().optional(), value: z.string().optional()}).optional(), value: z.object({type: z.string().optional(), value: z.string().optional()}).optional()})).optional()})).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 }; /** * Get a document * * GET /v1/data/{path} */ export const getApiV1DataPath = async (args: GetApiV1DataPathArgs, options: GetApiV1DataPathOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.input != null) query.append("input", JSON.stringify(args.input)) if (args.pretty != null) query.append("pretty", String(args.pretty)) if (args.provenance != null) query.append("provenance", String(args.provenance)) if (args.explain != null) query.append("explain", String(args.explain)) if (args.metrics != null) query.append("metrics", String(args.metrics)) if (args.instrument != null) query.append("instrument", String(args.instrument)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/data/${encodeURIComponent(String(args.path))}${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(getApiV1DataPathResponse, await res.json()) };