-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
225 lines (200 loc) · 5.65 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* index.js
* Entry point into hover-api
*
* (C) Steven White 2015
*/
var request = require('request'),
_ = require('lodash');
module.exports = function (username, password) {
// Base url for all hover api requests
var baseUrl = 'https://www.hover.com/api';
// Captured from cookie in hover authentication request.
var cookies = request.jar();
// Note whether login has occured.
// A successful login generates a "hoverauth" cookie
var _loggedin = false;
// Create request wrapper, enabling json and cookies
var r = request.defaults({
jar: cookies,
json: true
});
/**
* Retrieve list of all domains in account
*
* @param {Function} cb
* @api public
*/
function getAllDomains (cb) {
_hoverRequest('GET', '/domains', cb);
}
/**
* Retrieve list of all dns records in account
*
* @param {Function} cb
* @api public
*/
function getAllDns (cb) {
_hoverRequest('GET', '/dns', cb);
}
/**
* Retrieve individual domain in account
*
* @param {String} domain Domain identifier
* @param {Function} cb
* @api public
*/
function getDomain (domain, cb) {
_hoverRequest('GET', '/domains/' + domain, cb);
}
/**
* Retrieve list of all dns records for particular domain
*
* @param {String} domain Domain identifier
* @param {Function} cb
* @api public
*/
function getDomainDns (domain, cb) {
_hoverRequest('GET', '/domains/' + domain + '/dns', cb);
}
/**
* Create a new A record under the specified domain
*
* @param {String} domain Domain identifier
* @param {String} subdomain Subdomain of record
* @param {String} ip IP Address of record
* @param {Function} cb
* @api public
*/
function createARecord (domain, subdomain, ip, cb) {
var body = {
name: subdomain,
type: 'A',
content: ip
};
_hoverRequest('POST', '/domains/' + domain + '/dns', body, cb);
}
/**
* Create a new MX record under the specified domain
*
* @param {String} domain Domain identifier
* @param {String} subdomain Subdomain of record
* @param {String} priority Priority of record
* @param {String} ip IP Address of record
* @param {Function} cb
* @api public
*/
function createMXRecord (domain, subdomain, priority, ip, cb) {
var body = {
name: subdomain,
type: 'MX',
content: [priority, ip].join(' ')
};
_hoverRequest('POST', '/domains/' + domain + '/dns', body, cb);
}
/**
* Update an existing domain record
*
* @param {String} dns DNS identifier
* @param {String} ip New IP Address of record
* @param {Function} cb
* @api public
*/
function updateDomainDns (dns, ip, cb) {
var body = {
content: ip
};
_hoverRequest('PUT', '/dns/' + dns, body, cb);
}
/**
* Remove an existing dns record
*
* @param {String} dns DNS identifier
* @param {Function} cb
* @api public
*/
function removeDns (dns, cb) {
_hoverRequest('DELETE', '/dns/' + dns, cb);
}
/**
* Proxy request to hover API. Will issue login request if not
* previously generated.
*
* @param {String} method
* @param {String} path
* @param {Function} cb
* @api private
*/
function _hoverRequest (method, path, body, cb) {
// Check if previously logged in
if (_loggedin) return _hoverApiRequest(method, path, body, cb);
// Issue login request with provided username / password
r({
uri: baseUrl + '/login',
body: 'username=' + username + '&password=' + password,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}, _rCallback(function (err) {
if (err) return cb(err);
// Note logged in / forward request
_loggedin = true;
_hoverApiRequest(method, path, body, cb);
}));
}
/**
* Issue request to hover api.
*
* @param {String} method
* @param {String} path
* @param {Object} [body]
* @param {Function} cb
* @api private
*/
function _hoverApiRequest (method, path, body, cb) {
// Check body provided
if (typeof body === 'function') {
cb = body;
body = null;
}
// Default options
var options = {
method: method,
uri: baseUrl + path
};
// Add body if provided
if (body) options.body = body;
// Issue request
r(options, _rCallback(function (err, data) {
if (err) return cb(err);
// Pull out property name
var key = _.without(_.keys(data), 'succeeded');
cb(null, data[key]);
}));
}
/**
* Request callback abstraction to deliver http or connection error.
*
* @param {Function} cb
* @return {Function}
* @api private
*/
function _rCallback (cb) {
return function (err, res, data) {
if (err) return cb(err);
if (!res || res.statusCode > 400) return cb(data);
cb(null, data);
};
}
// Expose API
return {
getAllDomains: getAllDomains,
getAllDns: getAllDns,
getDomain: getDomain,
getDomainDns: getDomainDns,
createARecord: createARecord,
createMXRecord: createMXRecord,
updateDomainDns: updateDomainDns,
removeDns: removeDns
};
};