This repository has been archived by the owner on Jan 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe.js
58 lines (57 loc) · 1.75 KB
/
subscribe.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
// subscribe(log...
// Shim the Mailgun send function and export an EventEmitter
// tests can use to check invocations.
/* istanbul ignore else */
if (process.env.NODE_ENV === 'test') {
var EventEmitter = require('events').EventEmitter
var events = new EventEmitter()
module.exports = function (requestLog, list, address, subscribed, callback) {
var log = requestLog.log.child({subsystem: 'subscribe'})
var item = {list: list, address: address}
log.info(item)
events.emit('add', item)
callback()
}
module.exports.events = events
} else {
var FormData = require('form-data')
var https = require('https')
var pump = require('pump')
var concat = require('concat-stream')
module.exports = function (requestLog, list, address, callback) {
var key = process.env.MAILGUN_KEY
var form = new FormData()
var log = requestLog.log.child({subsystem: 'subscribe'})
log.info({list: list, address: address})
form.append('subscribed', 'yes')
form.append('upsert', 'yes')
form.append('address', address)
pump(form, https.request({
method: 'POST',
host: 'api.mailgun.net',
path: `/v3/lists/${list}/members`,
auth: 'api:' + key,
headers: form.getHeaders()
}, function (response) {
var status = response.statusCode
if (status === 200) {
log.info({event: 'subscribed'})
callback()
} else {
response
.once('error', function (error) {
log.error(error)
callback(error)
})
.pipe(concat(function (body) {
var error = {
status: response.statusCode,
body: body.toString()
}
log.error(error)
callback(error)
}))
}
}))
}
}