Skip to content

Commit

Permalink
feat: notify class
Browse files Browse the repository at this point in the history
  • Loading branch information
wajeht committed Aug 4, 2024
1 parent 6c5ead8 commit 4dcae10
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/notification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { logger } from './util';
import { discordConfig, appConfig } from './config';

interface Embed {
title: string;
description: string;
}

interface DiscordMessage {
username: string;
content: string;
embeds?: Embed[];
}

interface Notifier {
discord(msg: string, object?: any): Promise<void>;
email(to: string, subject: string, body: string): Promise<void>;
}

interface NotifyParams {
discordUrl?: string;
environment?: string;
botUsername?: string;
httpClient?: (url: string, options: RequestInit) => Promise<Response>;
}

export function notify(params: NotifyParams = {}): Notifier {
const {
discordUrl = discordConfig.url,
environment = appConfig.NODE_ENV,
botUsername = 'commit.jaw.dev',
httpClient = fetch,
} = params;

return {
discord: async (msg: string, object: any = null): Promise<void> => {
try {
if (environment !== 'production') {
logger.info('Skipping discord notification non production environment!');
return;
}
let params: DiscordMessage;
if (object === null) {
params = { username: botUsername, content: msg };
} else {
params = {
username: botUsername,
content: msg,
embeds: [{ title: msg, description: JSON.stringify(object) }],
};
}
const res = await httpClient(discordUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(params),
});
if (res.status === 204) {
logger.info(`Discord bot has sent: ${msg}`);
} else {
throw new Error(`Discord API returned status ${res.status}`);
}
} catch (error) {
console.error('Error sending Discord notification:', error);
}
},
email: async (to: string, subject: string, body: string): Promise<void> => {
try {
console.log('notify.email() has not been implemented yet');
} catch (error) {
console.error('Error sending email notification:', error);
}
},
};
}

0 comments on commit 4dcae10

Please sign in to comment.