Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions appengine/sendgrid/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const bodyParser = require('body-parser');
// but will need to be manually set when running locally.
const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY;
const SENDGRID_SENDER = process.env.SENDGRID_SENDER;
const Sendgrid = require('sendgrid')(SENDGRID_API_KEY);
const Sendgrid = require('@sendgrid/client');

Sendgrid.setApiKey(SENDGRID_API_KEY);

const app = express();

Expand All @@ -40,10 +42,10 @@ app.get('/', (req, res) => {
res.render('index');
});

app.post('/hello', (req, res, next) => {
const sgReq = Sendgrid.emptyRequest({
app.post('/hello', async (req, res, next) => {
const request = {
method: 'POST',
path: '/v3/mail/send',
url: '/v3/mail/send',
body: {
personalizations: [
{
Expand All @@ -59,19 +61,33 @@ app.post('/hello', (req, res, next) => {
},
],
},
});
};

// [END gae_flex_sendgrid]

if (req.query.test) {
console.log('TEST!', req.body.email);
request.mailSettings = {
sandboxMode: {
enable: true,
},
};
}

Sendgrid.API(sgReq, err => {
if (err) {
next(err);
return;
}
// Render the index route on success
res.render('index', {
sent: true,
});
// [START gae_flex_sendgrid]
try {
await Sendgrid.request(request);
} catch (err) {
next(err);
return;
}

// Render the index route on success
res.render('index', {
sent: true,
});
});
// [END gae_flex_sendgrid]

if (module === require.main) {
const PORT = process.env.PORT || 8080;
Expand All @@ -82,4 +98,3 @@ if (module === require.main) {
}

module.exports = app;
// [END gae_flex_sendgrid]
25 changes: 22 additions & 3 deletions appengine/sendgrid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,31 @@
"node": ">=8"
},
"scripts": {
"start": "node app.js"
"start": "node app.js",
"test": "mocha test/app.test.js"
},
"cloud-repo-tools": {
"test": {
"app": {
"msg": "Value:"
}
},
"requiresKeyFile": true,
"requiresProjectId": true,
"requiredEnvVars": [
"SENDGRID_SENDER",
"SENDGRID_API_KEY"
]
},
"dependencies": {
"body-parser": "^1.18.3",
"body-parser": "^1.19.0",
"express": "^4.16.4",
"pug": "^2.0.3",
"sendgrid": "^5.2.3"
"@sendgrid/client": "^6.3.0"
},
"devDependencies": {
"assert": "^1.4.1",
"mocha": "^6.1.4",
"supertest": "^4.0.2"
}
}
23 changes: 23 additions & 0 deletions appengine/sendgrid/test/app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const assert = require('assert');
const Supertest = require('supertest');
const supertest = Supertest('http://localhost:8080');

it('GET /: should show homepage template', async () => {
await supertest
.get('/')
.expect(200)
.expect(response => {
assert(response.text.includes('Hello World!'));
});
});

it('POST /hello: should send an email', async () => {
await supertest
.post('/hello?test=true')
.type('form')
.send({email: '[email protected]'})
.expect(200)
.expect(response => {
assert(response.text.includes('Email sent!'));
});
});
2 changes: 1 addition & 1 deletion appengine/sendgrid/views/index.pug
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ html
if sent
p Email sent!
else
form(name="hello", action="/hello", method="post")
form(name="hello", action="/hello?test=true", method="post")
input(type="email", placeholder="Enter your email to send yourself a Hello World message", name="email", style="width: 50%; margin-right: 15px;")
input(type="submit", value="Send")