import {z} from 'zod' export type GetApiV1DevicesArgs = {'hostIds[]'?: Array | undefined, time?: string | undefined, pageSize?: string | undefined, nextToken?: string | undefined}; export type GetApiV1DevicesOptions = {baseUrl?: string | undefined, siteManagerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1DevicesResponse = z.object({code: z.unknown(), data: z.array(z.object({hostId: z.string().optional(), hostName: z.string().optional(), devices: z.array(z.object({id: z.string().optional(), mac: z.string().optional(), name: z.string().optional(), model: z.string().optional(), shortname: z.string().optional(), ip: z.string().optional(), productLine: z.string().nullable().optional(), status: z.string().optional(), version: z.string().optional(), firmwareStatus: z.string().optional(), updateAvailable: z.string().nullable().optional(), isConsole: z.boolean().nullable().optional(), isManaged: z.boolean().nullable().optional(), startupTime: z.string().nullable().optional(), adoptionTime: z.string().nullable().optional(), note: z.string().nullable().optional(), uidb: z.unknown()}).nullable()).optional(), updatedAt: z.string().optional()}).nullable()).optional(), httpStatusCode: z.number().int().optional(), traceId: z.string().optional(), nextToken: z.string().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 Devices * * GET /v1/devices */ export const getApiV1Devices = async (args: GetApiV1DevicesArgs, options?: GetApiV1DevicesOptions) => { const {baseUrl = "https://api.ui.com", fetch: fetchFn = fetch, siteManagerApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args['hostIds[]'] ?? []) query.append("hostIds[]", String(item)) if (args.time != null) query.append("time", String(args.time)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.nextToken != null) query.append("nextToken", String(args.nextToken)) if (siteManagerApiKey !== undefined) headers.set("X-API-Key", siteManagerApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/devices${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(getApiV1DevicesResponse, await res.json()) };