import {z} from 'zod' export type GetApiApiOtaPackageOtaPackageIdArgs = {otaPackageId: string}; export type GetApiApiOtaPackageOtaPackageIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const otaPackageId = z.object({id: z.string(), entityType: z.literal("OTA_PACKAGE")}); export const tenantId = z.object({id: z.string(), entityType: z.literal("TENANT")}); export const deviceProfileId = z.object({id: z.string(), entityType: z.literal("DEVICE_PROFILE")}); export const jsonNode = z.unknown(); export const otaPackage = z.object({id: otaPackageId.optional(), createdTime: z.number().int().optional(), tenantId: tenantId.optional(), deviceProfileId: deviceProfileId.optional(), type: z.enum(["FIRMWARE", "SOFTWARE"]).optional(), title: z.string().optional(), version: z.string().optional(), tag: z.string().optional(), url: z.string().optional(), hasData: z.boolean().optional(), fileName: z.string().optional(), contentType: z.string().optional(), checksumAlgorithm: z.enum(["MD5", "SHA256", "SHA384", "SHA512", "CRC32", "MURMUR3_32", "MURMUR3_128"]).optional(), checksum: z.string().optional(), dataSize: z.number().int().optional(), data: z.object({short: z.number().int().optional(), char: z.string().optional(), int: z.number().int().optional(), long: z.number().int().optional(), float: z.number().optional(), double: z.number().optional(), direct: z.boolean().optional(), readOnly: z.boolean().optional()}).optional(), name: z.string().optional(), additionalInfo: jsonNode.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 OTA Package (getOtaPackageById) * * GET /api/otaPackage/{otaPackageId} */ export const getApiApiOtaPackageOtaPackageId = async (args: GetApiApiOtaPackageOtaPackageIdArgs, options?: GetApiApiOtaPackageOtaPackageIdOptions) => { const {baseUrl = "https://demo.thingsboard.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/otaPackage/${encodeURIComponent(String(args.otaPackageId))}${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(otaPackage, await res.json()) };