import {z} from 'zod' export type NetworkStatsLeg = {packets_lost_pct?: (number | null) | undefined, concealment_pct?: (number | null) | undefined, jitter_p95_ms?: (number | null) | undefined, jitter_avg_ms?: (number | null) | undefined, rtt_p95_ms?: (number | null) | undefined, freeze_count?: (number | null) | undefined, total_freezes_duration_ms?: (number | null) | undefined, samples?: (number | null) | undefined, duration_s?: (number | null) | undefined, worst_window?: ({[key: string]: unknown} | null) | undefined}; export type NetworkStatsPayload = {client_recv?: (NetworkStatsLeg | null) | undefined, client_send?: (NetworkStatsLeg | null) | undefined, user_agent?: (string | null) | undefined, livekit_region?: (string | null) | undefined, client_version?: (string | null) | undefined}; export type CreateApiV1SessionsSessionIdNetworkStatsArgs = {session_id: string, body: NetworkStatsPayload}; export type CreateApiV1SessionsSessionIdNetworkStatsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const createApiV1SessionsSessionIdNetworkStatsResponse = z.unknown(); 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 }; /** * Submit Session Network Stats * * POST /v1/sessions/{session_id}/network-stats */ export const createApiV1SessionsSessionIdNetworkStats = async (args: CreateApiV1SessionsSessionIdNetworkStatsArgs, options: CreateApiV1SessionsSessionIdNetworkStatsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/sessions/${encodeURIComponent(String(args.session_id))}/network-stats${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(createApiV1SessionsSessionIdNetworkStatsResponse, await res.json()) };