import {z} from 'zod' export type GetApiDevicesPingdataArgs = {key?: string | undefined}; export type GetApiDevicesPingdataOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const pingData = z.object({type: z.string().nullable().optional(), snap: z.string().nullable().optional(), player_version: z.string().nullable().optional(), os_version: z.string().nullable().optional(), cpu_usage: z.number().optional(), memory_usage: z.number().optional(), mac_address: z.string().nullable().optional(), disk_usage: z.number().optional(), timestamp: z.string().optional(), bytes_received: z.number().int().optional(), transfer_rate: z.number().int().optional(), ip_address: z.string().nullable().optional(), latitude: z.number().nullable().optional(), longitude: z.number().nullable().optional(), current_schedule: z.string().nullable().optional(), uptime: z.number().int().nullable().optional(), external_ip_address: z.string().nullable().optional(), download_pct_complete: z.number().int().nullable().optional(), meta: z.record(z.string(), z.string().nullable()).nullable().optional(), cpu_temperature: z.number().nullable().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 }; /** * Gets device ping data by registration key. * * GET /devices/pingdata * * Also accepts auth: ApiKeyInHeader, ApiKeyInQuery */ export const getApiDevicesPingdata = async (args: GetApiDevicesPingdataArgs, options?: GetApiDevicesPingdataOptions) => { const {baseUrl = "https://api.reveldigital.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.key != null) query.append("key", String(args.key)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/devices/pingdata${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(pingData, await res.json()) };