Files
freeCodeCamp/api/src/plugins/mailer.ts
Niraj Nandish 70741db619 feat(api): report user endpoint (#51170)
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
2023-10-11 20:49:05 +05:30

47 lines
827 B
TypeScript

import type { FastifyPluginCallback } from 'fastify';
import fp from 'fastify-plugin';
declare module 'fastify' {
interface FastifyInstance {
sendEmail: SendEmail;
}
}
export type SendEmailArgs = {
to: string;
from: string;
subject: string;
text: string;
cc?: string;
};
type SendEmail = (args: SendEmailArgs) => Promise<void>;
export interface MailProvider {
send: SendEmail;
}
const plugin: FastifyPluginCallback<{ provider: MailProvider }> = (
fastify,
options,
done
) => {
const { provider } = options;
if (!provider)
return done(
Error(
"The mailer plugin must be passed a provider via register's options."
)
);
fastify.decorate(
'sendEmail',
async (args: SendEmailArgs) => await provider.send(args)
);
done();
};
export default fp(plugin);