From 2bdf6ffc71262bb62d161baf04365f8ec0c35860 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 30 Jul 2019 20:00:06 -0600 Subject: [PATCH] mostly stylistic changes --- lib/index.js | 124 ++++++++++++++++++++++++--------------------------- 1 file changed, 58 insertions(+), 66 deletions(-) diff --git a/lib/index.js b/lib/index.js index dee6246..3f45af4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -11,22 +11,12 @@ var defaults = { }; module.exports.create = function(config) { - var baseUrl = config.baseUrl || defaults.baseUrl; + var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, ''); var apiKey = config.key || config.apiKey; var apiSecret = config.secret || config.apiSecret; var auth = 'sso-key ' + apiKey + ':' + apiSecret; - - function api(method, path, form) { - return request({ - method: method, - url: baseUrl + path, - headers: { - Authorization: auth - }, - json: form || true - }); - } + var redacted = 'sso-key ' + apiKey + ':[redacted]'; return { init: function(deps) { @@ -35,13 +25,7 @@ module.exports.create = function(config) { }, zones: function(data) { - return api('GET', 'domains?statuses=ACTIVE').then(function(resp) { - if (200 !== resp.statusCode) { - console.error(resp.statusCode); - console.error(resp.body); - throw new Error('Could not get list of zones. Check api key, etc'); - } - + return api('GET', '/domains?statuses=ACTIVE').then(function(resp) { return resp.body.map(function(x) { return x.domain; }); @@ -67,17 +51,9 @@ module.exports.create = function(config) { } ]; - return api('PATCH', 'domains/' + ch.dnsZone + '/records', records).then( + return api('PATCH', '/domains/' + ch.dnsZone + '/records', records).then( function(resp) { - if (200 !== resp.statusCode) { - console.error(resp.statusCode); - console.error(resp.body); - throw new Error( - 'record did not set. check subdomain, api key, etc' - ); - } - - return true; + return null; } ); }, @@ -87,25 +63,15 @@ module.exports.create = function(config) { // get all domain records of type or name return api( 'GET', - 'domains/' + ch.dnsZone + '/records/TXT/' + ch.dnsPrefix + '/domains/' + ch.dnsZone + '/records/TXT/' + ch.dnsPrefix ) .then(function(resp) { - if (200 !== resp.statusCode) { - console.error(resp.statusCode); - console.error(resp.body); - throw new Error('Could not get list of zones. Check api key, etc'); - } - - var newEntries = []; - // filter all TXT records without record to remove - for (let i = 0; i < resp.body.length; i++) { - if (resp.body[i]['data'] !== ch.dnsAuthorization) { - newEntries.push(resp.body[i]); - } - } - return newEntries; + // keep all TXT records that we don't need to remove + return resp.body.filter(function(el) { + return el.data !== ch.dnsAuthorization; + }); }) - .then(function(newRecords) { + .then(function(records) { // godaddy doesn't provide an endpoint for a single record removal // but provides this endpoint to replace all records of a given type // https://developer.godaddy.com/doc/endpoint/domains#/v1/recordReplaceType @@ -113,8 +79,8 @@ module.exports.create = function(config) { // hence when only a single record of type exists and is the one to remove // we call the endpoint with this dummy record - if (!newRecords.length) { - newRecords.push({ + if (!records.length) { + records.push({ data: 'free_to_delete', name: 'remove_placeholder', ttl: 600 @@ -124,17 +90,10 @@ module.exports.create = function(config) { // update - overwrite all type and name records under domain return api( 'PUT', - 'domains/' + ch.dnsZone + '/records/TXT', - newRecords + '/domains/' + ch.dnsZone + '/records/TXT', + records ).then(function(resp) { - if (200 !== resp.statusCode) { - console.error(resp.statusCode); - console.error(resp.body); - throw new Error( - 'record did not remove. check subdomain, api key, etc' - ); - } - return true; + return null; }); }); }, @@ -142,20 +101,53 @@ module.exports.create = function(config) { var ch = data.challenge; return api( 'GET', - 'domains/' + ch.dnsZone + '/records/TXT/' + ch.dnsPrefix + '/domains/' + ch.dnsZone + '/records/TXT/' + ch.dnsPrefix ).then(function(resp) { - resp = resp.body; + var entry = (resp.body || []).filter(function(x) { + return x.data === ch.dnsAuthorization; + })[0]; - var entry = null; - - if (resp.length) { - entry = resp.filter(function(x) { - return x.data === ch.dnsAuthorization; - })[0]; + if (entry) { + return { dnsAuthorization: entry.data }; } - return entry ? { dnsAuthorization: entry.data } : null; + return null; }); } }; + + function api(method, path, data) { + var req = { + method: method, + url: baseUrl + path, + headers: { Authorization: auth }, + json: data || true + }; + return request(req).then(function(resp) { + if (resp.statusCode < 200 || resp.statusCode >= 300) { + req.headers.Authorization = redacted; + console.error(); + console.error(req.method + ' ' + req.url); + console.error(req.headers); + if (data) { + console.error(data); + } + console.error(); + console.error(resp.statusCode); + console.error(resp.body); + console.error(); + throw new Error( + 'Request "' + + req.method + + ' ' + + req.url + + '" failed: ' + + resp.statusCode + + ' ' + + JSON.stringify(resp.body) + + '. Check subdomain, api key, etc' + ); + } + }); + } };