import {z} from 'zod' export type GetApiApiOrgIdQueryHistoryArgs = {org_id: string, source?: string | undefined, status?: string | undefined, stream?: string | undefined, q?: string | undefined, starred?: boolean | undefined, ran_from?: number | undefined, ran_to?: number | undefined, limit?: number | undefined, offset?: number | undefined, count_only?: boolean | undefined}; export type GetApiApiOrgIdQueryHistoryOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const queryHistoryEntry = z.object({context: z.unknown(), created_at: z.number().int(), id: z.string(), query: z.string(), query_key: z.string().nullable().optional(), run_count: z.number().int(), source: z.string(), starred: z.boolean(), status: z.string(), streams: z.array(z.string()), updated_at: z.number().int()}); export const queryHistoryCountResponse = z.object({count: z.number().int().gte(0)}); export const queryHistoryListResponse = z.union([z.array(queryHistoryEntry), queryHistoryCountResponse]); 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 }; /** * List query history * * GET /api/{org_id}/query_history */ export const getApiApiOrgIdQueryHistory = async (args: GetApiApiOrgIdQueryHistoryArgs, options: GetApiApiOrgIdQueryHistoryOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.source != null) query.append("source", String(args.source)) if (args.status != null) query.append("status", String(args.status)) if (args.stream != null) query.append("stream", String(args.stream)) if (args.q != null) query.append("q", String(args.q)) if (args.starred != null) query.append("starred", String(args.starred)) if (args.ran_from != null) query.append("ran_from", String(args.ran_from)) if (args.ran_to != null) query.append("ran_to", String(args.ran_to)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.count_only != null) query.append("count_only", String(args.count_only)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/query_history${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(queryHistoryListResponse, await res.json()) };