import {z} from 'zod' export type GetApiApiSessionsSessionIdCaptureArgs = {sessionId: string, includeSnapshots?: ('none' | 'metadata' | 'full') | undefined, includeCookieValues?: boolean | undefined}; export type GetApiApiSessionsSessionIdCaptureOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sessionCapture = z.object({sessionId: z.string(), platform: z.string(), startedAt: z.string().nullable().optional(), endedAt: z.string().nullable().optional(), browserContext: z.record(z.string(), z.unknown()).nullable().optional(), events: z.array(z.object({type: z.string(), ts: z.number().int()}).and(z.record(z.string(), z.unknown()))).optional(), storageState: z.record(z.string(), z.unknown()).nullable().optional(), snapshotCount: z.number().int(), snapshotsPrefix: z.string().nullable().optional(), snapshots: z.array(z.object({id: z.string(), ts: z.number().int(), messageId: z.string().nullable().optional(), trigger: z.enum(["screenshot", "page_load"]), targetId: z.string(), url: z.string(), title: z.string().optional(), dom: z.unknown(), axTree: z.unknown()})).optional(), truncated: z.boolean(), createdAt: z.string().optional(), updatedAt: 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 session capture metadata * * GET /api/sessions/{sessionId}/capture */ export const getApiApiSessionsSessionIdCapture = async (args: GetApiApiSessionsSessionIdCaptureArgs, options: GetApiApiSessionsSessionIdCaptureOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.includeSnapshots != null) query.append("includeSnapshots", String(args.includeSnapshots)) if (args.includeCookieValues != null) query.append("includeCookieValues", String(args.includeCookieValues)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/sessions/${encodeURIComponent(String(args.sessionId))}/capture${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(sessionCapture, await res.json()) };