Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/mobapi/notifme-sdk into m…
Browse files Browse the repository at this point in the history
…obapi-master
  • Loading branch information
BDav24 committed Oct 21, 2018
2 parents 709c6e2 + b7fde04 commit b528715
Show file tree
Hide file tree
Showing 6 changed files with 597 additions and 537 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,22 @@ new NotifmeSdk({
})
```

</p></details>
<details><summary>Mandrill</summary><p>

```javascript
new NotifmeSdk({
channels: {
email: {
providers: [{
type: 'mandrill',
apiKey: 'xxxxx'
}]
}
}
})
```

</p></details>
<details><summary>Sendgrid</summary><p>

Expand Down
45 changes: 45 additions & 0 deletions __tests__/providers/email/mandrill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* @flow */
/* global jest, test, expect */
import NotifmeSdk from '../../../src'

const mockSendMail = jest.fn()
mockSendMail.mockReturnValue({messageId: 'returned-id'})
jest.mock('nodemailer', () => ({
createTransport: () => ({
sendMail: mockSendMail
})
}))
jest.mock('../../../src/util/logger', () => ({
warn: jest.fn()
}))

const sdk = new NotifmeSdk({
channels: {
email: {
providers: [{
type: 'mandrill',
apiKey: 'apikey'
}]
}
}
})

const request = {
email: {
from: '[email protected]',
to: '[email protected]',
subject: 'Hi John',
text: 'Hello John! How are you?'
}
}

test('Mandrill should use nodemailer.', async () => {
const result = await sdk.send(request)
expect(mockSendMail).lastCalledWith(request.email)
expect(result).toEqual({
status: 'success',
channels: {
email: {id: 'returned-id', providerId: 'email-mandrill-provider'}
}
})
})
4 changes: 4 additions & 0 deletions src/models/provider-email.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ export type EmailProviderType = {
type: 'mailgun',
apiKey: string,
domainName: string
} | {
// Options (Doc: https://github.com/Rebelmail/nodemailer-mandrill-transport)
type: 'mandrill',
apiKey: string
} | {
type: 'sendgrid',
apiKey: string
Expand Down
4 changes: 4 additions & 0 deletions src/providers/email/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* @flow */
import EmailLoggerProvider from '../logger'
import EmailMailgunProvider from './mailgun'
import EmailMandrillProvider from './mandrill'
import EmailNotificationCatcherProvider from './notificationCatcher'
import EmailSendGridProvider from './sendgrid'
import EmailSesProvider from './ses'
Expand Down Expand Up @@ -39,6 +40,9 @@ export default function factory ({ type, ...config }: Object): EmailProviderType
case 'mailgun':
return new EmailMailgunProvider(config)

case 'mandrill':
return new EmailMandrillProvider(config)

case 'sendgrid':
return new EmailSendGridProvider(config)

Expand Down
61 changes: 61 additions & 0 deletions src/providers/email/mandrill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* @flow */
import crypto from 'crypto'
import fetch from '../../util/request'
// types
import type {EmailRequestType} from '../../models/notification-request'

export default class EmailMandrillProvider {
id: string = 'email-mandrill-provider'
apiKey: string

constructor (config: Object) {
this.apiKey = config.apiKey
}

async send (request: EmailRequestType): Promise<string> {
const {id, userId, from, replyTo, subject, html, text, headers, to, cc, bcc, attachments} = request
const generatedId = id || crypto.randomBytes(16).toString('hex')
const response = await fetch(`https://mandrillapp.com/api/1.0/messages/send.json`, {
method: 'POST',
headers: {
'User-Agent': 'notifme-sdk/v1 (+https://github.com/notifme/notifme-sdk)',
'Content-Type': 'application/json'
},
body: JSON.stringify({
key: this.apiKey,
message: {
from_email: from,
to: [
{ email: to, type: 'to' },
...(cc && cc.length ? cc.map(recipient => { return { email: recipient, type: 'cc' } }) : {}),
...(bcc && bcc.length ? bcc.map(recipient => { return { email: recipient, type: 'bcc' } }) : {}),
],
subject,
text,
html,
headers: {
...(replyTo ? { "Reply-To": replyTo } : null),
...headers
},
...(attachments && attachments.length ? {
attachments: attachments.map(({ contentType, filename, content }) => {
return { type: contentType, name: filename, content: content }
})
} : null),
metadata: {
id,
userId
}
},
async: false,
})
})

const responseBody = await response.json()
if (response.ok) {
return generatedId
} else {
throw new Error(`${response.status} - ${responseBody.message}`)
}
}
}
Loading

0 comments on commit b528715

Please sign in to comment.