import {z} from 'zod' export type AdHawkIntervalType = 'Minute' | 'Hour' | 'Day' | 'Week' | 'Month'; export type GetApiAdhawkPlaystatsbytimeArgs = {startDate?: string | undefined, endDate?: string | undefined, deviceId?: Array | undefined, fileId?: Array | undefined, interval?: AdHawkIntervalType | undefined, groupByDevice?: boolean | undefined, groupByFile?: boolean | undefined, limit?: number | undefined}; export type GetApiAdhawkPlaystatsbytimeOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const adHawkPlayStatsByTime = z.object({device_id: z.string().nullable().optional(), device_name: z.string().nullable().optional(), file_id: z.string().nullable().optional(), file_name: z.string().nullable().optional(), period_start: z.string().optional(), period_end: z.string().optional(), total_plays: z.number().int().optional(), total_duration: z.number().int().optional(), avg_duration: z.number().nullable().optional(), device_count: z.number().int().nullable().optional(), file_count: z.number().int().nullable().optional()}); export const getApiAdhawkPlaystatsbytimeResponse = z.array(adHawkPlayStatsByTime); 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 time-series play statistics aggregated by interval. * * GET /adhawk/playstatsbytime * * Also accepts auth: ApiKeyInHeader, ApiKeyInQuery */ export const getApiAdhawkPlaystatsbytime = async (args: GetApiAdhawkPlaystatsbytimeArgs, options?: GetApiAdhawkPlaystatsbytimeOptions) => { 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.interval != null) query.append("interval", String(args.interval)) if (args.groupByDevice != null) query.append("groupByDevice", String(args.groupByDevice)) if (args.groupByFile != null) query.append("groupByFile", String(args.groupByFile)) if (args.limit != null) query.append("limit", String(args.limit)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/adhawk/playstatsbytime${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(getApiAdhawkPlaystatsbytimeResponse, await res.json()) };