Skip to content

Commit

Permalink
docs: use the latest sendgrid npm module (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Jan 2, 2019
1 parent 4570b4e commit ecb278d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 60 deletions.
6 changes: 3 additions & 3 deletions compute/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
"test": "mocha --timeout 1200000"
},
"dependencies": {
"node-fetch": "^2.3.0",
"@google-cloud/compute": "^0.11.0",
"@sendgrid/mail": "^6.3.1",
"node-fetch": "^2.3.0",
"nodemailer": "^4.3.1",
"nodemailer-smtp-transport": "^2.7.4",
"sendgrid": "^5.2.3"
"nodemailer-smtp-transport": "^2.7.4"
},
"devDependencies": {
"chai": "^4.2.0",
Expand Down
41 changes: 12 additions & 29 deletions compute/sendgrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,19 @@
'use strict';

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

async function sendgrid() {
const request = Sendgrid.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: {
personalizations: [
{
to: [{email: 'to_email@example.com'}],
subject: 'Sendgrid test email from Node.js on Google Cloud Platform',
},
],
from: {email: 'from_email@example.com'},
content: [
{
type: 'text/plain',
value:
'Hello!\n\nThis a Sendgrid test email from Node.js on Google Cloud Platform.',
},
],
},
async function sendgridExample() {
await sendgrid.send({
to: 'to_email@example.com',
from: 'from_email@example.com',
subject: 'Sendgrid test email from Node.js on Google Cloud Platform',
text:
'Well hello! This is a Sendgrid test email from Node.js on Google Cloud Platform.',
});

const response = await Sendgrid.API(request);
console.log(response);
}
sendgrid().catch(console.error);

sendgridExample().catch(console.error);
// [END send]
44 changes: 16 additions & 28 deletions compute/test/sendgrid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,28 @@

'use strict';

const proxyquire = require(`proxyquire`).noPreserveCache();
const assert = require('assert');
const proxyquire = require('proxyquire');
const {assert} = require('chai');

process.env.SENDGRID_API_KEY = `foo`;

describe('sendgrid', () => {
it('should send an email', () => {
proxyquire(`../sendgrid`, {
sendgrid: key => {
assert.strictEqual(key, `foo`);
return {
emptyRequest: x => x,
API: request => {
assert.deepStrictEqual(request, {
method: `POST`,
path: `/v3/mail/send`,
body: {
personalizations: [
{
to: [{email: `to_email@example.com`}],
subject: `Sendgrid test email from Node.js on Google Cloud Platform`,
},
],
from: {email: `from_email@example.com`},
content: [
{
type: `text/plain`,
value: `Hello!\n\nThis a Sendgrid test email from Node.js on Google Cloud Platform.`,
},
],
},
});
},
};
'@sendgrid/mail': {
setApiKey: key => {
assert.strictEqual(key, `foo`);
},
send: msg => {
assert.deepStrictEqual(msg, {
to: 'to_email@example.com',
from: 'from_email@example.com',
subject:
'Sendgrid test email from Node.js on Google Cloud Platform',
text:
'Well hello! This is a Sendgrid test email from Node.js on Google Cloud Platform.',
});
},
},
});
});
Expand Down

0 comments on commit ecb278d

Please sign in to comment.