import {z} from 'zod' export type GetApiDevicesPagesArgs = {orgid?: number | undefined, limit?: number | undefined, states?: string | undefined, deviceids?: string | undefined, tagid?: number | undefined, tagids?: string | undefined, linkids?: string | undefined, sims?: string | undefined, simrange?: string | undefined, devicesPerPage?: number | undefined, showHidden?: number | undefined}; export type GetApiDevicesPagesOptions = {baseUrl?: string | undefined, apiKeyAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const successProperty = z.boolean(); export const getApiDevicesPagesResponse = z.object({success: successProperty.optional(), limit: z.number().int().optional(), size: z.number().int().optional(), continues: z.boolean().optional(), data: z.array(z.object({startAfterId: z.number().int().optional()})).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 SIMs (paginated) * * GET /devices/pages */ export const getApiDevicesPages = async (args: GetApiDevicesPagesArgs, options?: GetApiDevicesPagesOptions) => { 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.orgid != null) query.append("orgid", String(args.orgid)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.states != null) query.append("states", String(args.states)) if (args.deviceids != null) query.append("deviceids", String(args.deviceids)) if (args.tagid != null) query.append("tagid", String(args.tagid)) if (args.tagids != null) query.append("tagids", String(args.tagids)) if (args.linkids != null) query.append("linkids", String(args.linkids)) if (args.sims != null) query.append("sims", String(args.sims)) if (args.simrange != null) query.append("simrange", String(args.simrange)) if (args.devicesPerPage != null) query.append("devicesPerPage", String(args.devicesPerPage)) if (args.showHidden != null) query.append("showHidden", String(args.showHidden)) if (apiKeyAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${apiKeyAuth.username}:${apiKeyAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/devices/pages${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(getApiDevicesPagesResponse, await res.json()) };