From 8e309ba5d3465beecda0391e0f64686862befd66 Mon Sep 17 00:00:00 2001 From: jarombridges Date: Fri, 12 Jul 2019 01:32:43 +0000 Subject: [PATCH] Update 'lib/index.js' 'Prettified' code --- lib/index.js | 142 ++++++++++++++++++++++++++++----------------------- 1 file changed, 77 insertions(+), 65 deletions(-) diff --git a/lib/index.js b/lib/index.js index 7fdadd7..4e61b42 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,109 +1,121 @@ 'use strict'; -var request; var defaults = { - baseUrl: 'https://dns.api.gandi.net/api/v5/' + baseUrl: 'https://dns.api.gandi.net/api/v5/' }; -module.exports.create = function(config) { - var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, ''); +module.exports.create = function (config) { + var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, ''); var authtoken = config.token; var request; - + return { - init: function(opts) { + init: function (opts) { request = opts.request; return null; }, - zones: function(opts) { + zones: function (opts) { console.log(opts); - - return request({ - method: 'GET', - url: baseUrl + '/zones', - headers: { 'X-Api-Key': authtoken }, - json: true - }).then(function(resp) { - return resp.body.map(function (zone) { return zone.name; });; - });; - }, - set: function(opts) { + return request({ + method: 'GET', + url: baseUrl + '/zones', + headers: { + 'X-Api-Key': authtoken + }, + json: true + }).then(function (resp) { + return resp.body.map(function (zone) { + return zone.name; + });; + });; + }, + + set: function (opts) { console.log(opts); - + return request({ method: 'GET', url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records/' + opts.challenge.dnsPrefix + '/TXT', - headers: { 'X-Api-Key': authtoken}, + headers: { + 'X-Api-Key': authtoken + }, json: true - }).then(function(resp) { - if (resp.body.cause === 'Not Found') { + }).then(function (resp) { + if (resp.body.cause === 'Not Found') { return request({ - method: 'POST', - url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records', - headers: { 'X-Api-Key': authtoken }, - json: {'rrset_name': opts.challenge.dnsPrefix, - 'rrset_type': 'TXT', - 'rrset_ttl': 300, - 'rrset_values': [opts.challenge.dnsAuthorization] } + method: 'POST', + url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records', + headers: { + 'X-Api-Key': authtoken + }, + json: { + 'rrset_name': opts.challenge.dnsPrefix, + 'rrset_type': 'TXT', + 'rrset_ttl': 300, + 'rrset_values': [opts.challenge.dnsAuthorization] + } }) - } - else { - const body = resp.body - let value = body.rrset_values.map(x => JSON.parse(x)) - - if (body.rrset_values) { - return request({ - method: 'PUT', - url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records/' + opts.challenge.dnsPrefix + '/TXT', - headers: { 'X-Api-Key': authtoken}, - json: {'rrset_ttl': 300, - 'rrset_values': value.concat([opts.challenge.dnsAuthorization] ) - } - }) - } - } - }) - ;; + } else { + const body = resp.body + let value = body.rrset_values.map(x => JSON.parse(x)) + + if (body.rrset_values) { + return request({ + method: 'PUT', + url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records/' + opts.challenge.dnsPrefix + '/TXT', + headers: { + 'X-Api-Key': authtoken + }, + json: { + 'rrset_ttl': 300, + 'rrset_values': value.concat([opts.challenge.dnsAuthorization]) + } + }) + } + } + });; }, - remove: function(opts) { + remove: function (opts) { console.log(opts); - + return request({ method: 'DELETE', url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records/' + opts.challenge.dnsPrefix + '/TXT', - headers: { 'X-Api-Key': authtoken }, + headers: { + 'X-Api-Key': authtoken + }, json: true - }) - } - , + }) + }, - get: function(opts) { + get: function (opts) { console.log(opts); - + return request({ method: 'GET', url: baseUrl + '/domains/' + opts.challenge.dnsZone + '/records/' + opts.challenge.dnsPrefix, - headers: { 'X-Api-Key': authtoken}, + headers: { + 'X-Api-Key': authtoken + }, json: true - }).then(function(resp) { + }).then(function (resp) { const body = resp.body 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 !== []) { - return { dnsAuthorization: value[0]} - } - else { + return { + dnsAuthorization: value[0] + } + } else { return null } - } - else { + } else { return null } }) } } - }; - +}; \ No newline at end of file