Update 'lib/index.js'

'Prettified' code
This commit is contained in:
jarombridges 2019-07-12 01:32:43 +00:00
parent 7223ce6667
commit 8e309ba5d3
1 changed files with 77 additions and 65 deletions

View File

@ -1,55 +1,63 @@
'use strict'; 'use strict';
var request;
var defaults = { var defaults = {
baseUrl: 'https://dns.api.gandi.net/api/v5/' baseUrl: 'https://dns.api.gandi.net/api/v5/'
}; };
module.exports.create = function(config) { module.exports.create = function (config) {
var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, ''); var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, '');
var authtoken = config.token; var authtoken = config.token;
var request; var request;
return { return {
init: function(opts) { init: function (opts) {
request = opts.request; request = opts.request;
return null; return null;
}, },
zones: function(opts) { zones: function (opts) {
console.log(opts); console.log(opts);
return request({ return request({
method: 'GET', method: 'GET',
url: baseUrl + '/zones', url: baseUrl + '/zones',
headers: { 'X-Api-Key': authtoken }, headers: {
'X-Api-Key': authtoken
},
json: true json: true
}).then(function(resp) { }).then(function (resp) {
return resp.body.map(function (zone) { return zone.name; });; return resp.body.map(function (zone) {
return zone.name;
});;
});; });;
}, },
set: function(opts) { set: function (opts) {
console.log(opts); console.log(opts);
return request({ return request({
method: 'GET', method: 'GET',
url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records/' + opts.challenge.dnsPrefix + '/TXT', url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records/' + opts.challenge.dnsPrefix + '/TXT',
headers: { 'X-Api-Key': authtoken}, headers: {
'X-Api-Key': authtoken
},
json: true json: true
}).then(function(resp) { }).then(function (resp) {
if (resp.body.cause === 'Not Found') { if (resp.body.cause === 'Not Found') {
return request({ return request({
method: 'POST', method: 'POST',
url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records', url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records',
headers: { 'X-Api-Key': authtoken }, headers: {
json: {'rrset_name': opts.challenge.dnsPrefix, 'X-Api-Key': authtoken
},
json: {
'rrset_name': opts.challenge.dnsPrefix,
'rrset_type': 'TXT', 'rrset_type': 'TXT',
'rrset_ttl': 300, 'rrset_ttl': 300,
'rrset_values': [opts.challenge.dnsAuthorization] } 'rrset_values': [opts.challenge.dnsAuthorization]
})
} }
else { })
} else {
const body = resp.body const body = resp.body
let value = body.rrset_values.map(x => JSON.parse(x)) let value = body.rrset_values.map(x => JSON.parse(x))
@ -57,53 +65,57 @@ module.exports.create = function(config) {
return request({ return request({
method: 'PUT', method: 'PUT',
url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records/' + opts.challenge.dnsPrefix + '/TXT', url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records/' + opts.challenge.dnsPrefix + '/TXT',
headers: { 'X-Api-Key': authtoken}, headers: {
json: {'rrset_ttl': 300, 'X-Api-Key': authtoken
'rrset_values': value.concat([opts.challenge.dnsAuthorization] ) },
json: {
'rrset_ttl': 300,
'rrset_values': value.concat([opts.challenge.dnsAuthorization])
} }
}) })
} }
} }
}) });;
;;
}, },
remove: function(opts) { remove: function (opts) {
console.log(opts); console.log(opts);
return request({ return request({
method: 'DELETE', method: 'DELETE',
url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records/' + opts.challenge.dnsPrefix + '/TXT', url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records/' + opts.challenge.dnsPrefix + '/TXT',
headers: { 'X-Api-Key': authtoken }, headers: {
'X-Api-Key': authtoken
},
json: true json: true
}) })
} },
,
get: function(opts) { get: function (opts) {
console.log(opts); console.log(opts);
return request({ return request({
method: 'GET', method: 'GET',
url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records/' + opts.challenge.dnsPrefix, url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records/' + opts.challenge.dnsPrefix,
headers: { 'X-Api-Key': authtoken}, headers: {
'X-Api-Key': authtoken
},
json: true json: true
}).then(function(resp) { }).then(function (resp) {
const body = resp.body const body = resp.body
if (body.length > 0) { if (body.length > 0) {
let value = body[0].rrset_values.map(x => JSON.parse(x)).filter(field => field === opts.challenge.dnsAuthorization) let value = body[0].rrset_values.map(x => JSON.parse(x)).filter(field => field === opts.challenge.dnsAuthorization)
if (value !== []) { if (value !== []) {
return { dnsAuthorization: value[0]} return {
dnsAuthorization: value[0]
} }
else { } else {
return null return null
} }
} } else {
else {
return null return null
} }
}) })
} }
} }
}; };