26 lines
1.0 KiB
TypeScript
26 lines
1.0 KiB
TypeScript
import { z } from "zod"
|
|
|
|
const metric = z.number().finite().nonnegative().nullable()
|
|
const address = z.string().nullable().optional()
|
|
const response = z.object({
|
|
ok: z.literal(true),
|
|
result: z.object({
|
|
runId: z.string().min(1),
|
|
createdAt: z.string().datetime({ offset: true }).optional(),
|
|
txAvgMbps: metric,
|
|
rxAvgMbps: metric,
|
|
raw: z.array(z.record(z.string(), z.unknown())).optional(),
|
|
afterBtPing: z.object({ rttMs: metric, lossPct: z.number().finite().min(0).max(100).nullable(), error: z.string().nullable() }).optional(),
|
|
srcAddress: address, dstAddress: address,
|
|
srcInterfaceAddress: address, dstInterfaceAddress: address,
|
|
}),
|
|
})
|
|
|
|
export function parseSpeedRunResponse(raw: unknown, runId: string) {
|
|
const parsed = response.safeParse(raw)
|
|
if (!parsed.success || parsed.data.result.runId !== runId) {
|
|
throw new Error("Некорректное подтверждение speed-test. Результат операции неизвестен")
|
|
}
|
|
return parsed.data.result
|
|
}
|