Exploring the interface of a mail adapter
Add some tests and demonstrate the adapter loading interface
This commit is contained in:
committed by
Florent Vilmart
parent
d9f1e00345
commit
8dc37b9d30
@@ -1,4 +1,3 @@
|
||||
|
||||
export function loadAdapter(options, defaultAdapter) {
|
||||
let adapter;
|
||||
|
||||
@@ -12,7 +11,7 @@ export function loadAdapter(options, defaultAdapter) {
|
||||
adapter = options.adapter;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!adapter) {
|
||||
adapter = defaultAdapter;
|
||||
}
|
||||
@@ -26,10 +25,12 @@ export function loadAdapter(options, defaultAdapter) {
|
||||
}
|
||||
}
|
||||
// From there it's either a function or an object
|
||||
// if it's an function, instanciate and pass the options
|
||||
// if it's an function, instanciate and pass the options
|
||||
if (typeof adapter === "function") {
|
||||
var Adapter = adapter;
|
||||
adapter = new Adapter(options);
|
||||
}
|
||||
return adapter;
|
||||
}
|
||||
|
||||
module.exports = { loadAdapter }
|
||||
|
||||
39
src/Adapters/Email/SimpleMailgunAdapter.js
Normal file
39
src/Adapters/Email/SimpleMailgunAdapter.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import Mailgun from 'mailgun-js';
|
||||
|
||||
let SimpleMailgunAdapter = mailgunOptions => {
|
||||
if (!mailgunOptions || !mailgunOptions.apiKey || !mailgunOptions.domain) {
|
||||
throw 'SimpleMailgunAdapter requires an API Key and domain.';
|
||||
}
|
||||
let mailgun = Mailgun(mailgunOptions);
|
||||
|
||||
let sendMail = (to, subject, text) => {
|
||||
let data = {
|
||||
from: mailgunOptions.fromAddress,
|
||||
to: to,
|
||||
subject: subject,
|
||||
text: text,
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
mailgun.messages().send(data, (err, body) => {
|
||||
if (typeof err !== 'undefined') {
|
||||
reject(err);
|
||||
}
|
||||
resolve(body);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
sendVerificationEmail: ({ link, user, appName, }) => {
|
||||
let verifyMessage =
|
||||
"Hi,\n\n" +
|
||||
"You are being asked to confirm the e-mail address " + user.email + " with " + appName + "\n\n" +
|
||||
"" +
|
||||
"Click here to confirm it:\n" + link;
|
||||
return sendMail(user.email, 'Please verify your e-mail for ' + appName, verifyMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = SimpleMailgunAdapter
|
||||
25
src/Adapters/loadAdapter.js
Normal file
25
src/Adapters/loadAdapter.js
Normal file
@@ -0,0 +1,25 @@
|
||||
export default options => {
|
||||
if (!options) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (typeof options === 'string') {
|
||||
//Configuring via module name with no options
|
||||
return require(options)();
|
||||
}
|
||||
|
||||
if (!options.module && !options.class) {
|
||||
//Configuring via object
|
||||
return options;
|
||||
}
|
||||
|
||||
if (options.module) {
|
||||
//Configuring via module name + options
|
||||
return require(options.module)(options.options)
|
||||
}
|
||||
|
||||
if (options.class) {
|
||||
//Configuring via class + options
|
||||
return options.class(options.options);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user