import {z} from 'zod' export type RuleChainId = {id: string, entityType: 'RULE_CHAIN'}; export type RuleNodeId = {id: string, entityType: 'RULE_NODE'}; export type DebugSettings = {failuresEnabled?: boolean | undefined, allEnabled?: boolean | undefined, allEnabledUntil?: number | undefined}; export type JsonNode = unknown; export type RuleNode = {id?: RuleNodeId | undefined, createdTime?: number | undefined, ruleChainId?: RuleChainId | undefined, type?: string | undefined, name?: string | undefined, debugMode?: boolean | undefined, debugSettings?: DebugSettings | undefined, singletonMode?: boolean | undefined, queueName?: string | undefined, configurationVersion?: number | undefined, configuration?: JsonNode | undefined, externalId?: RuleNodeId | undefined, additionalInfo?: JsonNode | undefined}; export type NodeConnectionInfo = {fromIndex: number, toIndex: number, type: string}; export type RuleChainConnectionInfo = {fromIndex: number, targetRuleChainId: RuleChainId, additionalInfo: JsonNode, type: string}; export type RuleChainMetaData = {ruleChainId: RuleChainId, version?: number | undefined, firstNodeIndex: number, nodes: Array, connections: Array, ruleChainConnections: Array}; export type CreateApiApiRuleChainMetadataArgs = {updateRelated?: boolean | undefined, body: RuleChainMetaData}; export type CreateApiApiRuleChainMetadataOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const ruleChainId = z.object({id: z.string(), entityType: z.literal("RULE_CHAIN")}); export const ruleNodeId = z.object({id: z.string(), entityType: z.literal("RULE_NODE")}); export const debugSettings = z.object({failuresEnabled: z.boolean().optional(), allEnabled: z.boolean().optional(), allEnabledUntil: z.number().int().optional()}); export const jsonNode = z.unknown(); export const ruleNode = z.object({id: ruleNodeId.optional(), createdTime: z.number().int().optional(), ruleChainId: ruleChainId.optional(), type: z.string().optional(), name: z.string().optional(), debugMode: z.boolean().optional(), debugSettings: debugSettings.optional(), singletonMode: z.boolean().optional(), queueName: z.string().optional(), configurationVersion: z.number().int().optional(), configuration: jsonNode.optional(), externalId: ruleNodeId.optional(), additionalInfo: jsonNode.optional()}); export const nodeConnectionInfo = z.object({fromIndex: z.number().int(), toIndex: z.number().int(), type: z.string()}); export const ruleChainConnectionInfo = z.object({fromIndex: z.number().int(), targetRuleChainId: ruleChainId, additionalInfo: jsonNode, type: z.string()}); export const ruleChainMetaData = z.object({ruleChainId: ruleChainId, version: z.number().int().optional(), firstNodeIndex: z.number().int(), nodes: z.array(ruleNode), connections: z.array(nodeConnectionInfo), ruleChainConnections: z.array(ruleChainConnectionInfo)}); 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 }; /** * Update Rule Chain Metadata * * POST /api/ruleChain/metadata */ export const createApiApiRuleChainMetadata = async (args: CreateApiApiRuleChainMetadataArgs, options?: CreateApiApiRuleChainMetadataOptions) => { 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.updateRelated != null) query.append("updateRelated", String(args.updateRelated)) const url = `${baseUrl.replace(/\/+$/, '')}/api/ruleChain/metadata${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(ruleChainMetaData, await res.json()) };