import {z} from 'zod' export type RelationsSearchParameters = {rootId?: string | undefined, rootType?: ('TENANT' | 'CUSTOMER' | 'USER' | 'DASHBOARD' | 'ASSET' | 'DEVICE' | 'ALARM' | 'RULE_CHAIN' | 'RULE_NODE' | 'ENTITY_VIEW' | 'WIDGETS_BUNDLE' | 'WIDGET_TYPE' | 'TENANT_PROFILE' | 'DEVICE_PROFILE' | 'ASSET_PROFILE' | 'API_USAGE_STATE' | 'TB_RESOURCE' | 'OTA_PACKAGE' | 'EDGE' | 'RPC' | 'QUEUE' | 'NOTIFICATION_TARGET' | 'NOTIFICATION_TEMPLATE' | 'NOTIFICATION_REQUEST' | 'NOTIFICATION' | 'NOTIFICATION_RULE' | 'QUEUE_STATS' | 'OAUTH2_CLIENT' | 'DOMAIN' | 'MOBILE_APP' | 'MOBILE_APP_BUNDLE' | 'CALCULATED_FIELD' | 'JOB' | 'ADMIN_SETTINGS' | 'AI_MODEL' | 'API_KEY') | undefined, direction?: ('FROM' | 'TO') | undefined, relationTypeGroup?: ('COMMON' | 'DASHBOARD' | 'RULE_CHAIN' | 'RULE_NODE' | 'EDGE' | 'EDGE_AUTO_ASSIGN_RULE_CHAIN') | undefined, maxLevel?: number | undefined, fetchLastLevelOnly?: boolean | undefined}; export type AssetSearchQuery = {parameters?: RelationsSearchParameters | undefined, relationType?: string | undefined, assetTypes?: Array | undefined}; export type CreateApiApiAssetsArgs = {body: AssetSearchQuery}; export type CreateApiApiAssetsOptions = {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 createApiApiAssetsResponse = z.array(asset); 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 }; /** * Find related assets (findByQuery) * * POST /api/assets */ export const createApiApiAssets = async (args: CreateApiApiAssetsArgs, options?: CreateApiApiAssetsOptions) => { 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/assets${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(createApiApiAssetsResponse, await res.json()) };