import {z} from 'zod' export type GetApiApiOrgIdStreamNameTracesTimeRangeArgs = {org_id: string, stream_name: string, trace_id?: string | undefined, session_id?: string | undefined, hint_ts?: number | undefined, start_time?: number | undefined, end_time?: number | undefined}; export type GetApiApiOrgIdStreamNameTracesTimeRangeOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const traceTimeRange = z.object({end_time: z.number().int(), start_time: z.number().int()}); export const timeRangeStatus = z.enum(["found", "not_found", "timeout"]); export const timeRangeResult = z.object({partial: z.boolean().optional(), range: traceTimeRange.nullable().optional(), session_id: z.string().nullable().optional(), status: timeRangeStatus, stream: z.string().nullable().optional(), trace_id: z.string().nullable().optional()}); export const traceTimeRangeResponse = z.object({partial_coverage: z.boolean().optional(), range: traceTimeRange.nullable().optional(), results: z.array(timeRangeResult), searched_range: traceTimeRange.nullable().optional(), session_id: z.string().nullable().optional(), stream: z.string(), trace_id: z.string().nullable().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 the indexed time range for traces or sessions * * GET /api/{org_id}/{stream_name}/traces/time_range */ export const getApiApiOrgIdStreamNameTracesTimeRange = async (args: GetApiApiOrgIdStreamNameTracesTimeRangeArgs, options: GetApiApiOrgIdStreamNameTracesTimeRangeOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.trace_id != null) query.append("trace_id", String(args.trace_id)) if (args.session_id != null) query.append("session_id", String(args.session_id)) if (args.hint_ts != null) query.append("hint_ts", String(args.hint_ts)) if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/${encodeURIComponent(String(args.stream_name))}/traces/time_range${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(traceTimeRangeResponse, await res.json()) };