import {z} from 'zod' export type OtaPackageId = {id: string, entityType: 'OTA_PACKAGE'}; export type TenantId = {id: string, entityType: 'TENANT'}; export type DeviceProfileId = {id: string, entityType: 'DEVICE_PROFILE'}; export type JsonNode = unknown; export type SaveOtaPackageInfoRequest = {id?: OtaPackageId | undefined, createdTime?: number | undefined, tenantId?: TenantId | undefined, deviceProfileId?: DeviceProfileId | undefined, type?: ('FIRMWARE' | 'SOFTWARE') | undefined, title?: string | undefined, version?: string | undefined, tag?: string | undefined, url?: string | undefined, hasData?: boolean | undefined, fileName?: string | undefined, contentType?: string | undefined, checksumAlgorithm?: ('MD5' | 'SHA256' | 'SHA384' | 'SHA512' | 'CRC32' | 'MURMUR3_32' | 'MURMUR3_128') | undefined, checksum?: string | undefined, dataSize?: number | undefined, usesUrl?: boolean | undefined, name?: string | undefined, additionalInfo?: JsonNode | undefined}; export type CreateApiApiOtaPackageArgs = {body: SaveOtaPackageInfoRequest}; export type CreateApiApiOtaPackageOptions = {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 otaPackageInfo = 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(), 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 }; /** * Create Or Update OTA Package Info (saveOtaPackageInfo) * * POST /api/otaPackage */ export const createApiApiOtaPackage = async (args: CreateApiApiOtaPackageArgs, options?: CreateApiApiOtaPackageOptions) => { const {baseUrl = "https://demo.thingsboard.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/otaPackage${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(otaPackageInfo, await res.json()) };