import {z} from 'zod' export type TenantId = {id: string, entityType: 'TENANT'}; export type CustomerId = {id: string, entityType: 'CUSTOMER'}; export type EntityId = {id: string, entityType: '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'}; export type UserId = {id: string, entityType: 'USER'}; export type JsonNode = unknown; export type AlarmId = {id: string, entityType: 'ALARM'}; export type Alarm = {tenantId?: TenantId | undefined, customerId?: CustomerId | undefined, type: string, originator: EntityId, severity: 'CRITICAL' | 'MAJOR' | 'MINOR' | 'WARNING' | 'INDETERMINATE', acknowledged: boolean, cleared: boolean, assigneeId?: UserId | undefined, startTs?: number | undefined, endTs?: number | undefined, ackTs?: number | undefined, clearTs?: number | undefined, assignTs?: number | undefined, details?: JsonNode | undefined, propagate?: boolean | undefined, propagateToOwner?: boolean | undefined, propagateToTenant?: boolean | undefined, propagateRelationTypes?: Array | undefined, id?: AlarmId | undefined, createdTime?: number | undefined, name: string, status: 'ACTIVE_UNACK' | 'ACTIVE_ACK' | 'CLEARED_UNACK' | 'CLEARED_ACK'}; export type CreateApiApiAlarmArgs = {body: Alarm}; export type CreateApiApiAlarmOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; 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 entityId = z.object({id: z.string(), entityType: z.enum(["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"])}); export const userId = z.object({id: z.string(), entityType: z.literal("USER")}); export const jsonNode = z.unknown(); export const alarmId = z.object({id: z.string(), entityType: z.literal("ALARM")}); export const alarm = z.object({tenantId: tenantId.optional(), customerId: customerId.optional(), type: z.string(), originator: entityId, severity: z.enum(["CRITICAL", "MAJOR", "MINOR", "WARNING", "INDETERMINATE"]), acknowledged: z.boolean(), cleared: z.boolean(), assigneeId: userId.optional(), startTs: z.number().int().optional(), endTs: z.number().int().optional(), ackTs: z.number().int().optional(), clearTs: z.number().int().optional(), assignTs: z.number().int().optional(), details: jsonNode.optional(), propagate: z.boolean().optional(), propagateToOwner: z.boolean().optional(), propagateToTenant: z.boolean().optional(), propagateRelationTypes: z.array(z.string()).optional(), id: alarmId.optional(), createdTime: z.number().int().optional(), name: z.string(), status: z.enum(["ACTIVE_UNACK", "ACTIVE_ACK", "CLEARED_UNACK", "CLEARED_ACK"])}); 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 Alarm (saveAlarm) * * POST /api/alarm */ export const createApiApiAlarm = async (args: CreateApiApiAlarmArgs, options?: CreateApiApiAlarmOptions) => { 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/alarm${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(alarm, await res.json()) };