From d60a3930c5dc986df992c6c7c4cedbe8f9e5684f Mon Sep 17 00:00:00 2001 From: Fadion Dashi Date: Tue, 20 Mar 2018 18:21:27 +0100 Subject: [PATCH] Mail: Pass configuration in transport instead of the Mail class Signed-off-by: Fadion Dashi --- packages/mail/README.md | 14 +++++++------- packages/mail/lib/mail.js | 2 +- packages/mail/lib/transports/ses.js | 11 +++++++++-- packages/mail/lib/transports/smtp.js | 12 +++++++++--- packages/mail/test/mailer.spec.js | 2 +- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/packages/mail/README.md b/packages/mail/README.md index 2ff9396..3f8c58d 100644 --- a/packages/mail/README.md +++ b/packages/mail/README.md @@ -13,8 +13,8 @@ We'll start with an exhaustive example that includes pretty much every option. ```javascript const { Mail, Transport } = require('@sapphirejs/mail') -const config = {/* nodemailer configuration */} -const mail = new Mail(config, new Transport.SMTP()) +const config = { host: 'smtp.example.com' } +const mail = new Mail({}, new Transport.SMTP(config)) await mail.send('

Hi

', message => { message .from('from@domain.com') @@ -40,10 +40,10 @@ await mail.send({ html: '

Hi

', text: 'Hi' }, /* rest of the message */) ### Global "from" Header -When the "from" header is passed in the config, it will be automatically included in every mail instance. Off course it also be overriden with the `from` function. +When the "from" header is passed as `Mail`'s config, it will be automatically included in every mail instance. Off course it also be overriden with the `from` function. ```javascript -let config = { from: 'from@domain.com', /* other config options */ } +const mail = new Mail({ from: 'from@domain.com' }, new Transport.SMTP(config)) ``` ### Name, Email Format @@ -80,7 +80,7 @@ The same applies to `header`, `attachment`, and `alternative`. ```javascript try { - const mail = new Mail(config, new Transport.SMTP()) + const mail = new Mail({}, new Transport.SMTP(config)) const result = await mail .send('

Hello

', message => { message @@ -108,7 +108,7 @@ const config = { } } -const mail = new Mail(config, new Transport.SMTP()) +const mail = new Mail({}, new Transport.SMTP(config)) ``` ### SES Transport @@ -122,5 +122,5 @@ const config = {{ region: 'us-east-1' } -const mail = new Mail(config, new Transport.SES()) +const mail = new Mail({}, new Transport.SES(config)) ``` diff --git a/packages/mail/lib/mail.js b/packages/mail/lib/mail.js index a5c3283..35b92d6 100644 --- a/packages/mail/lib/mail.js +++ b/packages/mail/lib/mail.js @@ -47,7 +47,7 @@ class Mail { if (validationError) throw new MissingMailParams(validationError) - return this._transport.send(this._config, messageWithBody) + return this._transport.send(messageWithBody) .then(info => { return info }) diff --git a/packages/mail/lib/transports/ses.js b/packages/mail/lib/transports/ses.js index 77b8acd..3e3e600 100644 --- a/packages/mail/lib/transports/ses.js +++ b/packages/mail/lib/transports/ses.js @@ -7,6 +7,13 @@ const aws = require('aws-sdk') * @class SES */ class SES { + /** + * @param {Object} config + */ + constructor(config) { + this._config = config + } + /** * Sends the message. * @@ -15,9 +22,9 @@ class SES { * @param {Object} message * @returns {Prommise} */ - send(config, message) { + send(message) { const transport = nodemailer.createTransport({ - SES: new aws.SES(config) + SES: new aws.SES(this._config) }) return transport.sendMail(message) } diff --git a/packages/mail/lib/transports/smtp.js b/packages/mail/lib/transports/smtp.js index 4351431..d9b24e6 100644 --- a/packages/mail/lib/transports/smtp.js +++ b/packages/mail/lib/transports/smtp.js @@ -6,16 +6,22 @@ const nodemailer = require('nodemailer') * @class SMTP */ class SMTP { + /** + * @param {Object} config + */ + constructor(config) { + this._config = config + } + /** * Sends the message. * * @public - * @param {Object} config * @param {Object} message * @returns {Prommise} */ - send(config, message) { - const transport = nodemailer.createTransport(config) + send(message) { + const transport = nodemailer.createTransport(this._config) return transport.sendMail(message) } } diff --git a/packages/mail/test/mailer.spec.js b/packages/mail/test/mailer.spec.js index e6fca54..bca9c7b 100644 --- a/packages/mail/test/mailer.spec.js +++ b/packages/mail/test/mailer.spec.js @@ -6,7 +6,7 @@ class MockTransport { this._shouldErr = shouldErr } - send(config, message) { + send(message) { return new Promise((resolve, reject) => { if (this._shouldErr) reject('error') // Transport isn't supposed to return the message