-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
127 lines (115 loc) · 3.37 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
'use strict'
const request = require('request-micro')
const aws4 = require('aws4')
const assert = require('assert')
const encoder = require('aws-form-urlencoded')
module.exports = makeClient
function noop () {}
function functionElseNoop (func) {
if (func && typeof func === 'function') {
return func
}
return noop
}
function logWrapper (loggerArg) {
const logger = loggerArg || {}
return {
error: functionElseNoop(logger.error),
warn: functionElseNoop(logger.warn),
info: functionElseNoop(logger.info),
debug: functionElseNoop(logger.debug)
}
}
function makeClient (options) {
let context = Object.assign({}, options)
assert(context.region, 'Region is a required option for SES clients')
// Configure optional logger
context.logger = logWrapper(context.logger)
return {
sendEmail: sendEmail.bind(null, context)
}
}
function sendEmail (context, options, callback) {
let sendResult
// encodeBody and aws4.sign can both throw before the promise starts
try {
context.logger.info('SES: starting send', options)
sendResult = request(
aws4.sign({
service: 'email',
region: context.region,
method: 'POST',
protocol: 'https:',
path: '/',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: encodeBody(context, options)
})
).then(response => {
if (response.statusCode >= 400) {
context.logger.error(
'SES: ',
response.statusCode,
response.data && response.data.toString()
)
return Promise.reject(
new Error(
'Amazon SES service returned status code: ' + response.statusCode
)
)
}
return response
})
} catch (e) {
context.logger.error('SES: ', e)
sendResult = Promise.reject(e)
}
if (!callback) {
return sendResult.then(result => {
context.logger.info(
'SES: finished',
result.statusCode,
result.statusMessage
)
context.logger.info('SES: data', result.data.toString())
return result
})
}
sendResult
.then(result => callback(null, result))
.catch(error => callback(error))
}
function encodeBody (context, options) {
context.logger.debug('SES: validating params', options)
validateParams(options)
context.logger.debug('SES: params validated')
let body = encoder(Object.assign({}, options, { Action: 'SendEmail' }))
context.logger.debug('SES: body encoded', body)
return body
}
const requiredEmailParams = ['Source', 'Destination', 'Message']
function validateParams (params) {
requiredEmailParams.forEach(prop =>
assert(params[prop], `The "${prop}" property is required`)
)
assert(params.Message.Body, 'The "Message.Body" property is required')
assert(params.Message.Subject, 'The "Message.Subject" property is required')
assert(
params.Message.Subject.Data,
'The "Message.Subject.Data" property is required'
)
if ('Html' in params.Message.Body) {
assert(
params.Message.Body.Html.Data,
'The "Message.Body.Html.Data" property is required when using Html'
)
} else if ('Text' in params.Message.Body) {
assert(
params.Message.Body.Text.Data,
'The "Message.Body.Text.Data" property is required when using Text'
)
} else {
throw new Error('One of "Html", "Text" is required on Message.Body')
}
}