Change NotificationTemplateRenderer methods to return a promise

Signed-off-by: Mikko Korhonen <mikko.korhonen@op.fi>
This commit is contained in:
Mikko Korhonen
2024-07-24 12:30:55 +03:00
parent 8711fa8004
commit def53a7f96
4 changed files with 55 additions and 9 deletions
+46
View File
@@ -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 `<p>${notification.content}</p>`;
+ const html = await getNotificationHtmlContent(notification);
+ return html;
},
});
},
});
},
});
```
@@ -23,10 +23,10 @@ export default notificationsModuleEmail;
// @public (undocumented)
export interface NotificationTemplateRenderer {
// (undocumented)
getHtml?(notification: Notification_2): string;
getHtml?(notification: Notification_2): Promise<string>;
// (undocumented)
getSubject?(notification: Notification_2): string;
getSubject?(notification: Notification_2): Promise<string>;
// (undocumented)
getText?(notification: Notification_2): string;
getText?(notification: Notification_2): Promise<string>;
}
```
@@ -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<string>;
getText?(notification: Notification): Promise<string>;
getHtml?(notification: Notification): Promise<string>;
}
/**
@@ -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,
};