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 CreateApiApiNotificationRequestPreviewArgs = {recipientsPreviewSize?: number | undefined, body: NotificationRequest}; export type CreateApiApiNotificationRequestPreviewOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; 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 notificationRequestPreview = z.object({processedTemplates: z.record(z.string(), z.union([emailDeliveryMethodNotificationTemplate, microsoftTeamsDeliveryMethodNotificationTemplate, mobileAppDeliveryMethodNotificationTemplate, slackDeliveryMethodNotificationTemplate, smsDeliveryMethodNotificationTemplate, webDeliveryMethodNotificationTemplate])).optional(), totalRecipientsCount: z.number().int().optional(), recipientsCountByTarget: z.record(z.string(), z.number().int()).optional(), recipientsPreview: z.array(z.string()).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 }; /** * Get notification request preview (getNotificationRequestPreview) * * POST /api/notification/request/preview */ export const createApiApiNotificationRequestPreview = async (args: CreateApiApiNotificationRequestPreviewArgs, options?: CreateApiApiNotificationRequestPreviewOptions) => { 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.recipientsPreviewSize != null) query.append("recipientsPreviewSize", String(args.recipientsPreviewSize)) const url = `${baseUrl.replace(/\/+$/, '')}/api/notification/request/preview${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(notificationRequestPreview, await res.json()) };