-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
91 lines (83 loc) · 2.59 KB
/
index.js
File metadata and controls
91 lines (83 loc) · 2.59 KB
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
let req = require('request'),
querystring = require('querystring');
module.exports = function(apiKey, url = 'https://cnai48194m.execute-api.us-east-2.amazonaws.com/v1/') {
this.apiKey = apiKey;
this.url = url ;
};
let funcObj = {
createList: function(o, cb) {
if (!o.list_name) return cb(new Error('Missing "list_name" parameter'), null);
this.request('email/lists', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'x-api-key': this.apiKey,
},
body: querystring.stringify({
list_name: o.list_name
})
}, function(err, res, body) {
if (err) cb(err, null);
else {
cb(null, body);
}
});
},
bulkSubscribe: function(o, cb) {
if (!o.list_id) return cb(new Error('Missing "list_id" parameter'), null);
/*
o.data.forEach(function(emailData) {
if (!emailData.email) return cb(new Error('Missing "email" parameter'), null);
});
*/
let path = 'email/lists/' + o.list_id + '/subscribers';
delete o.list_id;
this.request(path, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': this.apiKey,
},
body: JSON.stringify({
"data": o.data
})
}, function(err, res, body) {
if (err) cb(err, null);
else {
cb(null, body);
}
});
},
subscribe: function(o, cb) {
if (!o.list_id) return cb(new Error('Missing "list_id" parameter'), null);
if (!o.email) return cb(new Error('Missing "email" parameter'), null);
let path = 'email/lists/' + o.list_id + '/subscribers';
delete o.list_id;
this.request(path, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': this.apiKey,
},
body: JSON.stringify({
"data": [
o
]
})
}, function(err, res, body) {
if (err) cb(err, null);
else {
cb(null, body);
}
});
},
request: function(path, o,cb) {
req({
url: this.url + path,
method: o.method,
headers: o.headers,
body: o.body
}, cb);
}
};
for (var k in funcObj) module.exports.prototype[k] = funcObj[k];