-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/mobapi/notifme-sdk into m…
…obapi-master
- Loading branch information
Showing
6 changed files
with
597 additions
and
537 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'} | ||
} | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} | ||
} | ||
} |
Oops, something went wrong.