import {z} from 'zod' export type NotificationTemplateId = {id: string, entityType: 'NOTIFICATION_TEMPLATE'}; export type TenantId = {id: string, entityType: 'TENANT'}; 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 CreateApiApiNotificationTemplateArgs = {body: NotificationTemplate}; export type CreateApiApiNotificationTemplateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const notificationTemplateId = z.object({id: z.string(), entityType: z.literal("NOTIFICATION_TEMPLATE")}); export const tenantId = z.object({id: z.string(), entityType: z.literal("TENANT")}); 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 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 }; /** * Save notification template (saveNotificationTemplate) * * POST /api/notification/template */ export const createApiApiNotificationTemplate = async (args: CreateApiApiNotificationTemplateArgs, options?: CreateApiApiNotificationTemplateOptions) => { 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/template${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(notificationTemplate, await res.json()) };