import {z} from 'zod' export type GetApiCsrDataArgs = {deviceid?: number | undefined, deviceids?: string | undefined, timestart?: number | undefined, timeend?: number | undefined, topicname?: string | undefined, topicnames?: string | undefined, limit?: number | undefined, startafter?: number | undefined}; export type GetApiCsrDataOptions = {baseUrl?: string | undefined, apiKeyAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const successProperty = z.boolean(); export const paginationLinks = z.object({path: z.string().optional(), base: z.string().optional(), next: z.string().optional()}); export const cSRDataMessage = z.object({id: z.number().int().optional(), logged: z.string().optional(), expires: z.string().nullable().optional(), orgid: z.number().int().optional(), deviceid: z.number().int().optional(), record_id: z.string().optional(), device_metadata: z.string().nullable().optional(), data: z.string().optional(), tags: z.array(z.string()).optional(), matched_rules: z.array(z.object({ruleid: z.number().int().optional(), error: z.boolean().optional(), result_data: z.string().optional()})).optional()}); export const cSRDataMessageListResponse = z.object({success: successProperty.optional(), limit: z.number().int().optional(), size: z.number().int().optional(), continues: z.boolean().optional(), lastid: z.number().int().optional(), links: paginationLinks.optional(), data: z.array(cSRDataMessage).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 }; /** * List events * * GET /csr/data */ export const getApiCsrData = async (args: GetApiCsrDataArgs, options?: GetApiCsrDataOptions) => { const {baseUrl = "https://dashboard.hologram.io/api/1", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.deviceid != null) query.append("deviceid", String(args.deviceid)) if (args.deviceids != null) query.append("deviceids", String(args.deviceids)) if (args.timestart != null) query.append("timestart", String(args.timestart)) if (args.timeend != null) query.append("timeend", String(args.timeend)) if (args.topicname != null) query.append("topicname", String(args.topicname)) if (args.topicnames != null) query.append("topicnames", String(args.topicnames)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.startafter != null) query.append("startafter", String(args.startafter)) if (apiKeyAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${apiKeyAuth.username}:${apiKeyAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/csr/data${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(cSRDataMessageListResponse, await res.json()) };