import {z} from 'zod' export type GetApiV1IspMetricsTypeArgs = {type: string, beginTimestamp?: string | undefined, endTimestamp?: string | undefined, duration?: string | undefined}; export type GetApiV1IspMetricsTypeOptions = {baseUrl?: string | undefined, siteManagerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1IspMetricsTypeResponse = z.object({data: z.array(z.object({metricType: z.string().optional(), periods: z.array(z.object({data: z.object({wan: z.object({avgLatency: z.number().int().optional(), download_kbps: z.number().int().optional(), downtime: z.number().int().optional(), ispAsn: z.string().optional(), ispName: z.string().optional(), maxLatency: z.number().int().optional(), packetLoss: z.number().int().optional(), upload_kbps: z.number().int().optional(), uptime: z.number().int().optional()}).optional()}).optional(), metricTime: z.string().optional(), version: z.string().optional()}).nullable()).optional(), hostId: z.string().optional(), siteId: z.string().optional()}).nullable()).optional(), httpStatusCode: z.number().int().optional(), traceId: 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 ISP Metrics * * GET /v1/isp-metrics/{type} */ export const getApiV1IspMetricsType = async (args: GetApiV1IspMetricsTypeArgs, options?: GetApiV1IspMetricsTypeOptions) => { const {baseUrl = "https://api.ui.com", fetch: fetchFn = fetch, siteManagerApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.beginTimestamp != null) query.append("beginTimestamp", String(args.beginTimestamp)) if (args.endTimestamp != null) query.append("endTimestamp", String(args.endTimestamp)) if (args.duration != null) query.append("duration", String(args.duration)) if (siteManagerApiKey !== undefined) headers.set("X-API-Key", siteManagerApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/isp-metrics/${encodeURIComponent(String(args.type))}${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(getApiV1IspMetricsTypeResponse, await res.json()) };