-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
44 lines (36 loc) · 1.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
var Hoek = require('hoek'),
Joi = require('joi'),
HapiSendGridClient = require('./client'),
Promise = require('bluebird');
var internals = {
defaults: {
clientOptions: {},
emailOptions: {}
}
};
internals.schema = Joi.object({
username: Joi.string(),
password: Joi.string(),
apiKey: Joi.string(),
clientOptions: Joi.object(),
emailOptions: Joi.object(),
})
.min(1)
.and('username', 'password')
.without('apiKey', ['username', 'password']);
exports.register = function(server, options, next) {
var sendgrid,
settings = Hoek.applyToDefaults(internals.defaults, options);
Joi.assert(settings, internals.schema, 'Invalid hapi-sendgrid configuration. Must specify either a SendGrid API Key OR a SendGrid username/password.');
if (settings.apiKey) {
sendgrid = require('sendgrid')(settings.apiKey, settings.clientOptions);
} else {
sendgrid = require('sendgrid')(settings.username, settings.password, settings.clientOptions);
}
// expose the sendgrid client
server.expose('client', new HapiSendGridClient(Promise.promisifyAll(sendgrid), settings.emailOptions));
next();
}
exports.register.attributes = {
pkg: require('./package.json')
};