import {z} from 'zod' export type GetApiSessionsMetricsArgs = {period_start_at?: string | undefined, period_end_at?: string | undefined, granularity?: ('hour' | 'day') | undefined, time_zone?: string | undefined, group_by?: ('allocated_by' | 'device_class' | 'platform') | undefined}; export type GetApiSessionsMetricsOptions = {baseUrl: string, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const phoneSessionMetricsPoint = z.object({allocated_by: z.enum(["dashboard_run", "api_run", "interactive"]).optional(), device_class: z.literal("phone").optional(), period_start_at: z.string(), platform: z.literal("android").optional(), session_cost_microdollars: z.number().int(), session_seconds: z.number().int(), sessions: z.number().int()}); export const phoneSessionMetricsResponse = z.object({$schema: z.string().optional(), points: z.array(phoneSessionMetricsPoint).nullable()}); 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 phone session metrics * * GET /sessions/metrics */ export const getApiSessionsMetrics = async (args: GetApiSessionsMetricsArgs, options: GetApiSessionsMetricsOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.period_start_at != null) query.append("period_start_at", String(args.period_start_at)) if (args.period_end_at != null) query.append("period_end_at", String(args.period_end_at)) if (args.granularity != null) query.append("granularity", String(args.granularity)) if (args.time_zone != null) query.append("time_zone", String(args.time_zone)) if (args.group_by != null) query.append("group_by", String(args.group_by)) if (apiKeyAuth !== undefined) headers.set("X-Axilio-Api-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/sessions/metrics${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(phoneSessionMetricsResponse, await res.json()) };