@athenna/mail
Version:
The Athenna email handler. Built on top of nodemailer.
52 lines (51 loc) • 1.78 kB
JavaScript
/**
* @athenna/mail
*
* (c) Victor Tesoura Júnior <txsoura@athenna.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { Config } from '@athenna/config';
import { SmtpDriver } from '#src/drivers/SmtpDriver';
import { FakeDriver } from '#src/drivers/FakeDriver';
import { NotFoundDriverException } from '#src/exceptions/NotFoundDriverException';
import { NotImplementedConfigException } from '#src/exceptions/NotImplementedConfigException';
export class DriverFactory {
/**
* Driver of driver factory.
*/
static { this.drivers = new Map()
.set('fake', { Driver: FakeDriver })
.set('smtp', { Driver: SmtpDriver }); }
/**
* Return an array of all available drivers.
*/
static availableDrivers() {
return [...this.drivers.keys()];
}
/**
* Fabricate a new instance of a driver based in mailer configurations.
*/
static fabricate(mailerName, runtimeConfig = {}) {
const mailerConfig = this.getMailerConfig(mailerName);
const { Driver } = this.drivers.get(mailerConfig.driver);
return new Driver({ ...mailerConfig, ...runtimeConfig });
}
/**
* Get all mailer configuration.
*/
static getMailerConfig(mailerName) {
if (mailerName === 'default') {
mailerName = Config.get('mail.default');
}
const mailerConfig = Config.get(`mail.mailers.${mailerName}`);
if (!mailerConfig) {
throw new NotImplementedConfigException(mailerName);
}
if (!this.drivers.has(mailerConfig.driver)) {
throw new NotFoundDriverException(mailerConfig.driver);
}
return mailerConfig;
}
}