Skip to content

Commit

Permalink
call sendgrid api with node-fetch #7 #12
Browse files Browse the repository at this point in the history
  • Loading branch information
BDav24 committed Aug 18, 2017
1 parent b643e25 commit eb928b0
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 42 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,22 @@ new NotifmeSdk({
})
```

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

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

</p></details>
<details><summary>SparkPost</summary><p>

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"node-fetch": "1.7.2",
"node-pushnotifications": "1.0.18",
"nodemailer": "4.0.1",
"nodemailer-sendgrid-transport": "0.2.0",
"web-push": "3.2.2",
"winston": "2.3.1"
},
Expand Down
4 changes: 2 additions & 2 deletions src/models/notification-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export type EmailRequestType = CommonRequestType & {
from: string,
to: string,
subject: string,
cc?: string,
bcc?: string,
cc?: string[],
bcc?: string[],
replyTo?: string,
text?: string,
html?: string,
Expand Down
5 changes: 4 additions & 1 deletion src/models/provider-email.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow */

// TODO: notification-catcher API
// TODO?: provider APIs (mailgun, SES, sendinblue, mailjet, sendgrid, postmark, mandrill, elasticemail...)
// TODO?: provider APIs (mailgun, SES, sendinblue, mailjet, mandrill, elasticemail...)
export type EmailProviderType = {
type: 'logger'
} | {
Expand Down Expand Up @@ -64,6 +64,9 @@ export type EmailProviderType = {
rateLimit?: number,
// Proxy options (Doc: https://nodemailer.com/smtp/proxies/)
proxy?: string
} | {
type: 'sendgrid',
apiKey: string
} | {
type: 'sparkpost',
apiKey: string
Expand Down
67 changes: 51 additions & 16 deletions src/providers/email/sendgrid.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,60 @@
/* @flow */
import nodemailer from "nodemailer";
import sgTransport from "nodemailer-sendgrid-transport";
import crypto from 'crypto'
import fetch from 'node-fetch'
// Types
import type { EmailRequestType } from "../../models/notification-request";
import type {EmailRequestType} from '../../models/notification-request'

export default class EmailSendGridProvider {
id: string;
transporter: Object;
id: string
apiKey: string

constructor(config: Object) {
this.id = "email-sendgrid-provider";
const sgOptions = {
auth: {
api_key: config.apiKey
}
};
this.transporter = nodemailer.createTransport(sgTransport(sgOptions));
constructor (config: Object) {
this.id = 'email-sendgrid-provider'
this.apiKey = config.apiKey
}

async send(request: EmailRequestType): Promise<string> {
const result = await this.transporter.sendMail(request);
return result.messageId;
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://api.sendgrid.com/v3/mail/send', {
method: 'POST',
headers: {
Authorization: `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
personalizations: [{
to: [{email: to}],
...(cc && cc.length > 1 ? {cc: cc.map((email) => ({email}))} : null),
...(bcc && bcc.length > 1 ? {bcc: bcc.map((email) => ({email}))} : null)
}],
from: {email: from},
...(replyTo ? {reply_to: {email: replyTo}} : null),
subject: subject,
content: [
...(text ? [{type: 'text/plain', value: text}] : []),
...(html ? [{type: 'text/html', value: html}] : [])
],
headers,
custom_args: {id: generatedId, userId},
...(attachments && attachments.length > 1 ? {
attachments: attachments.map(({contentType, filename, content}) =>
({
type: contentType,
filename,
content: (typeof content === 'string' ? Buffer.from(content) : content).toString('base64')
}))
} : null)
})
})

if (response.ok) {
return generatedId
} else {
const responseBody = await response.json()
const [firstError] = responseBody.errors
const message = Object.keys(firstError).map((key) => `${key}: ${firstError[key]}`).join(', ')
throw new Error(`${response.status} - ${message}`)
}
}
}
25 changes: 3 additions & 22 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2399,7 +2399,7 @@ lodash.restparam@^3.0.0:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"

"lodash@^3.0.1 || ^2.0.0", lodash@^3.10.1:
lodash@^3.10.1:
version "3.10.1"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-3.10.1.tgz#5bf45e8e49ba4189e17d482789dfd15bd140b7b6"

Expand Down Expand Up @@ -2484,7 +2484,7 @@ mime@1.3.4:
version "1.3.4"
resolved "https://registry.yarnpkg.com/mime/-/mime-1.3.4.tgz#115f9e3b6b3daf2959983cb38f149a2d40eb5d53"

mime@^1.2.9, mime@^1.3.4:
mime@^1.3.4:
version "1.3.6"
resolved "https://registry.yarnpkg.com/mime/-/mime-1.3.6.tgz#591d84d3653a6b0b4a3b9df8de5aa8108e72e5e0"

Expand Down Expand Up @@ -2615,12 +2615,6 @@ nodemailer-fetch@1.6.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/nodemailer-fetch/-/nodemailer-fetch-1.6.0.tgz#79c4908a1c0f5f375b73fe888da9828f6dc963a4"

nodemailer-sendgrid-transport@0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/nodemailer-sendgrid-transport/-/nodemailer-sendgrid-transport-0.2.0.tgz#a516593bfe3d1f278cfe17060e1db23658a8f4fc"
dependencies:
sendgrid "^1.8.0"

nodemailer-shared@1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/nodemailer-shared/-/nodemailer-shared-1.0.4.tgz#8b5c5c35bfb29a47dda7d38303f3a4fb47ba38ae"
Expand Down Expand Up @@ -3118,7 +3112,7 @@ repeating@^2.0.0:
dependencies:
is-finite "^1.0.0"

request@^2.60.0, request@^2.81.0:
request@^2.81.0:
version "2.81.0"
resolved "https://registry.yarnpkg.com/request/-/request-2.81.0.tgz#c6928946a0e06c5f8d6f8a9333469ffda46298a0"
dependencies:
Expand Down Expand Up @@ -3241,15 +3235,6 @@ send@0.13.2:
range-parser "~1.0.3"
statuses "~1.2.1"

sendgrid@^1.8.0:
version "1.9.2"
resolved "https://registry.yarnpkg.com/sendgrid/-/sendgrid-1.9.2.tgz#d407e6a206b0a2a6964246dd9c0641c10bf02f19"
dependencies:
lodash "^3.0.1 || ^2.0.0"
mime "^1.2.9"
request "^2.60.0"
smtpapi "^1.2.0"

serve-static@~1.10.2:
version "1.10.3"
resolved "https://registry.yarnpkg.com/serve-static/-/serve-static-1.10.3.tgz#ce5a6ecd3101fed5ec09827dac22a9c29bfb0535"
Expand Down Expand Up @@ -3303,10 +3288,6 @@ smtp-server@1.16.1:
ipv6-normalize "^1.0.1"
nodemailer-shared "^1.1.0"

smtpapi@^1.2.0:
version "1.3.1"
resolved "https://registry.yarnpkg.com/smtpapi/-/smtpapi-1.3.1.tgz#187429eb4ed27df4a3cffd23f0e0245cdfe687d4"

sntp@1.x.x:
version "1.0.9"
resolved "https://registry.yarnpkg.com/sntp/-/sntp-1.0.9.tgz#6541184cc90aeea6c6e7b35e2659082443c66198"
Expand Down

0 comments on commit eb928b0

Please sign in to comment.