import {z} from 'zod' export type CompileOptions = {disableInlining?: Array | undefined, targetDialects?: (Array<'ucast+all' | 'ucast+minimal' | 'ucast+prisma' | 'ucast+linq' | 'sql+sqlserver' | 'sql+mysql' | 'sql+postgresql' | 'sql+sqlite'>) | undefined, targetSQLTableMappings?: ({sqlserver?: {[key: string]: unknown} | undefined, mysql?: {[key: string]: unknown} | undefined, postgresql?: {[key: string]: unknown} | undefined, sqlite?: {[key: string]: unknown} | undefined}) | undefined, maskRule?: string | undefined} | {[key: string]: unknown}; export type CompileUnknowns = Array; export type Input = boolean | string | number | Array | {[key: string]: unknown}; export type CreateApiV1CompilePathArgs = {path: unknown, 'Accept-Encoding'?: 'gzip' | undefined, 'Content-Encoding'?: 'gzip' | undefined, pretty?: boolean | undefined, explain?: ('notes' | 'fails' | 'full' | 'debug') | undefined, metrics?: boolean | undefined, instrument?: boolean | undefined, body: {options?: CompileOptions | undefined, unknowns?: CompileUnknowns | undefined, input?: Input | undefined}}; export type CreateApiV1CompilePathOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const compileResultJSON = z.object({result: z.object({query: z.object({}).optional(), support: z.object({}).optional()}).and(z.record(z.string(), z.unknown())).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 }; /** * Partially evaluate a query * * POST /v1/compile/{path} */ export const createApiV1CompilePath = async (args: CreateApiV1CompilePathArgs, options?: CreateApiV1CompilePathOptions) => { const {baseUrl = "http://localhost:8181", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Accept-Encoding'] != null) headers.set("Accept-Encoding", String(args['Accept-Encoding'])) if (args['Content-Encoding'] != null) headers.set("Content-Encoding", String(args['Content-Encoding'])) if (args.pretty != null) query.append("pretty", String(args.pretty)) 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)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/compile/${encodeURIComponent(String(args.path))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(compileResultJSON, await res.json()) };