Refactor mailer setup and validate web configuration in app

This commit refactors how SMTP configuration for mailers is set up and introduces schema validation for incoming configurations. The mailer modules have been restructured, with schema definition files added, and redundant codes removed. Moreover, web application configuration now has minimum validation on name and title, and URL validation has been added.
This commit is contained in:
giancarlo
2024-03-27 14:10:53 +08:00
parent ff19e0c204
commit 2acd9c7d10
12 changed files with 146 additions and 70 deletions

View File

@@ -0,0 +1,18 @@
import { z } from 'zod';
export const MailerSchema = z
.object({
to: z.string().email(),
from: z.string().email(),
subject: z.string(),
})
.and(
z.union([
z.object({
text: z.string(),
}),
z.object({
html: z.string(),
}),
]),
);

View File

@@ -0,0 +1,49 @@
import { z } from 'zod';
/*
const user = process.env.EMAIL_USER;
const pass = process.env.EMAIL_PASSWORD;
const host = process.env.EMAIL_HOST;
const port = Number(process.env.EMAIL_PORT);
const secure = process.env.EMAIL_TLS !== 'false';
// validate that we have all the required appConfig
if (!user || !pass || !host || !port) {
throw new Error(
`Missing email configuration. Please add the following environment variables:
EMAIL_USER
EMAIL_PASSWORD
EMAIL_HOST
EMAIL_PORT
`,
);
}
return {
host,
port,
secure,
auth: {
user,
pass,
},
};
*/
export const SmtpConfigSchema = z.object({
user: z.string({
description:
'This is the email account to send emails from. This is specific to the email provider.',
}),
pass: z.string({
description: 'This is the password for the email account',
}),
host: z.string({
description: 'This is the SMTP host for the email provider',
}),
port: z.number({
description:
'This is the port for the email provider. Normally 587 or 465.',
}),
secure: z.boolean(),
});