summary refs log tree commit diff
diff options
context:
space:
mode:
authorPuyodead1 <puyodead@protonmail.com>2023-02-24 07:10:56 -0500
committerPuyodead1 <puyodead@protonmail.com>2023-02-24 07:10:56 -0500
commitd3b1fd202622ea10bd5cb89f589786e279f10f5e (patch)
tree3d698d6b7392a061ff04d0dad145200377ed7fad
parentsimplify replacer function (diff)
downloadserver-d3b1fd202622ea10bd5cb89f589786e279f10f5e.tar.xz
move transporters to their own files
-rw-r--r--src/util/util/email/index.ts (renamed from src/util/util/Email.ts)154
-rw-r--r--src/util/util/email/transports/MailGun.ts36
-rw-r--r--src/util/util/email/transports/MailJet.ts36
-rw-r--r--src/util/util/email/transports/SMTP.ts38
-rw-r--r--src/util/util/email/transports/SendGrid.ts35
-rw-r--r--src/util/util/email/transports/index.ts1
-rw-r--r--src/util/util/index.ts12
7 files changed, 165 insertions, 147 deletions
diff --git a/src/util/util/Email.ts b/src/util/util/email/index.ts
index 45919f9e..8c7a848c 100644
--- a/src/util/util/Email.ts
+++ b/src/util/util/email/index.ts
@@ -18,10 +18,14 @@
 
 import fs from "node:fs";
 import path from "node:path";
-import nodemailer, { SentMessageInfo, Transporter } from "nodemailer";
-import { User } from "../entities";
-import { Config } from "./Config";
-import { generateToken } from "./Token";
+import { SentMessageInfo, Transporter } from "nodemailer";
+import { User } from "../../entities";
+import { Config } from "../Config";
+import { generateToken } from "../Token";
+import MailGun from "./transports/MailGun";
+import MailJet from "./transports/MailJet";
+import SendGrid from "./transports/SendGrid";
+import SMTP from "./transports/SMTP";
 
 const ASSET_FOLDER_PATH = path.join(__dirname, "..", "..", "..", "assets");
 export const EMAIL_REGEX =
@@ -53,144 +57,12 @@ export function adjustEmail(email?: string): string | undefined {
 }
 
 const transporters: {
-	[key: string]: () => Promise<nodemailer.Transporter<unknown> | void>;
+	[key: string]: () => Promise<Transporter<unknown> | void>;
 } = {
-	smtp: async function () {
-		// get configuration
-		const { host, port, secure, username, password } =
-			Config.get().email.smtp;
-
-		// ensure all required configuration values are set
-		if (!host || !port || secure === null || !username || !password)
-			return console.error(
-				"[Email] SMTP has not been configured correctly.",
-			);
-
-		if (!Config.get().general.correspondenceEmail)
-			return console.error(
-				"[Email] Correspondence email has not been configured! This is used as the sender email address.",
-			);
-
-		// construct the transporter
-		const transporter = nodemailer.createTransport({
-			host,
-			port,
-			secure,
-			auth: {
-				user: username,
-				pass: password,
-			},
-		});
-
-		// verify connection configuration
-		const verified = await transporter.verify().catch((err) => {
-			console.error("[Email] SMTP verification failed:", err);
-			return;
-		});
-
-		// if verification failed, return void and don't set transporter
-		if (!verified) return;
-
-		return transporter;
-	},
-	mailgun: async function () {
-		// get configuration
-		const { apiKey, domain } = Config.get().email.mailgun;
-
-		// ensure all required configuration values are set
-		if (!apiKey || !domain)
-			return console.error(
-				"[Email] Mailgun has not been configured correctly.",
-			);
-
-		let mg;
-		try {
-			// try to import the transporter package
-			mg = require("nodemailer-mailgun-transport");
-		} catch {
-			// if the package is not installed, log an error and return void so we don't set the transporter
-			console.error(
-				"[Email] Mailgun transport is not installed. Please run `npm install nodemailer-mailgun-transport --save-optional` to install it.",
-			);
-			return;
-		}
-
-		// create the transporter configuration object
-		const auth = {
-			auth: {
-				api_key: apiKey,
-				domain: domain,
-			},
-		};
-
-		// create the transporter and return it
-		return nodemailer.createTransport(mg(auth));
-	},
-	mailjet: async function () {
-		// get configuration
-		const { apiKey, apiSecret } = Config.get().email.mailjet;
-
-		// ensure all required configuration values are set
-		if (!apiKey || !apiSecret)
-			return console.error(
-				"[Email] Mailjet has not been configured correctly.",
-			);
-
-		let mj;
-		try {
-			// try to import the transporter package
-			mj = require("nodemailer-mailjet-transport");
-		} catch {
-			// if the package is not installed, log an error and return void so we don't set the transporter
-			console.error(
-				"[Email] Mailjet transport is not installed. Please run `npm install n0script22/nodemailer-mailjet-transport --save-optional` to install it.",
-			);
-			return;
-		}
-
-		// create the transporter configuration object
-		const auth = {
-			auth: {
-				apiKey: apiKey,
-				apiSecret: apiSecret,
-			},
-		};
-
-		// create the transporter and return it
-		return nodemailer.createTransport(mj(auth));
-	},
-	sendgrid: async function () {
-		// get configuration
-		const { apiKey } = Config.get().email.sendgrid;
-
-		// ensure all required configuration values are set
-		if (!apiKey)
-			return console.error(
-				"[Email] SendGrid has not been configured correctly.",
-			);
-
-		let sg;
-		try {
-			// try to import the transporter package
-			sg = require("nodemailer-sendgrid-transport");
-		} catch {
-			// if the package is not installed, log an error and return void so we don't set the transporter
-			console.error(
-				"[Email] SendGrid transport is not installed. Please run `npm install Maria-Golomb/nodemailer-sendgrid-transport --save-optional` to install it.",
-			);
-			return;
-		}
-
-		// create the transporter configuration object
-		const auth = {
-			auth: {
-				api_key: apiKey,
-			},
-		};
-
-		// create the transporter and return it
-		return nodemailer.createTransport(sg(auth));
-	},
+	smtp: SMTP,
+	mailgun: MailGun,
+	mailjet: MailJet,
+	sendgrid: SendGrid,
 };
 
 export const Email: {
diff --git a/src/util/util/email/transports/MailGun.ts b/src/util/util/email/transports/MailGun.ts
new file mode 100644
index 00000000..3a5be13c
--- /dev/null
+++ b/src/util/util/email/transports/MailGun.ts
@@ -0,0 +1,36 @@
+import { Config } from "@fosscord/util";
+import nodemailer from "nodemailer";
+
+export default async function () {
+	// get configuration
+	const { apiKey, domain } = Config.get().email.mailgun;
+
+	// ensure all required configuration values are set
+	if (!apiKey || !domain)
+		return console.error(
+			"[Email] Mailgun has not been configured correctly.",
+		);
+
+	let mg;
+	try {
+		// try to import the transporter package
+		mg = require("nodemailer-mailgun-transport");
+	} catch {
+		// if the package is not installed, log an error and return void so we don't set the transporter
+		console.error(
+			"[Email] Mailgun transport is not installed. Please run `npm install nodemailer-mailgun-transport --save-optional` to install it.",
+		);
+		return;
+	}
+
+	// create the transporter configuration object
+	const auth = {
+		auth: {
+			api_key: apiKey,
+			domain: domain,
+		},
+	};
+
+	// create the transporter and return it
+	return nodemailer.createTransport(mg(auth));
+}
diff --git a/src/util/util/email/transports/MailJet.ts b/src/util/util/email/transports/MailJet.ts
new file mode 100644
index 00000000..561d13ea
--- /dev/null
+++ b/src/util/util/email/transports/MailJet.ts
@@ -0,0 +1,36 @@
+import { Config } from "@fosscord/util";
+import nodemailer from "nodemailer";
+
+export default async function () {
+	// get configuration
+	const { apiKey, apiSecret } = Config.get().email.mailjet;
+
+	// ensure all required configuration values are set
+	if (!apiKey || !apiSecret)
+		return console.error(
+			"[Email] Mailjet has not been configured correctly.",
+		);
+
+	let mj;
+	try {
+		// try to import the transporter package
+		mj = require("nodemailer-mailjet-transport");
+	} catch {
+		// if the package is not installed, log an error and return void so we don't set the transporter
+		console.error(
+			"[Email] Mailjet transport is not installed. Please run `npm install n0script22/nodemailer-mailjet-transport --save-optional` to install it.",
+		);
+		return;
+	}
+
+	// create the transporter configuration object
+	const auth = {
+		auth: {
+			apiKey: apiKey,
+			apiSecret: apiSecret,
+		},
+	};
+
+	// create the transporter and return it
+	return nodemailer.createTransport(mj(auth));
+}
diff --git a/src/util/util/email/transports/SMTP.ts b/src/util/util/email/transports/SMTP.ts
new file mode 100644
index 00000000..7d8e1e20
--- /dev/null
+++ b/src/util/util/email/transports/SMTP.ts
@@ -0,0 +1,38 @@
+import { Config } from "@fosscord/util";
+import nodemailer from "nodemailer";
+
+export default async function () {
+	// get configuration
+	const { host, port, secure, username, password } = Config.get().email.smtp;
+
+	// ensure all required configuration values are set
+	if (!host || !port || secure === null || !username || !password)
+		return console.error("[Email] SMTP has not been configured correctly.");
+
+	if (!Config.get().general.correspondenceEmail)
+		return console.error(
+			"[Email] Correspondence email has not been configured! This is used as the sender email address.",
+		);
+
+	// construct the transporter
+	const transporter = nodemailer.createTransport({
+		host,
+		port,
+		secure,
+		auth: {
+			user: username,
+			pass: password,
+		},
+	});
+
+	// verify connection configuration
+	const verified = await transporter.verify().catch((err) => {
+		console.error("[Email] SMTP verification failed:", err);
+		return;
+	});
+
+	// if verification failed, return void and don't set transporter
+	if (!verified) return;
+
+	return transporter;
+}
diff --git a/src/util/util/email/transports/SendGrid.ts b/src/util/util/email/transports/SendGrid.ts
new file mode 100644
index 00000000..7b46c7be
--- /dev/null
+++ b/src/util/util/email/transports/SendGrid.ts
@@ -0,0 +1,35 @@
+import { Config } from "@fosscord/util";
+import nodemailer from "nodemailer";
+
+export default async function () {
+	// get configuration
+	const { apiKey } = Config.get().email.sendgrid;
+
+	// ensure all required configuration values are set
+	if (!apiKey)
+		return console.error(
+			"[Email] SendGrid has not been configured correctly.",
+		);
+
+	let sg;
+	try {
+		// try to import the transporter package
+		sg = require("nodemailer-sendgrid-transport");
+	} catch {
+		// if the package is not installed, log an error and return void so we don't set the transporter
+		console.error(
+			"[Email] SendGrid transport is not installed. Please run `npm install Maria-Golomb/nodemailer-sendgrid-transport --save-optional` to install it.",
+		);
+		return;
+	}
+
+	// create the transporter configuration object
+	const auth = {
+		auth: {
+			api_key: apiKey,
+		},
+	};
+
+	// create the transporter and return it
+	return nodemailer.createTransport(sg(auth));
+}
diff --git a/src/util/util/email/transports/index.ts b/src/util/util/email/transports/index.ts
new file mode 100644
index 00000000..d14acbf0
--- /dev/null
+++ b/src/util/util/email/transports/index.ts
@@ -0,0 +1 @@
+export * from "./SMTP";
diff --git a/src/util/util/index.ts b/src/util/util/index.ts
index 6eb686d0..93656ecb 100644
--- a/src/util/util/index.ts
+++ b/src/util/util/index.ts
@@ -17,27 +17,27 @@
 */
 
 export * from "./ApiError";
+export * from "./Array";
 export * from "./BitField";
-export * from "./Token";
 //export * from "./Categories";
 export * from "./cdn";
 export * from "./Config";
 export * from "./Constants";
 export * from "./Database";
-export * from "./Email";
+export * from "./email";
 export * from "./Event";
 export * from "./FieldError";
 export * from "./Intents";
+export * from "./InvisibleCharacters";
+export * from "./JSON";
 export * from "./MessageFlags";
 export * from "./Permissions";
 export * from "./RabbitMQ";
 export * from "./Regex";
 export * from "./Rights";
+export * from "./Sentry";
 export * from "./Snowflake";
 export * from "./String";
-export * from "./Array";
+export * from "./Token";
 export * from "./TraverseDirectory";
-export * from "./InvisibleCharacters";
-export * from "./Sentry";
 export * from "./WebAuthn";
-export * from "./JSON";