diff --git a/AUTHORS b/AUTHORS index f2496e6..4148889 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1 +1,2 @@ AJ ONeal (https://coolaj86.com/) +Aneem Patrabansha (https://aneem.com.np) diff --git a/lib/index.js b/lib/index.js index 00b99bd..d3a9022 100644 --- a/lib/index.js +++ b/lib/index.js @@ -12,20 +12,23 @@ module.exports.create = function(config) { var baseUrl = config.baseUrl || defaults.baseUrl; var authtoken = config.token; + function api(method, path, form) { + return request({ + method: method, + url: baseUrl + path, + headers: { + Authorization: 'Bearer ' + authtoken, + 'Content-Type': 'application/json' + }, + json: true, + form: form + }); + } + var helpers = { getZonenames: function(/*opts*/) { // { dnsHosts: [ xxxx.foo.example.com ] } - var url = baseUrl + '/v2/domains/'; - - return request({ - method: 'GET', - url: url, - headers: { - Authorization: 'Bearer ' + authtoken, - 'Content-Type': 'application/json' - }, - json: true - }).then(function(resp) { + return api('GET', '/v2/domains/').then(function(resp) { return resp.body.domains.map(function(x) { return x.name; }); @@ -35,20 +38,13 @@ module.exports.create = function(config) { // data:{dnsPrefix:"_88-acme-challenge-0e.foo",zone:"example.com",txt:"_cdZWaclIbkP1qYpMkZIURTK--ydQIK6d9axFmftWz0"} var dnsPrefix = data.dnsPrefix; var txt = data.txt; - var url = baseUrl + '/v2/domains/' + data.zone + '/records'; // Digital ocean provides the api to fetch records by ID. Since we do not have id, we fetch all the records, // filter the required TXT record - return request({ - method: 'GET', - url: url, - json: true, - headers: { - Authorization: 'Bearer ' + authtoken, - 'Content-Type': 'application/json' - } - }).then(function(resp) { + return api('GET', '/v2/domains/' + data.zone + '/records').then(function( + resp + ) { resp = resp.body; var entries = resp && @@ -69,25 +65,13 @@ module.exports.create = function(config) { set: function(data) { var ch = data.challenge; var txt = ch.dnsAuthorization; - var url = baseUrl + '/v2/domains/' + ch.dnsZone + '/records'; // console.info('Adding TXT', data); - return request({ - method: 'POST', - url: url, - headers: { - Authorization: 'Bearer ' + authtoken, - 'Content-Type': 'application/json' - }, - json: { - type: 'TXT', - // Note: dnsPrefix != dnsHost.split('.')[0] - // _greenlock-dryrun-2277.bar.foo.example.com => _greenlock-dryrun-2277.bar.foo - name: ch.dnsPrefix, - data: txt, - // Note: set a LOW ttl so that responses are not cached so long - ttl: 300 - } + return api('POST', '/v2/domains/' + ch.dnsZone + '/records', { + type: 'TXT', + name: ch.dnsPrefix, + data: txt, + ttl: 300 }).then(function(resp) { resp = resp.body; if (resp && resp.domain_record && resp.domain_record.data === txt) { @@ -112,14 +96,10 @@ module.exports.create = function(config) { var url = baseUrl + '/v2/domains/' + ch.dnsZone + '/records/' + txtRecord.id; - return request({ - method: 'DELETE', - url: url, - headers: { - Authorization: 'Bearer ' + authtoken, - 'Content-Type': 'application/json' - } - }).then(function(resp) { + return api( + 'DELETE', + '/v2/domains/' + ch.dnsZone + '/records/' + txtRecord.id + ).then(function(resp) { resp = resp.body; return true; });