-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail.js
46 lines (43 loc) · 1.31 KB
/
email.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
45
46
/*******************************************************************************
*
*
* (c) Copyright Merative US L.P. and others 2020-2022
*
* SPDX-Licence-Identifier: Apache 2.0
*
*******************************************************************************/
const sendGrid = require('@sendgrid/mail');
class EmailService {
/**
* Wrapper for sendgrid email API.
*
* @param config {sendgrid: {SENDGRID_API_KEY}}
* Only the API key is required.
*/
constructor(config){
if(!config){
throw new Error("A config object is required");
}
if(!config.sendgrid){
throw new Error("The sendgrid property in the config is required");
}
if(!config.sendgrid.SENDGRID_API_KEY) {
throw new Error("Missing SENDGRID_API_KEY");
}
try {
this.config = config;
sendGrid.setApiKey(config.sendgrid.SENDGRID_API_KEY);
} catch (err) {
throw err;
}
}
/**
* Send one or more messages
* @param message - An object defined in https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/README.md
* @returns {Promise<[ClientResponse, {}]>}
*/
async send(message){
return await sendGrid.send(message);
}
}
module.exports = EmailService;