import {z} from 'zod' export type GetApiApiNotificationTemplateIdArgs = {id: string}; export type GetApiApiNotificationTemplateIdOptions = {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 }; /** * Get notification template by id (getNotificationTemplateById) * * GET /api/notification/template/{id} */ export const getApiApiNotificationTemplateId = async (args: GetApiApiNotificationTemplateIdArgs, options?: GetApiApiNotificationTemplateIdOptions) => { const {baseUrl = "https://demo.thingsboard.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/notification/template/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) 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()) };