Skip to content

Commit

Permalink
feat(drivers): drivers receives the config via setConfig method
Browse files Browse the repository at this point in the history
since drivers can control their construction lifecycle, we pass them the config via `setConfig`
method
  • Loading branch information
thetutlage committed Aug 24, 2017
1 parent d2f2771 commit 45911a3
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 18 deletions.
138 changes: 132 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"author": "virk",
"license": "MIT",
"devDependencies": {
"@adonisjs/fold": "^4.0.2",
"@adonisjs/sink": "^1.0.13",
"coveralls": "^2.13.1",
"cz-conventional-changelog": "^2.0.0",
Expand All @@ -41,10 +42,13 @@
}
},
"nyc": {
"exclude": ["bin"]
"exclude": [
"bin"
]
},
"dependencies": {
"@adonisjs/generic-exceptions": "^1.0.0",
"debug": "^3.0.0",
"form-data": "^2.2.0",
"get-stream": "^3.0.0",
"got": "^7.1.0",
Expand Down
10 changes: 9 additions & 1 deletion src/Mail/Drivers/Smtp.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ const nodemailer = require('nodemailer')
* @constructor
*/
class SmtpDriver {
constructor (config) {
/**
* This method is called by mail manager automatically
* and passes the config object
*
* @method setConfig
*
* @param {Object} config
*/
setConfig (config) {
this.transporter = nodemailer.createTransport(config)
}

Expand Down
16 changes: 15 additions & 1 deletion src/Mail/Drivers/SparkPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const nodemailer = require('nodemailer')
const getStream = require('get-stream')
const Request = require('../../Request')

/**
* The core transportor node-mailer
*
* @class SparkPostTransporter
* @constructor
*/
class SparkPostTransporter {
constructor (config) {
this.config = config
Expand Down Expand Up @@ -182,7 +188,15 @@ class SparkPostTransporter {
* @constructor
*/
class SparkPost {
constructor (config) {
/**
* This method is called by mail manager automatically
* and passes the config object
*
* @method setConfig
*
* @param {Object} config
*/
setConfig (config) {
this.transporter = nodemailer.createTransport(new SparkPostTransporter(config))
}

Expand Down
28 changes: 26 additions & 2 deletions src/Mail/Manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

const GE = require('@adonisjs/generic-exceptions')
const { ioc } = require('@adonisjs/fold')
const Drivers = require('./Drivers')
const MailSender = require('./Sender')

Expand All @@ -21,6 +22,26 @@ const MailSender = require('./Sender')
* @constructor
*/
class MailManager {
constructor () {
this._drivers = {}
}

/**
* Exposing api to be extend, IoC container will
* use this method when someone tries to
* extend mail provider
*
* @method extend
*
* @param {String} name
* @param {Object} implementation
*
* @return {void}
*/
extend (name, implementation) {
this._drivers[name] = implementation
}

/**
* Returns an instance of sender with the defined
* driver.
Expand All @@ -37,13 +58,16 @@ class MailManager {
throw GE.InvalidArgumentException.invalidParameter('Cannot get driver instance without a name')
}

const Driver = Drivers[name.toLowerCase()]
name = name.toLowerCase()
const Driver = Drivers[name] || this._drivers[name]

if (!Driver) {
throw GE.InvalidArgumentException.invalidParameter(`${name} is not a valid mail driver`)
}

return new MailSender(new Driver(config))
const driverInstance = ioc.make(Driver)
driverInstance.setConfig(config)
return new MailSender(driverInstance)
}
}

Expand Down
6 changes: 4 additions & 2 deletions test/mail-sender.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ test.group('Mail sender', () => {
/**
* Driver instance
*/
const smtp = new SmtpDriver({
const smtp = new SmtpDriver()
smtp.setConfig({
host: process.env.SMTP_HOST,
port: Number(process.env.SMTP_PORT),
auth: {
Expand Down Expand Up @@ -96,7 +97,8 @@ test.group('Mail sender', () => {
/**
* Driver instance
*/
const smtp = new SmtpDriver({
const smtp = new SmtpDriver()
smtp.setConfig({
host: process.env.SMTP_HOST,
port: Number(process.env.SMTP_PORT),
auth: {
Expand Down
12 changes: 8 additions & 4 deletions test/smtp-driver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ test.group('Stmp driver', (group) => {
})

test('newup smtp driver', (assert) => {
const smtp = new SmtpDriver(this.config)
const smtp = new SmtpDriver()
smtp.setConfig(this.config)
assert.instanceOf(smtp, SmtpDriver)
assert.isDefined(smtp.transporter)
})

test('send plain email', async (assert) => {
const smtp = new SmtpDriver(this.config)
const smtp = new SmtpDriver()
smtp.setConfig(this.config)

const response = await helpers.processWithDelay(smtp.send({
from: process.env.SMTP_FROM_EMAIL,
Expand All @@ -53,7 +55,8 @@ test.group('Stmp driver', (group) => {
}).timeout(0)

test('send email with attachment', async (assert) => {
const smtp = new SmtpDriver(this.config)
const smtp = new SmtpDriver()
smtp.setConfig(this.config)

const response = await helpers.processWithDelay(smtp.send({
from: process.env.SMTP_FROM_EMAIL,
Expand Down Expand Up @@ -81,7 +84,8 @@ test.group('Stmp driver', (group) => {
test('throw errors if unable to send email', async (assert) => {
assert.plan(1)
this.config.auth.user = null
const smtp = new SmtpDriver(this.config)
const smtp = new SmtpDriver()
smtp.setConfig(this.config)

try {
await helpers.processWithDelay(smtp.send({
Expand Down
Loading

0 comments on commit 45911a3

Please sign in to comment.