import {z} from 'zod' export type QueueId = {id: string, entityType: 'QUEUE'}; export type TenantId = {id: string, entityType: 'TENANT'}; export type SubmitStrategy = {type?: ('BURST' | 'BATCH' | 'SEQUENTIAL_BY_ORIGINATOR' | 'SEQUENTIAL_BY_TENANT' | 'SEQUENTIAL') | undefined, batchSize?: number | undefined}; export type ProcessingStrategy = {type?: ('SKIP_ALL_FAILURES' | 'SKIP_ALL_FAILURES_AND_TIMED_OUT' | 'RETRY_ALL' | 'RETRY_FAILED' | 'RETRY_TIMED_OUT' | 'RETRY_FAILED_AND_TIMED_OUT') | undefined, retries?: number | undefined, failurePercentage?: number | undefined, pauseBetweenRetries?: number | undefined, maxPauseBetweenRetries?: number | undefined}; export type JsonNode = unknown; export type Queue = {id?: QueueId | undefined, createdTime?: number | undefined, tenantId?: TenantId | undefined, name?: string | undefined, topic?: string | undefined, pollInterval?: number | undefined, partitions?: number | undefined, consumerPerPartition?: boolean | undefined, packProcessingTimeout?: number | undefined, submitStrategy?: SubmitStrategy | undefined, processingStrategy?: ProcessingStrategy | undefined, additionalInfo?: JsonNode | undefined}; export type CreateApiApiQueuesArgs = {serviceType: 'TB-RULE-ENGINE' | 'TB-CORE' | 'TB-TRANSPORT' | 'JS-EXECUTOR', body: Queue}; export type CreateApiApiQueuesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const queueId = z.object({id: z.string(), entityType: z.literal("QUEUE")}); export const tenantId = z.object({id: z.string(), entityType: z.literal("TENANT")}); export const submitStrategy = z.object({type: z.enum(["BURST", "BATCH", "SEQUENTIAL_BY_ORIGINATOR", "SEQUENTIAL_BY_TENANT", "SEQUENTIAL"]).optional(), batchSize: z.number().int().optional()}); export const processingStrategy = z.object({type: z.enum(["SKIP_ALL_FAILURES", "SKIP_ALL_FAILURES_AND_TIMED_OUT", "RETRY_ALL", "RETRY_FAILED", "RETRY_TIMED_OUT", "RETRY_FAILED_AND_TIMED_OUT"]).optional(), retries: z.number().int().optional(), failurePercentage: z.number().optional(), pauseBetweenRetries: z.number().int().optional(), maxPauseBetweenRetries: z.number().int().optional()}); export const jsonNode = z.unknown(); export const queue = z.object({id: queueId.optional(), createdTime: z.number().int().optional(), tenantId: tenantId.optional(), name: z.string().optional(), topic: z.string().optional(), pollInterval: z.number().int().optional(), partitions: z.number().int().optional(), consumerPerPartition: z.boolean().optional(), packProcessingTimeout: z.number().int().optional(), submitStrategy: submitStrategy.optional(), processingStrategy: processingStrategy.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 Queue (saveQueue) * * POST /api/queues */ export const createApiApiQueues = async (args: CreateApiApiQueuesArgs, options?: CreateApiApiQueuesOptions) => { 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.serviceType != null) query.append("serviceType", String(args.serviceType)) const url = `${baseUrl.replace(/\/+$/, '')}/api/queues${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(queue, await res.json()) };