import {z} from 'zod' export type GetApiAdhawkDeviceplaystatsArgs = {startDate?: string | undefined, endDate?: string | undefined, deviceId?: Array | undefined, fileId?: Array | undefined, limit?: number | undefined}; export type GetApiAdhawkDeviceplaystatsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const adHawkDevicePlayStats = z.object({device_id: z.string().nullable().optional(), device_name: z.string().nullable().optional(), total_plays: z.number().int().optional(), total_duration: z.number().int().optional(), avg_duration: z.number().nullable().optional(), file_count: z.number().int().optional(), first_play: z.string().nullable().optional(), last_play: z.string().nullable().optional()}); export const getApiAdhawkDeviceplaystatsResponse = z.array(adHawkDevicePlayStats); 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 play statistics aggregated by device. * * GET /adhawk/deviceplaystats * * Also accepts auth: ApiKeyInHeader, ApiKeyInQuery */ export const getApiAdhawkDeviceplaystats = async (args: GetApiAdhawkDeviceplaystatsArgs, options?: GetApiAdhawkDeviceplaystatsOptions) => { 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.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) for (const item of args.deviceId ?? []) query.append("deviceId", String(item)) for (const item of args.fileId ?? []) query.append("fileId", String(item)) if (args.limit != null) query.append("limit", String(args.limit)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/adhawk/deviceplaystats${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(getApiAdhawkDeviceplaystatsResponse, await res.json()) };