Skip to content

Commit ecb278d

Browse files
docs: use the latest sendgrid npm module (#246)
1 parent 4570b4e commit ecb278d

File tree

3 files changed

+31
-60
lines changed

3 files changed

+31
-60
lines changed

compute/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
"test": "mocha --timeout 1200000"
1515
},
1616
"dependencies": {
17-
"node-fetch": "^2.3.0",
1817
"@google-cloud/compute": "^0.11.0",
18+
"@sendgrid/mail": "^6.3.1",
19+
"node-fetch": "^2.3.0",
1920
"nodemailer": "^4.3.1",
20-
"nodemailer-smtp-transport": "^2.7.4",
21-
"sendgrid": "^5.2.3"
21+
"nodemailer-smtp-transport": "^2.7.4"
2222
},
2323
"devDependencies": {
2424
"chai": "^4.2.0",

compute/sendgrid.js

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,19 @@
1414
'use strict';
1515

1616
// [START send]
17-
// This sample is based off of https://www.npmjs.com/package/sendgrid#without-mail-helper-class
18-
const Sendgrid = require('sendgrid')(
19-
process.env.SENDGRID_API_KEY || '<your-sendgrid-api-key>'
20-
);
17+
// This sample is based off of:
18+
// https://github.com/sendgrid/sendgrid-nodejs/tree/master/packages/mail
19+
const sendgrid = require('@sendgrid/mail');
20+
sendgrid.setApiKey(process.env.SENDGRID_API_KEY || '<your-sendgrid-api-key>');
2121

22-
async function sendgrid() {
23-
const request = Sendgrid.emptyRequest({
24-
method: 'POST',
25-
path: '/v3/mail/send',
26-
body: {
27-
personalizations: [
28-
{
29-
to: [{email: 'to_email@example.com'}],
30-
subject: 'Sendgrid test email from Node.js on Google Cloud Platform',
31-
},
32-
],
33-
from: {email: 'from_email@example.com'},
34-
content: [
35-
{
36-
type: 'text/plain',
37-
value:
38-
'Hello!\n\nThis a Sendgrid test email from Node.js on Google Cloud Platform.',
39-
},
40-
],
41-
},
22+
async function sendgridExample() {
23+
await sendgrid.send({
24+
to: 'to_email@example.com',
25+
from: 'from_email@example.com',
26+
subject: 'Sendgrid test email from Node.js on Google Cloud Platform',
27+
text:
28+
'Well hello! This is a Sendgrid test email from Node.js on Google Cloud Platform.',
4229
});
43-
44-
const response = await Sendgrid.API(request);
45-
console.log(response);
4630
}
47-
sendgrid().catch(console.error);
48-
31+
sendgridExample().catch(console.error);
4932
// [END send]

compute/test/sendgrid.test.js

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,28 @@
1515

1616
'use strict';
1717

18-
const proxyquire = require(`proxyquire`).noPreserveCache();
19-
const assert = require('assert');
18+
const proxyquire = require('proxyquire');
19+
const {assert} = require('chai');
2020

2121
process.env.SENDGRID_API_KEY = `foo`;
2222

2323
describe('sendgrid', () => {
2424
it('should send an email', () => {
2525
proxyquire(`../sendgrid`, {
26-
sendgrid: key => {
27-
assert.strictEqual(key, `foo`);
28-
return {
29-
emptyRequest: x => x,
30-
API: request => {
31-
assert.deepStrictEqual(request, {
32-
method: `POST`,
33-
path: `/v3/mail/send`,
34-
body: {
35-
personalizations: [
36-
{
37-
to: [{email: `to_email@example.com`}],
38-
subject: `Sendgrid test email from Node.js on Google Cloud Platform`,
39-
},
40-
],
41-
from: {email: `from_email@example.com`},
42-
content: [
43-
{
44-
type: `text/plain`,
45-
value: `Hello!\n\nThis a Sendgrid test email from Node.js on Google Cloud Platform.`,
46-
},
47-
],
48-
},
49-
});
50-
},
51-
};
26+
'@sendgrid/mail': {
27+
setApiKey: key => {
28+
assert.strictEqual(key, `foo`);
29+
},
30+
send: msg => {
31+
assert.deepStrictEqual(msg, {
32+
to: 'to_email@example.com',
33+
from: 'from_email@example.com',
34+
subject:
35+
'Sendgrid test email from Node.js on Google Cloud Platform',
36+
text:
37+
'Well hello! This is a Sendgrid test email from Node.js on Google Cloud Platform.',
38+
});
39+
},
5240
},
5341
});
5442
});

0 commit comments

Comments
 (0)