import {z} from 'zod' export type AssetId = {id: string, entityType: 'ASSET'}; export type TenantId = {id: string, entityType: 'TENANT'}; export type CustomerId = {id: string, entityType: 'CUSTOMER'}; export type AssetProfileId = {id: string, entityType: 'ASSET_PROFILE'}; export type JsonNode = unknown; export type Asset = {id?: AssetId | undefined, createdTime?: number | undefined, tenantId?: TenantId | undefined, customerId?: CustomerId | undefined, name: string, type?: string | undefined, label?: string | undefined, assetProfileId?: AssetProfileId | undefined, version?: number | undefined, additionalInfo?: JsonNode | undefined}; export type CreateApiApiAssetArgs = {nameConflictPolicy?: ('FAIL' | 'UNIQUIFY') | undefined, uniquifySeparator?: string | undefined, uniquifyStrategy?: ('RANDOM' | 'INCREMENTAL') | undefined, body: Asset}; export type CreateApiApiAssetOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const assetId = z.object({id: z.string(), entityType: z.literal("ASSET")}); 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 assetProfileId = z.object({id: z.string(), entityType: z.literal("ASSET_PROFILE")}); export const jsonNode = z.unknown(); export const asset = z.object({id: assetId.optional(), createdTime: z.number().int().optional(), tenantId: tenantId.optional(), customerId: customerId.optional(), name: z.string(), type: z.string().optional(), label: z.string().optional(), assetProfileId: assetProfileId.optional(), version: z.number().int().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 Asset (saveAsset) * * POST /api/asset */ export const createApiApiAsset = async (args: CreateApiApiAssetArgs, options?: CreateApiApiAssetOptions) => { 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") if (args.nameConflictPolicy != null) query.append("nameConflictPolicy", String(args.nameConflictPolicy)) if (args.uniquifySeparator != null) query.append("uniquifySeparator", String(args.uniquifySeparator)) if (args.uniquifyStrategy != null) query.append("uniquifyStrategy", String(args.uniquifyStrategy)) const url = `${baseUrl.replace(/\/+$/, '')}/api/asset${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(asset, await res.json()) };