import {z} from 'zod' export type GetApiApiNotificationRequestIdArgs = {id: string}; export type GetApiApiNotificationRequestIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const notificationRequestId = z.object({id: z.string(), entityType: z.literal("NOTIFICATION_REQUEST")}); 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 notificationRequestInfo = z.object({id: notificationRequestId.optional(), createdTime: z.number().int().optional(), 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(), templateName: z.string().optional(), deliveryMethods: z.array(z.enum(["WEB", "EMAIL", "SMS", "SLACK", "MICROSOFT_TEAMS", "MOBILE_APP"])).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 by id (getNotificationRequestById) * * GET /api/notification/request/{id} */ export const getApiApiNotificationRequestId = async (args: GetApiApiNotificationRequestIdArgs, options?: GetApiApiNotificationRequestIdOptions) => { 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/request/${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(notificationRequestInfo, await res.json()) };