Skip to content

Commit

Permalink
feat(mailer.service): refactor usage of defaultsDeep
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The usage of  in  has been refactored. This could potentially alter the way configurations are merged in the application. Update your configurations if necessary to accommodate this change.
  • Loading branch information
juandav committed Apr 27, 2024
1 parent f80e7b9 commit 3506885
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 1,566 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v20.10.0
v20.12.2
71 changes: 47 additions & 24 deletions lib/mailer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { get, defaultsDeep } from 'lodash';
import { Injectable, Inject, Optional, Logger } from '@nestjs/common';
import { SentMessageInfo, Transporter } from 'nodemailer';
import * as previewEmail from 'preview-email';
import * as smtpTransport from 'nodemailer/lib/smtp-transport';

/** Constants **/
Expand Down Expand Up @@ -36,13 +35,26 @@ export class MailerService {
return templateAdapter.compile(mail, callback, this.mailerOptions);
});

if (this.mailerOptions.preview) {
transporter.use('stream', (mail, callback) => {
let previewEmail;

try {
previewEmail = require('preview-email');
} catch (err) {
this.mailerLogger.warn('preview-email is not installed. This is an optional dependency. Install it if you want to preview emails in the development environment. You can install it using npm (npm install preview-email), yarn (yarn add preview-email), or pnpm (pnpm add preview-email).');
}

if (this.mailerOptions.preview) {
transporter.use('stream', (mail, callback) => {
if (typeof previewEmail !== 'undefined') {
return previewEmail(mail.data, this.mailerOptions.preview)
.then(() => callback())
.catch(callback);
});
}
} else {
this.mailerLogger.warn('previewEmail is not available. Skipping preview.');
return callback();
}
});
}
}
}

Expand All @@ -57,15 +69,8 @@ export class MailerService {
if (!transportFactory) {
this.transportFactory = new MailerTransportFactory(mailerOptions);
}
if (
(!mailerOptions.transport ||
Object.keys(mailerOptions.transport).length <= 0) &&
!mailerOptions.transports
) {
throw new Error(
'Make sure to provide a nodemailer transport configuration object, connection url or a transport plugin instance.',
);
}

this.validateTransportOptions();

/** Adapter setup **/
this.templateAdapter = get(
Expand All @@ -86,20 +91,38 @@ export class MailerService {
}

/** Transporters setup **/
if (mailerOptions.transports) {
Object.keys(mailerOptions.transports).forEach((name) => {
const transporter = this.transportFactory.createTransport(this.mailerOptions.transports![name])
this.setupTransporters();
}

private validateTransportOptions(): void {
if (
(!this.mailerOptions.transport ||
Object.keys(this.mailerOptions.transport).length <= 0) &&
!this.mailerOptions.transports
) {
throw new Error(
'Make sure to provide a nodemailer transport configuration object, connection url or a transport plugin instance.',
);
}
}

private createTransporter(config: string | smtpTransport | smtpTransport.Options, name?: string): Transporter {
const transporter = this.transportFactory.createTransport(config);
if (this.mailerOptions.verifyTransporters) this.verifyTransporter(transporter, name);
this.initTemplateAdapter(this.templateAdapter, transporter);
return transporter;
}

private setupTransporters(): void {
if (this.mailerOptions.transports) {
Object.keys(this.mailerOptions.transports).forEach((name) => {
const transporter = this.createTransporter(this.mailerOptions.transports![name], name);
this.transporters.set(name, transporter);
if (mailerOptions.verifyTransporters) this.verifyTransporter(transporter, name);
this.initTemplateAdapter(this.templateAdapter, transporter);
});
}

/** Transporter setup **/
if (mailerOptions.transport) {
this.transporter = this.transportFactory.createTransport();
if (mailerOptions.verifyTransporters) this.verifyTransporter(this.transporter);
this.initTemplateAdapter(this.templateAdapter, this.transporter);
if (this.mailerOptions.transport) {
this.transporter = this.createTransporter(this.mailerOptions.transport);
}
}

Expand Down
25 changes: 14 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"Paweł Partyka <[email protected]>",
"Alexandre TITEUX <[email protected]>",
"Yanarp",
"Joao vitor FREZYN <[email protected]>"
"Joao vitor FREZYN <[email protected]>"
],
"scripts": {
"prebuild": "rimraf dist",
Expand Down Expand Up @@ -54,24 +54,25 @@
]
},
"dependencies": {
"@css-inline/css-inline": "0.14.0",
"glob": "10.3.10",
"preview-email": "3.0.19"
"@css-inline/css-inline": "0.14.1",
"glob": "10.3.12"
},
"optionalDependencies": {
"@types/ejs": "^3.1.5",
"@types/pug": "^2.0.10",
"ejs": "^3.1.9",
"@types/mjml": "^4.7.4",
"ejs": "^3.1.10",
"handlebars": "^4.7.8",
"mjml": "^4.15.3",
"pug": "^3.0.2"
"pug": "^3.0.2",
"liquidjs": "^10.11.1"
},
"devDependencies": {
"@commitlint/cli": "19.3.0",
"@commitlint/config-angular": "19.0.3",
"@commitlint/config-angular": "19.3.0",
"@nestjs/common": "10.3.8",
"@nestjs/core": "10.3.8",
"@nestjs/testing": "10.3.3",
"@nestjs/testing": "10.3.8",
"@types/glob": "8.1.0",
"@types/jest": "29.5.12",
"@types/lodash": "4.17.0",
Expand All @@ -83,8 +84,9 @@
"jest": "29.7.0",
"lint-staged": "15.2.2",
"nodemailer": "6.9.13",
"nodemailer-mock": "2.0.4",
"nodemailer-mock": "2.0.6",
"prettier": "3.2.5",
"preview-email": "3.0.19",
"reflect-metadata": "0.2.2",
"rimraf": "5.0.5",
"rxjs": "7.8.1",
Expand All @@ -100,11 +102,12 @@
"@nestjs/core": ">=7.0.9",
"@types/ejs": ">=3.0.3",
"@types/pug": ">=2.0.6",
"@types/mjml": ">=4.7.4",
"nodemailer": ">=6.4.6",
"ejs": ">=3.1.2",
"handlebars": ">=4.7.6",
"mjml": ">=4.15.3",
"nodemailer": ">=6.4.6",
"pug": ">=3.0.1",
"liquidjs": "^10.8.2"
"liquidjs": ">=10.8.2"
}
}
Loading

0 comments on commit 3506885

Please sign in to comment.