import {z} from 'zod' export type GetApiSkynetAssetIdEventListArgs = {key: unknown, monitor_id?: string | undefined, start_time?: number | undefined, end_time?: number | undefined, pn?: number | undefined, ps?: number | undefined}; export type GetApiSkynetAssetIdEventListOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const commonStatus = z.string(); export const dtoEvent = z.object({asset_id: z.string().optional(), event_type: z.enum(["enter", "exit", "speeding", "idle"]).optional(), geofence_id: z.string().optional(), monitor_id: z.string().optional(), monitor_tags: z.array(z.string()).optional(), prev_location: z.object({location: z.object({lat: z.number().optional(), lon: z.number().optional()}).optional(), timestamp: z.number().int().optional(), speed: z.number().optional(), bearing: z.number().optional(), meta_data: z.object({}).optional()}).optional(), timestamp: z.number().int().optional(), triggered_location: z.object({location: z.object({lat: z.number().optional(), lon: z.number().optional()}).optional(), timestamp: z.number().int().optional(), speed: z.number().optional(), bearing: z.number().optional(), meta_data: z.object({}).optional()}).optional(), triggered_timestamp: z.number().int().optional(), extra: z.object({}).optional()}); export const dtoPagination = z.object({total: z.number().int().optional(), page: z.number().int().optional(), size: z.number().int().optional(), hasmore: z.boolean().optional()}); export const commonMessage = z.string(); export const dtoGetEventHisotryResp = z.object({status: commonStatus.optional(), data: z.object({list: z.array(dtoEvent).optional(), page: dtoPagination.optional()}).optional(), message: commonMessage.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 }; /** * Event History of an Asset * * GET /skynet/asset/{id}/event/list */ export const getApiSkynetAssetIdEventList = async (args: GetApiSkynetAssetIdEventListArgs, options?: GetApiSkynetAssetIdEventListOptions) => { const {baseUrl = "https://api.nextbillion.io", fetch: fetchFn = fetch, apiKey} = 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 (args.monitor_id != null) query.append("monitor_id", String(args.monitor_id)) if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (args.pn != null) query.append("pn", String(args.pn)) if (args.ps != null) query.append("ps", String(args.ps)) if (apiKey !== undefined) query.set("key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/skynet/asset/${encodeURIComponent(String(args.id))}/event/list${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(dtoGetEventHisotryResp, await res.json()) };