use abstract class

This commit is contained in:
Alex Holliday committed 2026-04-13 12:55:20 -07:00
1 parent 0bb3320010
commit 22f54391d0
8 files changed
+31 -67

No files matched your search

@@ -1,18 +1,11 @@
const SERVICE_NAME = "DiscordProvider"; const SERVICE_NAME = "DiscordProvider";
import type { AlertDiscordPayload, DiscordEmbedField, Notification } from "@/types/index.js"; import type { AlertDiscordPayload, DiscordEmbedField, Notification } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js"; import { NotificationProvider } from "@/service/index.js";
import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js"; import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js";
import type { NotificationMessage, NotificationSeverity } from "@/types/notificationMessage.js"; import type { NotificationMessage, NotificationSeverity } from "@/types/notificationMessage.js";
import got from "got"; import got from "got";
import { ILogger } from "@/utils/logger.js";
export class DiscordProvider implements INotificationProvider {
private logger: ILogger;
constructor(logger: ILogger) {
this.logger = logger;
}
export class DiscordProvider extends NotificationProvider {
sendTestAlert = async (notification: Partial<Notification>) => { sendTestAlert = async (notification: Partial<Notification>) => {
if (!notification.address) { if (!notification.address) {
return false; return false;
@@ -23,6 +16,7 @@ export class DiscordProvider implements INotificationProvider {
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
...this.gotRequestOptions(),
}); });
return true; return true;
} catch (error) { } catch (error) {
@@ -55,6 +49,7 @@ export class DiscordProvider implements INotificationProvider {
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
...this.gotRequestOptions(),
}); });
return true; return true;
} catch (error) { } catch (error) {
@@ -1,17 +1,16 @@
const SERVICE_NAME = "EmailProvider"; const SERVICE_NAME = "EmailProvider";
import type { Notification } from "@/types/index.js"; import type { Notification } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js"; import { NotificationProvider } from "@/service/index.js";
import { buildTestEmail } from "@/service/infrastructure/notificationProviders/utils.js"; import { buildTestEmail } from "@/service/infrastructure/notificationProviders/utils.js";
import type { NotificationMessage } from "@/types/notificationMessage.js"; import type { NotificationMessage } from "@/types/notificationMessage.js";
import type { ILogger } from "@/utils/logger.js"; import type { ILogger } from "@/utils/logger.js";
import { IEmailService } from "@/service/infrastructure/emailService.js"; import { IEmailService } from "@/service/infrastructure/emailService.js";
export class EmailProvider implements INotificationProvider { export class EmailProvider extends NotificationProvider {
private emailService: IEmailService; private emailService: IEmailService;
private logger: ILogger;
constructor(emailService: IEmailService, logger: ILogger) { constructor(emailService: IEmailService, logger: ILogger) {
super(logger);
this.emailService = emailService; this.emailService = emailService;
this.logger = logger;
} }
async sendTestAlert(notification: Partial<Notification>): Promise<boolean> { async sendTestAlert(notification: Partial<Notification>): Promise<boolean> {
@@ -1,19 +1,12 @@
const SERVICE_NAME = "MatrixProvider"; const SERVICE_NAME = "MatrixProvider";
import got from "got"; import got from "got";
import type { INotificationProvider } from "@/service/index.js"; import { NotificationProvider } from "@/service/index.js";
import type { AlertMatrixPayload, Notification } from "@/types/index.js"; import type { AlertMatrixPayload, Notification } from "@/types/index.js";
import type { NotificationMessage } from "@/types/notificationMessage.js"; import type { NotificationMessage } from "@/types/notificationMessage.js";
import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js"; import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js";
import { ILogger } from "@/utils/logger.js";
import { randomUUID } from "crypto"; import { randomUUID } from "crypto";
export class MatrixProvider implements INotificationProvider { export class MatrixProvider extends NotificationProvider {
private logger: ILogger;
constructor(logger: ILogger) {
this.logger = logger;
}
sendTestAlert = async (notification: Partial<Notification>) => { sendTestAlert = async (notification: Partial<Notification>) => {
const { homeserverUrl, accessToken, roomId } = notification; const { homeserverUrl, accessToken, roomId } = notification;
if (!homeserverUrl || !accessToken || !roomId) { if (!homeserverUrl || !accessToken || !roomId) {
@@ -55,6 +48,7 @@ export class MatrixProvider implements INotificationProvider {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`, Authorization: `Bearer ${accessToken}`,
}, },
...this.gotRequestOptions(),
}); });
return true; return true;
} catch (error) { } catch (error) {
@@ -1,19 +1,12 @@
const SERVICE_NAME = "PagerDutyProvider"; const SERVICE_NAME = "PagerDutyProvider";
import got from "got"; import got from "got";
import type { Notification } from "@/types/index.js"; import type { Notification } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js"; import { NotificationProvider } from "@/service/index.js";
import type { NotificationMessage } from "@/types/notificationMessage.js"; import type { NotificationMessage } from "@/types/notificationMessage.js";
import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js"; import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js";
import { ILogger } from "@/utils/logger.js";
import { AlertPagerDutyPayload } from "@/types/index.js"; import { AlertPagerDutyPayload } from "@/types/index.js";
export class PagerDutyProvider implements INotificationProvider { export class PagerDutyProvider extends NotificationProvider {
private logger: ILogger;
constructor(logger: ILogger) {
this.logger = logger;
}
async sendTestAlert(notification: Partial<Notification>): Promise<boolean> { async sendTestAlert(notification: Partial<Notification>): Promise<boolean> {
try { try {
await got.post("https://events.pagerduty.com/v2/enqueue", { await got.post("https://events.pagerduty.com/v2/enqueue", {
@@ -28,6 +21,7 @@ export class PagerDutyProvider implements INotificationProvider {
}, },
}, },
responseType: "json", responseType: "json",
...this.gotRequestOptions(),
}); });
return true; return true;
} catch (error) { } catch (error) {
@@ -59,6 +53,7 @@ export class PagerDutyProvider implements INotificationProvider {
const payload = this.buildPagerDutyPayload(notification, message); const payload = this.buildPagerDutyPayload(notification, message);
await got.post("https://events.pagerduty.com/v2/enqueue", { await got.post("https://events.pagerduty.com/v2/enqueue", {
json: payload, json: payload,
...this.gotRequestOptions(),
}); });
this.logger.info({ this.logger.info({
message: "[NEW] PagerDuty notification sent via sendMessage", message: "[NEW] PagerDuty notification sent via sendMessage",
@@ -1,18 +1,11 @@
const SERVICE_NAME = "SlackProvider"; const SERVICE_NAME = "SlackProvider";
import type { Notification } from "@/types/index.js"; import type { Notification } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js"; import { NotificationProvider } from "@/service/index.js";
import type { NotificationMessage } from "@/types/notificationMessage.js"; import type { NotificationMessage } from "@/types/notificationMessage.js";
import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js"; import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js";
import got, { HTTPError } from "got"; import got, { HTTPError } from "got";
import { ILogger } from "@/utils/logger.js";
export class SlackProvider implements INotificationProvider {
private logger: ILogger;
constructor(logger: ILogger) {
this.logger = logger;
}
export class SlackProvider extends NotificationProvider {
async sendTestAlert(notification: Partial<Notification>): Promise<boolean> { async sendTestAlert(notification: Partial<Notification>): Promise<boolean> {
if (!notification.address) { if (!notification.address) {
return false; return false;
@@ -24,6 +17,7 @@ export class SlackProvider implements INotificationProvider {
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
...this.gotRequestOptions(),
}); });
return true; return true;
} catch (error) { } catch (error) {
@@ -54,6 +48,7 @@ export class SlackProvider implements INotificationProvider {
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
...this.gotRequestOptions(),
}); });
this.logger.info({ this.logger.info({
message: "[NEW] Slack notification sent via sendMessage", message: "[NEW] Slack notification sent via sendMessage",
@@ -1,9 +1,8 @@
const SERVICE_NAME = "TeamsProvider"; const SERVICE_NAME = "TeamsProvider";
import type { Notification } from "@/types/index.js"; import type { Notification } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js"; import { NotificationProvider } from "@/service/index.js";
import type { NotificationMessage } from "@/types/notificationMessage.js"; import type { NotificationMessage } from "@/types/notificationMessage.js";
import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js"; import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js";
import type { ILogger } from "@/utils/logger.js";
import got, { HTTPError } from "got"; import got, { HTTPError } from "got";
// Types for Adaptive Card elements // Types for Adaptive Card elements
@@ -58,13 +57,7 @@ type TeamsMessage = {
}>; }>;
}; };
export class TeamsProvider implements INotificationProvider { export class TeamsProvider extends NotificationProvider {
private logger: ILogger;
constructor(logger: ILogger) {
this.logger = logger;
}
async sendTestAlert(notification: Partial<Notification>): Promise<boolean> { async sendTestAlert(notification: Partial<Notification>): Promise<boolean> {
if (!notification.address) { if (!notification.address) {
return false; return false;
@@ -90,6 +83,7 @@ export class TeamsProvider implements INotificationProvider {
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
...this.gotRequestOptions(),
}); });
return true; return true;
} catch (error) { } catch (error) {
@@ -117,6 +111,7 @@ export class TeamsProvider implements INotificationProvider {
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
...this.gotRequestOptions(),
}); });
this.logger.info({ this.logger.info({
message: "Teams notification sent via sendMessage", message: "Teams notification sent via sendMessage",
@@ -1,18 +1,11 @@
const SERVICE_NAME = "TelegramProvider"; const SERVICE_NAME = "TelegramProvider";
import type { Notification } from "@/types/index.js"; import type { Notification } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js"; import { NotificationProvider } from "@/service/index.js";
import type { NotificationMessage } from "@/types/notificationMessage.js"; import type { NotificationMessage } from "@/types/notificationMessage.js";
import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js"; import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js";
import got from "got"; import got from "got";
import { ILogger } from "@/utils/logger.js";
export class TelegramProvider implements INotificationProvider {
private logger: ILogger;
constructor(logger: ILogger) {
this.logger = logger;
}
export class TelegramProvider extends NotificationProvider {
async sendTestAlert(notification: Partial<Notification>): Promise<boolean> { async sendTestAlert(notification: Partial<Notification>): Promise<boolean> {
if (!notification.address || !notification.accessToken) { if (!notification.address || !notification.accessToken) {
return false; return false;
@@ -25,6 +18,7 @@ export class TelegramProvider implements INotificationProvider {
text: getTestMessage(), text: getTestMessage(),
parse_mode: "HTML", parse_mode: "HTML",
}, },
...this.gotRequestOptions(),
}); });
return true; return true;
} catch (error) { } catch (error) {
@@ -55,7 +49,9 @@ export class TelegramProvider implements INotificationProvider {
text, text,
parse_mode: "HTML", parse_mode: "HTML",
}, },
...this.gotRequestOptions(),
}); });
this.logger.info({ this.logger.info({
message: "Telegram notification sent", message: "Telegram notification sent",
service: SERVICE_NAME, service: SERVICE_NAME,
@@ -1,18 +1,11 @@
const SERVICE_NAME = "WebhookProvider"; const SERVICE_NAME = "WebhookProvider";
import type { Notification } from "@/types/index.js"; import type { Notification } from "@/types/index.js";
import { INotificationProvider } from "@/service/index.js"; import { NotificationProvider } from "@/service/index.js";
import type { NotificationMessage } from "@/types/notificationMessage.js"; import type { NotificationMessage } from "@/types/notificationMessage.js";
import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js"; import { getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js";
import got from "got"; import got from "got";
import { ILogger } from "@/utils/logger.js";
export class WebhookProvider implements INotificationProvider {
private logger: ILogger;
constructor(logger: ILogger) {
this.logger = logger;
}
export class WebhookProvider extends NotificationProvider {
sendMessage = async (notification: Notification, message: NotificationMessage): Promise<boolean> => { sendMessage = async (notification: Notification, message: NotificationMessage): Promise<boolean> => {
if (!notification.address) { if (!notification.address) {
return false; return false;
@@ -27,6 +20,7 @@ export class WebhookProvider implements INotificationProvider {
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
...this.gotRequestOptions(),
}); });
this.logger.info({ this.logger.info({
message: "Webhook notification sent", message: "Webhook notification sent",
@@ -108,6 +102,7 @@ export class WebhookProvider implements INotificationProvider {
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
...this.gotRequestOptions(),
}); });
return true; return true;
} catch (error) { } catch (error) {