import {z} from 'zod' export type GetApiAlertsSevereArgs = {fromTimestamp: string, toTimestamp: string, place?: string | undefined, sensorId?: string | undefined, severeAlertTypes?: Array | undefined, vlmVerified?: boolean | undefined, vlmVerdict?: ('all' | 'confirmed' | 'rejected' | 'verification-failed' | 'not-confirmed') | undefined}; export type GetApiAlertsSevereOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const severeAlerts = z.object({severeAlerts: z.union([z.object({places: z.array(z.string().max(10000))}), z.object({sensors: z.array(z.string().max(10000))}), z.object({places: z.array(z.string().max(10000)), sensors: z.array(z.string().max(10000))})]).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 severe alerts by place or sensor * * GET /alerts/severe */ export const getApiAlertsSevere = async (args: GetApiAlertsSevereArgs, options?: GetApiAlertsSevereOptions) => { 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)) if (args.place != null) query.append("place", String(args.place)) if (args.sensorId != null) query.append("sensorId", String(args.sensorId)) for (const item of args.severeAlertTypes ?? []) query.append("severeAlertTypes", String(item)) if (args.vlmVerified != null) query.append("vlmVerified", String(args.vlmVerified)) if (args.vlmVerdict != null) query.append("vlmVerdict", String(args.vlmVerdict)) const url = `${baseUrl.replace(/\/+$/, '')}/alerts/severe${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(severeAlerts, await res.json()) };