import {z} from 'zod' export type GetApiApiTenantDevicesArgs = {pageSize: number, page: number, type?: string | undefined, textSearch?: string | undefined, sortProperty?: ('createdTime' | 'name' | 'deviceProfileName' | 'label' | 'customerTitle') | undefined, sortOrder?: ('ASC' | 'DESC') | undefined, deviceName: string}; export type GetApiApiTenantDevicesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const deviceId = z.object({id: z.string(), entityType: z.literal("DEVICE")}); export const tenantId = z.object({id: z.string(), entityType: z.literal("TENANT")}); export const customerId = z.object({id: z.string(), entityType: z.literal("CUSTOMER")}); export const deviceProfileId = z.object({id: z.string(), entityType: z.literal("DEVICE_PROFILE")}); export const otaPackageId = z.object({id: z.string(), entityType: z.literal("OTA_PACKAGE")}); export const jsonNode = z.unknown(); export const defaultDeviceConfiguration = z.object({type: z.string()}); export const coapDeviceTransportConfiguration = z.object({type: z.string(), powerMode: z.enum(["PSM", "DRX", "E_DRX"]).optional(), psmActivityTimer: z.number().int().optional(), edrxCycle: z.number().int().optional(), pagingTransmissionWindow: z.number().int().optional()}); export const defaultDeviceTransportConfiguration = z.object({type: z.string()}); export const lwm2mDeviceTransportConfiguration = z.object({type: z.string(), powerMode: z.enum(["PSM", "DRX", "E_DRX"]).optional(), psmActivityTimer: z.number().int().optional(), edrxCycle: z.number().int().optional(), pagingTransmissionWindow: z.number().int().optional()}); export const mqttDeviceTransportConfiguration = z.object({type: z.string()}); export const snmpDeviceTransportConfiguration = z.object({type: z.string(), host: z.string().optional(), port: z.number().int().optional(), protocolVersion: z.enum(["V1", "V2C", "V3"]).optional(), community: z.string().optional(), username: z.string().optional(), securityName: z.string().optional(), contextName: z.string().optional(), authenticationProtocol: z.enum(["SHA_1", "SHA_224", "SHA_256", "SHA_384", "SHA_512", "MD5"]).optional(), authenticationPassphrase: z.string().optional(), privacyProtocol: z.enum(["DES", "AES_128", "AES_192", "AES_256"]).optional(), privacyPassphrase: z.string().optional(), engineId: z.string().optional()}); export const deviceData = z.object({configuration: defaultDeviceConfiguration.optional(), transportConfiguration: z.union([coapDeviceTransportConfiguration, defaultDeviceTransportConfiguration, lwm2mDeviceTransportConfiguration, mqttDeviceTransportConfiguration, snmpDeviceTransportConfiguration]).optional()}); export const device = z.object({id: deviceId.optional(), createdTime: z.number().int().optional(), tenantId: tenantId.optional(), customerId: customerId.optional(), name: z.string(), type: z.string().optional(), label: z.string().optional(), deviceProfileId: deviceProfileId, firmwareId: otaPackageId.optional(), softwareId: otaPackageId.optional(), version: z.number().int().optional(), additionalInfo: jsonNode.optional(), deviceData: deviceData.optional()}); export const pageDataDevice = z.object({data: z.array(device).optional(), totalPages: z.number().int().optional(), totalElements: z.number().int().optional(), hasNext: z.boolean().optional()}); export const getApiApiTenantDevicesResponse = z.union([device, pageDataDevice]); 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 Tenant Device (getTenantDevice) * * GET /api/tenant/devices */ export const getApiApiTenantDevices = async (args: GetApiApiTenantDevicesArgs, options?: GetApiApiTenantDevicesOptions) => { const {baseUrl = "https://demo.thingsboard.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.page != null) query.append("page", String(args.page)) if (args.type != null) query.append("type", String(args.type)) if (args.textSearch != null) query.append("textSearch", String(args.textSearch)) if (args.sortProperty != null) query.append("sortProperty", String(args.sortProperty)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (args.deviceName != null) query.append("deviceName", String(args.deviceName)) const url = `${baseUrl.replace(/\/+$/, '')}/api/tenant/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(getApiApiTenantDevicesResponse, await res.json()) };