forked from Wylio/meteor-mandrill
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mandrill.js
66 lines (60 loc) · 3.86 KB
/
mandrill.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
Mandrill = {
options: {},
config: function (options) {
// Taken verbatim from https://mandrillapp.com/api/docs/. We could save a camelCase conversion and camelCase all calls inline below.
var categories = {
users: ['info', 'ping', 'ping2', 'senders'],
messages: ['send', 'send-template', 'search', 'search-time-series', 'info', 'content', 'parse', 'send-raw', 'list-scheduled', 'cancel-scheduled', 'reschedule'],
tags: ['list', 'delete', 'info', 'time-series', 'all-time-series'],
rejects: ['add', 'list', 'delete'],
whitelists: ['add', 'list', 'delete'],
senders: ['list', 'domains', 'add-domain', 'check-domain', 'verify-domain', 'info', 'time-series'],
urls: ['list', 'search', 'time-series', 'tracking-domains', 'add-tracking-domain', 'check-tracking-domain'],
templates: ['add', 'info', 'update', 'publish', 'delete', 'list', 'time-series', 'render'],
webhooks: ['list', 'add', 'info', 'update', 'delete'],
subaccounts: ['list', 'add', 'info', 'update', 'delete', 'pause', 'resume'],
inbound: ['domains', 'add-domain', 'check-domain', 'delete-domain', 'routes', 'add-route', 'update-route', 'delete-route', 'send-raw'],
exports: ['info', 'list', 'rejects', 'whitelist', 'activity'],
ips: ['list', 'info', 'provision', 'start-warmup', 'cancel-warmup', 'set-pool', 'delete', 'list-pools', 'pool-info', 'create-pool', 'delete-pool', 'check-custom-dns', 'set-custom-dns'],
metadata: ['list', 'add', 'update', 'delete']
};
var headers = {
'User-Agent': 'Meteor package wylio:mandrill/1.0.2'
};
var instance = this;
instance.options.username = options["username"];
instance.options.key = options["key"];
instance.options.port = options["port"] || "465";
instance.options.host = "smtp.mandrillapp.com";
instance.options.baseUrl = options.baseUrl || 'https://mandrillapp.com/api/1.0/';
instance.options.protocol = options["protocol"] || this.options.port === "465" ? "smtps" : "smtp";
// set the environment SMTP server
process.env.MAIL_URL = this.options.protocol + "://" + this.options.username + ":" + this.options.key + "@" + this.options.host + ":" + this.options.port;
// wrap the full Mandrill API
Object.keys(categories).forEach(function (category) {
instance[category] = {};
categories[category].forEach(function (call) {
// converting to camelCase is for our convenience; Mandrill takes https://mandrillapp.com/api/1.0/messages.sendTemplate.json as well as https://mandrillapp.com/api/1.0/messages.send-template.json
var camelCaseName = call.replace(/-(.)/g, function (match, p1) {
return p1.toUpperCase()
});
instance[category][camelCaseName] = function (options, callback) {
var url = instance.options.baseUrl + category + '/' + call + '.json';
options = options || {}; // the ping call has no options to send
options.key = options.key || instance.options.key;
// perform an async call if a callback is provided, or return the result otherwise
if (!!callback) {
HTTP.post(url, {data: options, headers: headers}, callback);
} else {
return HTTP.post(url, {data: options, headers: headers});
}
// ^^ that is all this package really does, but we needed a package, didn't we?
}
});
});
},
sendTemplate: function (options) {
console.error('Please see the "Breaking changes" section in the wylio:mandrill README');
process.exit(1);
}
};