From def53a7f9606550b5cd68744b8c11ab3c8a7cba0 Mon Sep 17 00:00:00 2001 From: Mikko Korhonen Date: Wed, 24 Jul 2024 12:30:55 +0300 Subject: [PATCH] Change NotificationTemplateRenderer methods to return a promise Signed-off-by: Mikko Korhonen --- .changeset/spicy-planets-provide.md | 46 +++++++++++++++++++ .../api-report.md | 6 +-- .../src/extensions.ts | 6 +-- .../processor/NotificationsEmailProcessor.ts | 6 +-- 4 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 .changeset/spicy-planets-provide.md diff --git a/.changeset/spicy-planets-provide.md b/.changeset/spicy-planets-provide.md new file mode 100644 index 0000000000..f2929df850 --- /dev/null +++ b/.changeset/spicy-planets-provide.md @@ -0,0 +1,46 @@ +--- +'@backstage/plugin-notifications-backend-module-email': minor +--- + +**BREAKING** Following `NotificationTemplateRenderer` methods now return a Promise and **must** be awaited: `getSubject`, `getText` and `getHtml`. + +Required changes and example usage: + +```diff +import { notificationsEmailTemplateExtensionPoint } from '@backstage/plugin-notifications-backend-module-email'; +import { Notification } from '@backstage/plugin-notifications-common'; ++import { getNotificationSubject, getNotificationTextContent, getNotificationHtmlContent } from 'my-notification-processing-library` +export const notificationsModuleEmailDecorator = createBackendModule({ + pluginId: 'notifications', + moduleId: 'email.templates', + register(reg) { + reg.registerInit({ + deps: { + emailTemplates: notificationsEmailTemplateExtensionPoint, + }, + async init({ emailTemplates }) { + emailTemplates.setTemplateRenderer({ +- getSubject(notification) { ++ async getSubject(notification) { +- return `New notification from ${notification.source}`; ++ const subject = await getNotificationSubject(notification); ++ return `New notification from ${subject}`; + }, +- getText(notification) { ++ async getText(notification) { +- return notification.content; ++ const text = await getNotificationTextContent(notification); ++ return text; + }, +- getHtml(notification) { ++ async getHtml(notification) { +- return `

${notification.content}

`; ++ const html = await getNotificationHtmlContent(notification); ++ return html; + }, + }); + }, + }); + }, +}); +``` diff --git a/plugins/notifications-backend-module-email/api-report.md b/plugins/notifications-backend-module-email/api-report.md index 7e8eac6cb7..b105f15cba 100644 --- a/plugins/notifications-backend-module-email/api-report.md +++ b/plugins/notifications-backend-module-email/api-report.md @@ -23,10 +23,10 @@ export default notificationsModuleEmail; // @public (undocumented) export interface NotificationTemplateRenderer { // (undocumented) - getHtml?(notification: Notification_2): string; + getHtml?(notification: Notification_2): Promise; // (undocumented) - getSubject?(notification: Notification_2): string; + getSubject?(notification: Notification_2): Promise; // (undocumented) - getText?(notification: Notification_2): string; + getText?(notification: Notification_2): Promise; } ``` diff --git a/plugins/notifications-backend-module-email/src/extensions.ts b/plugins/notifications-backend-module-email/src/extensions.ts index 454fe9b384..49795eadcf 100644 --- a/plugins/notifications-backend-module-email/src/extensions.ts +++ b/plugins/notifications-backend-module-email/src/extensions.ts @@ -20,9 +20,9 @@ import { Notification } from '@backstage/plugin-notifications-common'; * @public */ export interface NotificationTemplateRenderer { - getSubject?(notification: Notification): string; - getText?(notification: Notification): string; - getHtml?(notification: Notification): string; + getSubject?(notification: Notification): Promise; + getText?(notification: Notification): Promise; + getHtml?(notification: Notification): Promise; } /** diff --git a/plugins/notifications-backend-module-email/src/processor/NotificationsEmailProcessor.ts b/plugins/notifications-backend-module-email/src/processor/NotificationsEmailProcessor.ts index 8689afc065..3bfccfcc90 100644 --- a/plugins/notifications-backend-module-email/src/processor/NotificationsEmailProcessor.ts +++ b/plugins/notifications-backend-module-email/src/processor/NotificationsEmailProcessor.ts @@ -280,10 +280,10 @@ export class NotificationsEmailProcessor implements NotificationProcessor { const mailOptions = { from: this.sender, subject: - this.templateRenderer?.getSubject?.(notification) ?? + (await this.templateRenderer?.getSubject?.(notification)) ?? notification.payload.title, - html: this.templateRenderer?.getHtml?.(notification), - text: this.templateRenderer?.getText?.(notification), + html: await this.templateRenderer?.getHtml?.(notification), + text: await this.templateRenderer?.getText?.(notification), replyTo: this.replyTo, };