import {z} from 'zod' export type GetApiApiOrgIdStreamNameTracesSessionArgs = {org_id: string, stream_name: string, filter?: string | undefined, keyword?: string | undefined, from: number, size: number, start_time: number, end_time: number, timeout?: number | undefined, sort_by?: string | undefined, sort_order?: string | undefined}; export type GetApiApiOrgIdStreamNameTracesSessionOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const latestSessionsResponse = z.object({from: z.number().int(), function_error: z.string().optional(), has_more: z.boolean(), hits: z.array(z.object({})), size: z.number().int(), took: z.number().int().gte(0), total: z.number().int().gte(0), trace_id: z.string().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 recent session data * * GET /api/{org_id}/{stream_name}/traces/session */ export const getApiApiOrgIdStreamNameTracesSession = async (args: GetApiApiOrgIdStreamNameTracesSessionArgs, options: GetApiApiOrgIdStreamNameTracesSessionOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.filter != null) query.append("filter", String(args.filter)) if (args.keyword != null) query.append("keyword", String(args.keyword)) if (args.from != null) query.append("from", String(args.from)) if (args.size != null) query.append("size", String(args.size)) 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 (args.timeout != null) query.append("timeout", String(args.timeout)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/${encodeURIComponent(String(args.stream_name))}/traces/session${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(latestSessionsResponse, await res.json()) };