import {z} from 'zod' export type GetApiTrackerUniqueObjectsArgs = {fromTimestamp: string, toTimestamp: string, sensorIds?: Array | undefined, objectId?: string | undefined, place?: string | undefined, globalId?: string | undefined, maxResultSize?: number | undefined}; export type GetApiTrackerUniqueObjectsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const location = z.array(z.number()); export const mTMCBehavior = z.object({id: z.string().max(10000).optional(), sensorId: z.string().max(10000), objectId: z.string().max(10000), objectType: z.string().max(10000).optional(), timestamp: z.string().max(24), end: z.string().max(24), startFrame: z.string().max(100000).optional(), endFrame: z.string().max(100000).optional(), place: z.string().max(10000).optional(), locations: z.array(location).optional()}); export const mTMCObject = z.object({batchId: z.string().max(10000), globalId: z.string().max(10000), timestamp: z.string().max(24), end: z.string().max(24), objectType: z.string().max(10000), matched: z.array(mTMCBehavior)}); export const mTMCObjects = z.object({uniqueObjects: z.array(mTMCObject)}); 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 tracked objects * * GET /tracker/unique-objects */ export const getApiTrackerUniqueObjects = async (args: GetApiTrackerUniqueObjectsArgs, options?: GetApiTrackerUniqueObjectsOptions) => { const {baseUrl = "https://ai.nvidia.com/api/v3.3/analytics", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.fromTimestamp != null) query.append("fromTimestamp", String(args.fromTimestamp)) if (args.toTimestamp != null) query.append("toTimestamp", String(args.toTimestamp)) for (const item of args.sensorIds ?? []) query.append("sensorIds", String(item)) if (args.objectId != null) query.append("objectId", String(args.objectId)) if (args.place != null) query.append("place", String(args.place)) if (args.globalId != null) query.append("globalId", String(args.globalId)) if (args.maxResultSize != null) query.append("maxResultSize", String(args.maxResultSize)) const url = `${baseUrl.replace(/\/+$/, '')}/tracker/unique-objects${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(mTMCObjects, await res.json()) };