Skip to content

Commit

Permalink
feat(mailer): add gmail nodemailer transport; change SES handling
Browse files Browse the repository at this point in the history
Amazon SES creds have to be provided in configuration object, not in a JSON file
  • Loading branch information
adekbadek committed Mar 8, 2018
1 parent 8e24a57 commit 02b3fdf
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
18 changes: 17 additions & 1 deletion README.md
Expand Up @@ -9,7 +9,7 @@ Basically a very limited version of what [keygen.sh](https://keygen.sh/) does.

## Quick start

1. configure [Amazon SES](https://aws.amazon.com/ses/) and create `aws-config.json` file.
1. configure [Amazon SES](https://aws.amazon.com/ses/) or get some gmail creds

1. install

Expand All @@ -25,9 +25,25 @@ Basically a very limited version of what [keygen.sh](https://keygen.sh/) does.
authkey({
from: 'superthing@things.com'
productName: 'SuperThing',
mailerConfig: {
// see below for other services
type: 'gmail',
credentials: {
user: process.env.GMAIL_USER,
pass: process.env.GMAIL_PASS,
}
},
}).listen()
```

### `mailerConfig` options

| service (`type`) | required `credentials` |
| :------------- | :------------- |
| `amazonSES` | `accessKeyId`, `secretAccessKey`, `region` |
| `gmail` | `user`, `pass` |


## API Reference

| verb | endpoint | what it does |
Expand Down
10 changes: 9 additions & 1 deletion __tests__/mailer.js
@@ -1,4 +1,4 @@
const {getMailOptions} = require('../src/mailer')
const {getMailOptions, mailer} = require('../src/mailer')

describe('getMailOptions', () => {
const createMailOptions = (config) => getMailOptions({
Expand All @@ -24,3 +24,11 @@ describe('getMailOptions', () => {
})).toMatchSnapshot()
})
})

describe('mailer', () => {
it('throws if no mailerConfig is provided', () => {
expect(() => {
mailer({})
}).toThrow(/mailerConfig must be/)
})
})
1 change: 1 addition & 0 deletions __tests__/server.js
Expand Up @@ -6,6 +6,7 @@ const instance = authkey({
from: 'someone@mail.com',
productName: process.env.PRODUCT_NAME,
dbFile: '__tests__/db.json',
mailerConfig: {},
})

beforeEach(() => {
Expand Down
30 changes: 18 additions & 12 deletions src/mailer/index.js
@@ -1,5 +1,4 @@
const nodemailer = require('nodemailer')
const aws = require('aws-sdk')

const isTest = process.env.NODE_ENV === 'test'

Expand Down Expand Up @@ -32,24 +31,31 @@ const sendMail = ({nodemailerTransport, config}) => ({to, authkey}) => new Promi
)
})

const getSESTransprter = () => {
if (isTest) {
} else {
// configure AWS SDK
aws.config.loadFromPath('aws-config.json')
const getNodemailerTransport = ({type, credentials}) => {
if (!isTest) {
const transport = {}

// create Nodemailer SES transporter
return nodemailer.createTransport({
SES: new aws.SES(),
})
if (type === 'amazonSES') {
const aws = require('aws-sdk')
aws.config.update(credentials)
transport.SES = new aws.SES()
} else if (type === 'gmail') {
transport.service = 'gmail'
transport.auth = credentials
}

return nodemailer.createTransport(transport)
}
}

module.exports = {
mailer: (config) => {
mailer: ({mailerConfig, ...config}) => {
if (!mailerConfig) {
throw new Error(`mailerConfig must be provided in config object`)
}
return ({
send: sendMail({
nodemailerTransport: getSESTransprter(),
nodemailerTransport: getNodemailerTransport(mailerConfig),
config,
}),
})
Expand Down

0 comments on commit 02b3fdf

Please sign in to comment.