import {z} from 'zod' export type RuleChainId = {id: string, entityType: 'RULE_CHAIN'}; export type TenantId = {id: string, entityType: 'TENANT'}; export type RuleNodeId = {id: string, entityType: 'RULE_NODE'}; export type JsonNode = unknown; export type RuleChain = {id?: RuleChainId | undefined, createdTime?: number | undefined, tenantId: TenantId, name: string, type?: ('CORE' | 'EDGE') | undefined, firstRuleNodeId?: RuleNodeId | undefined, root?: boolean | undefined, debugMode?: boolean | undefined, version?: number | undefined, configuration?: JsonNode | undefined, additionalInfo?: JsonNode | undefined}; export type DebugSettings = {failuresEnabled?: boolean | undefined, allEnabled?: boolean | undefined, allEnabledUntil?: number | undefined}; 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 RuleChainData = {ruleChains: Array, metadata: Array}; export type CreateApiApiRuleChainsImportArgs = {overwrite?: boolean | undefined, body: RuleChainData}; export type CreateApiApiRuleChainsImportOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const ruleChainId = z.object({id: z.string(), entityType: z.literal("RULE_CHAIN")}); export const ruleChainImportResult = z.object({ruleChainId: ruleChainId.optional(), ruleChainName: z.string().optional(), updated: z.boolean().optional(), error: z.string().optional()}); export const createApiApiRuleChainsImportResponse = z.array(ruleChainImportResult); 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 }; /** * Import Rule Chains * * POST /api/ruleChains/import */ export const createApiApiRuleChainsImport = async (args: CreateApiApiRuleChainsImportArgs, options?: CreateApiApiRuleChainsImportOptions) => { 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.overwrite != null) query.append("overwrite", String(args.overwrite)) const url = `${baseUrl.replace(/\/+$/, '')}/api/ruleChains/import${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(createApiApiRuleChainsImportResponse, await res.json()) };