import {z} from 'zod' export type CreateApiV0DataPathArgs = {pretty?: boolean | undefined, body: BodyInit}; export type CreateApiV0DataPathOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const createApiV0DataPathResponse = 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 (with webhook) * * POST /v0/data/{path} */ export const createApiV0DataPath = async (args: CreateApiV0DataPathArgs, options: CreateApiV0DataPathOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/yaml") headers.set('accept', "application/json") if (args.pretty != null) query.append("pretty", String(args.pretty)) const url = `${baseUrl.replace(/\/+$/, '')}/v0/data/${encodeURIComponent(String(args.path))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV0DataPathResponse, await res.json()) };