This repository has been archived by the owner on Nov 28, 2017. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdnsimple.js
211 lines (178 loc) · 5.5 KB
/
dnsimple.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
/*
Name: DNSimple module for Node.js
Author: Franklin van de Meent (https://frankl.in)
Source: https://github.com/fvdm/nodejs-dnsimple
Feedback: https://github.com/fvdm/nodejs-dnsimple/issues
License: Unlicense (public domain)
see UNLICENSE file
Service name: DNSimple
Service URL: https://dnsimple.com
Service API: http://developer.dnsimple.com
*/
var http = require ('httpreq');
/**
* Process httpreq response
*
* @callback callback
* @param err {Error, null} - Agent error
* @param response {object} - Response details
* @param request {object} - Request details
* @param callback {function} - `function (err, data) {}`
* @returns {void}
*/
function processResponse (err, response, request, callback) {
var error = null;
var data = response && response.body || '';
var meta = {};
// agent error
if (err) {
error = new Error ('request failed');
error.error = err;
callback (error);
return;
}
// parse response
try {
data = JSON.parse (data);
} catch (e) {
error = new Error ('invalid response');
error.error = e;
}
meta.statusCode = response.statusCode;
meta.request_id = response.headers ['x-request-id'];
meta.runtime = response.headers ['x-runtime'];
if (typeof response.headers ['x-dnsimple-otp-token'] === 'string') {
meta.twoFactorToken = response.headers ['x-dnsimple-otp-token'];
}
// status ok, no data
if (!data && meta.statusCode < 300) {
error = null;
}
// domain check 404 = free
if (request.url.match (/\/domains\/.+\/check$/) && meta.statusCode === 404) {
error = null;
}
// delete ok
if (request.method === 'DELETE' && !(data instanceof Object && Object.keys (data).length > 0)) {
callback (null, meta.statusCode === 200 || meta.statusCode === 204, meta);
return;
}
// check HTTP status code
if (!error && response.statusCode < 300) {
callback (null, data, meta);
return;
} else if (response.statusCode === 401 && response.headers ['x-dnsimple-otp'] === 'required') {
error = new Error ('twoFactorOTP required');
} else {
error = new Error ('API error');
}
error.code = response.statusCode;
error.error = data.message || data.error || data.errors || null;
error.data = data;
callback (error, null, meta);
}
/**
* Send request to API
*
* @param props {object}
* @param props.path {string} - Request path
* @param [props.method] {string} - GET, POST, PUT, DELETE
* @param props.config {object} - See module.exports below
*/
function sendRequest (props) {
var options = {
url: 'https://' + props.config.hostname + '/v1' + props.path,
method: props.method || 'GET',
timeout: props.config.timeout,
headers: {
'Accept': 'application/json',
'User-Agent': 'Nodejs-DNSimple'
}
};
// credentials set?
if (
!(props.config.email && props.config.token)
&& !(props.config.email && props.config.password)
&& !props.config.domainToken
&& !props.config.twoFactorToken
) {
props.callback (new Error ('credentials missing'));
return;
}
// token in headers
if (props.config.token) {
options.headers['X-DNSimple-Token'] = props.config.email + ':' + props.config.token;
}
if (props.config.domainToken) {
options.headers['X-DNSimple-Domain-Token'] = props.config.domainToken;
}
// build request
if (options.method.match (/(POST|PUT|DELETE)/)) {
options.json = props.fields;
} else {
options.parameters = props.fields;
}
// password authentication
if (
!props.config.twoFactorToken
&& !props.config.token
&& !props.config.domainToken
&& props.config.password
&& props.config.email
) {
options.auth = props.config.email + ':' + props.config.password;
// two-factor authentication (2FA)
if (props.config.twoFactorOTP) {
options.headers['X-DNSimple-2FA-Strict'] = 1;
options.headers['X-DNSimple-OTP'] = props.config.twoFactorOTP;
}
}
if (props.config.twoFactorToken) {
options.auth = props.config.twoFactorToken + ':x-2fa-basic';
options.headers['X-DNSimple-2FA-Strict'] = 1;
}
// start request
http.doRequest (options, function (err, res) {
processResponse (err, res, options, props.callback);
});
}
/**
* Module configuration and interface
*
* @param config {object} - Configuration, see README.md
* @param [config.hostname = api.dnsimple.com] {string} - API hostname
* @param [config.email] {string} - Account email
* @param [config.token] {string} - Account token
* @param [config.password] {string} - Account password
* @param [config.domainToken] {string} - Domain token
* @param [config.twoFactorTokeb] {string} - TFA token
* @param [config.twoFactorOTP] {string} - TFA one-time code
* @param [config.timeout = 30000] {string} - Request timeout in ms
* @returns {function} - `( method, path, [fields], callback )`
*/
module.exports = function (config) {
var api = {
hostname: config.hostname || 'api.dnsimple.com',
email: config.email || null,
token: config.token || null,
domainToken: config.domainToken || null,
twoFactorOTP: config.twoFactorOTP || null,
twoFactorToken: config.twoFactorToken || null,
password: config.password || null,
timeout: config.timeout || 30000
};
// interface
return function (method, path, fields, callback) {
if (typeof fields === 'function') {
callback = fields;
fields = null;
}
sendRequest ({
method: method,
path: path,
fields: fields,
callback: callback,
config: api
});
};
};