diff --git a/compute/package.json b/compute/package.json index 5903345973..2cb90f5b05 100644 --- a/compute/package.json +++ b/compute/package.json @@ -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", diff --git a/compute/sendgrid.js b/compute/sendgrid.js index 2013785759..8b06f4ddb2 100644 --- a/compute/sendgrid.js +++ b/compute/sendgrid.js @@ -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 || '' -); +// 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 || ''); -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] diff --git a/compute/test/sendgrid.test.js b/compute/test/sendgrid.test.js index a3e1aa4d84..8d83f66214 100644 --- a/compute/test/sendgrid.test.js +++ b/compute/test/sendgrid.test.js @@ -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.', + }); + }, }, }); });