import {z} from 'zod' export type TenantId = {id: string, entityType: 'TENANT'}; export type NotificationTemplateId = {id: string, entityType: 'NOTIFICATION_TEMPLATE'}; export type EmailDeliveryMethodNotificationTemplate = {enabled?: boolean | undefined, body: string, method: string, subject: string}; export type Button = {enabled?: boolean | undefined, text?: string | undefined, linkType?: ('LINK' | 'DASHBOARD') | undefined, link?: string | undefined, dashboardId?: string | undefined, dashboardState?: string | undefined, setEntityIdInState?: boolean | undefined}; export type MicrosoftTeamsDeliveryMethodNotificationTemplate = {enabled?: boolean | undefined, body: string, method: string, subject?: string | undefined, themeColor?: string | undefined, button?: Button | undefined}; export type JsonNode = unknown; export type MobileAppDeliveryMethodNotificationTemplate = {enabled?: boolean | undefined, body: string, method: string, subject: string, additionalConfig?: JsonNode | undefined}; export type SlackDeliveryMethodNotificationTemplate = {enabled?: boolean | undefined, body: string, method: string}; export type SmsDeliveryMethodNotificationTemplate = {enabled?: boolean | undefined, body: string, method: string}; export type WebDeliveryMethodNotificationTemplate = {enabled?: boolean | undefined, body: string, method: string, subject: string, additionalConfig?: JsonNode | undefined}; export type NotificationTemplateConfig = {deliveryMethodsTemplates: {[key: string]: EmailDeliveryMethodNotificationTemplate | MicrosoftTeamsDeliveryMethodNotificationTemplate | MobileAppDeliveryMethodNotificationTemplate | SlackDeliveryMethodNotificationTemplate | SmsDeliveryMethodNotificationTemplate | WebDeliveryMethodNotificationTemplate}}; export type NotificationTemplate = {id?: NotificationTemplateId | undefined, createdTime?: number | undefined, tenantId?: TenantId | undefined, name: string, notificationType: 'GENERAL' | 'ALARM' | 'DEVICE_ACTIVITY' | 'ENTITY_ACTION' | 'ALARM_COMMENT' | 'RULE_ENGINE_COMPONENT_LIFECYCLE_EVENT' | 'ALARM_ASSIGNMENT' | 'NEW_PLATFORM_VERSION' | 'ENTITIES_LIMIT' | 'ENTITIES_LIMIT_INCREASE_REQUEST' | 'API_USAGE_LIMIT' | 'RULE_NODE' | 'RATE_LIMITS' | 'EDGE_CONNECTION' | 'EDGE_COMMUNICATION_FAILURE' | 'TASK_PROCESSING_FAILURE' | 'RESOURCES_SHORTAGE', configuration: NotificationTemplateConfig}; export type DashboardId = {id: string, entityType: 'DASHBOARD'}; 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 NotificationInfo = {dashboardId?: DashboardId | undefined, stateEntityId?: EntityId | undefined, type: string}; export type NotificationRequestConfig = {sendingDelayInSec?: number | undefined}; export type NotificationRuleId = {id: string, entityType: 'NOTIFICATION_RULE'}; export type NotificationRequestStats = {sent?: ({[key: string]: {opaque?: number | undefined, acquire?: number | undefined, release?: number | undefined, andIncrement?: number | undefined, andDecrement?: number | undefined, plain?: number | undefined}}) | undefined, errors?: {[key: string]: {[key: string]: string}} | undefined, totalErrors?: ({opaque?: number | undefined, acquire?: number | undefined, release?: number | undefined, andIncrement?: number | undefined, andDecrement?: number | undefined, plain?: number | undefined}) | undefined, error?: string | undefined}; export type NotificationRequestId = {id: string, entityType: 'NOTIFICATION_REQUEST'}; export type NotificationRequest = {tenantId?: TenantId | undefined, targets: Array, templateId?: NotificationTemplateId | undefined, template?: NotificationTemplate | undefined, info?: NotificationInfo | undefined, additionalConfig?: NotificationRequestConfig | undefined, originatorEntityId?: EntityId | undefined, ruleId?: NotificationRuleId | undefined, status?: ('PROCESSING' | 'SENT' | 'SCHEDULED') | undefined, stats?: NotificationRequestStats | undefined, id?: NotificationRequestId | undefined, createdTime?: number | undefined}; export type CreateApiApiNotificationRequestArgs = {body: NotificationRequest}; export type CreateApiApiNotificationRequestOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const tenantId = z.object({id: z.string(), entityType: z.literal("TENANT")}); export const notificationTemplateId = z.object({id: z.string(), entityType: z.literal("NOTIFICATION_TEMPLATE")}); export const emailDeliveryMethodNotificationTemplate = z.object({enabled: z.boolean().optional(), body: z.string().min(1), method: z.string(), subject: z.string().min(1)}); export const button = z.object({enabled: z.boolean().optional(), text: z.string().optional(), linkType: z.enum(["LINK", "DASHBOARD"]).optional(), link: z.string().optional(), dashboardId: z.string().optional(), dashboardState: z.string().optional(), setEntityIdInState: z.boolean().optional()}); export const microsoftTeamsDeliveryMethodNotificationTemplate = z.object({enabled: z.boolean().optional(), body: z.string().min(1), method: z.string(), subject: z.string().optional(), themeColor: z.string().optional(), button: button.optional()}); export const jsonNode = z.unknown(); export const mobileAppDeliveryMethodNotificationTemplate = z.object({enabled: z.boolean().optional(), body: z.string().min(1), method: z.string(), subject: z.string().min(1), additionalConfig: jsonNode.optional()}); export const slackDeliveryMethodNotificationTemplate = z.object({enabled: z.boolean().optional(), body: z.string().min(1), method: z.string()}); export const smsDeliveryMethodNotificationTemplate = z.object({enabled: z.boolean().optional(), body: z.string().min(1), method: z.string()}); export const webDeliveryMethodNotificationTemplate = z.object({enabled: z.boolean().optional(), body: z.string().min(1), method: z.string(), subject: z.string().min(1), additionalConfig: jsonNode.optional()}); export const notificationTemplateConfig = z.object({deliveryMethodsTemplates: z.record(z.string(), z.union([emailDeliveryMethodNotificationTemplate, microsoftTeamsDeliveryMethodNotificationTemplate, mobileAppDeliveryMethodNotificationTemplate, slackDeliveryMethodNotificationTemplate, smsDeliveryMethodNotificationTemplate, webDeliveryMethodNotificationTemplate]))}); export const notificationTemplate = z.object({id: notificationTemplateId.optional(), createdTime: z.number().int().optional(), tenantId: tenantId.optional(), name: z.string().min(1), notificationType: z.enum(["GENERAL", "ALARM", "DEVICE_ACTIVITY", "ENTITY_ACTION", "ALARM_COMMENT", "RULE_ENGINE_COMPONENT_LIFECYCLE_EVENT", "ALARM_ASSIGNMENT", "NEW_PLATFORM_VERSION", "ENTITIES_LIMIT", "ENTITIES_LIMIT_INCREASE_REQUEST", "API_USAGE_LIMIT", "RULE_NODE", "RATE_LIMITS", "EDGE_CONNECTION", "EDGE_COMMUNICATION_FAILURE", "TASK_PROCESSING_FAILURE", "RESOURCES_SHORTAGE"]), configuration: notificationTemplateConfig}); export const dashboardId = z.object({id: z.string(), entityType: z.literal("DASHBOARD")}); 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 notificationInfo = z.object({dashboardId: dashboardId.optional(), stateEntityId: entityId.optional(), type: z.string()}); export const notificationRequestConfig = z.object({sendingDelayInSec: z.number().int().lte(604800).optional()}); export const notificationRuleId = z.object({id: z.string(), entityType: z.literal("NOTIFICATION_RULE")}); export const notificationRequestStats = z.object({sent: z.record(z.string(), z.object({opaque: z.number().int().optional(), acquire: z.number().int().optional(), release: z.number().int().optional(), andIncrement: z.number().int().optional(), andDecrement: z.number().int().optional(), plain: z.number().int().optional()})).optional(), errors: z.record(z.string(), z.record(z.string(), z.string())).optional(), totalErrors: z.object({opaque: z.number().int().optional(), acquire: z.number().int().optional(), release: z.number().int().optional(), andIncrement: z.number().int().optional(), andDecrement: z.number().int().optional(), plain: z.number().int().optional()}).optional(), error: z.string().optional()}); export const notificationRequestId = z.object({id: z.string(), entityType: z.literal("NOTIFICATION_REQUEST")}); export const notificationRequest = z.object({tenantId: tenantId.optional(), targets: z.array(z.string()), templateId: notificationTemplateId.optional(), template: notificationTemplate.optional(), info: notificationInfo.optional(), additionalConfig: notificationRequestConfig.optional(), originatorEntityId: entityId.optional(), ruleId: notificationRuleId.optional(), status: z.enum(["PROCESSING", "SENT", "SCHEDULED"]).optional(), stats: notificationRequestStats.optional(), id: notificationRequestId.optional(), createdTime: z.number().int().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 notification request (createNotificationRequest) * * POST /api/notification/request */ export const createApiApiNotificationRequest = async (args: CreateApiApiNotificationRequestArgs, options?: CreateApiApiNotificationRequestOptions) => { 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/notification/request${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(notificationRequest, await res.json()) };