import {z} from 'zod' export type GetApiObjectInventoryArgs = {eq_timestamp?: number | undefined, count?: number | undefined, offset?: number | undefined}; export type GetApiObjectInventoryOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const inventoryModel = z.object({label: z.string(), value: z.number().int()}); export const inventorySuperModel = z.object({label: z.string(), data: z.array(inventoryModel)}); export const inventorySampleClass = z.object({total: z.number().int().optional(), os_version: inventorySuperModel.optional(), os_vendor: inventorySuperModel.optional(), asic: inventorySuperModel.optional(), asic_vendor: inventorySuperModel.optional(), asic_model: inventorySuperModel.optional(), cl_license: inventorySuperModel.optional(), agent_version: inventorySuperModel.optional(), agent_state: inventorySuperModel.optional(), platform: inventorySuperModel.optional(), platform_vendor: inventorySuperModel.optional(), disk_size: inventorySuperModel.optional(), memory_size: inventorySuperModel.optional(), platform_model: inventorySuperModel.optional(), interface_speeds: inventorySuperModel.optional()}); export const inventoryOutput = z.object({data: inventorySampleClass.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 }; /** * Get component inventory information from all network devices * * GET /object/inventory */ export const getApiObjectInventory = async (args: GetApiObjectInventoryArgs, options?: GetApiObjectInventoryOptions) => { const {baseUrl = "https://35.160.167.233:32708/netq/telemetry/v1", fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.eq_timestamp != null) query.append("eq_timestamp", String(args.eq_timestamp)) if (args.count != null) query.append("count", String(args.count)) if (args.offset != null) query.append("offset", String(args.offset)) if (jwt !== undefined) headers.set("Authorization", jwt) const url = `${baseUrl.replace(/\/+$/, '')}/object/inventory${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(inventoryOutput, await res.json()) };