From 9071b0c048645f859cb8bb470df2d5f1c50b4efd Mon Sep 17 00:00:00 2001 From: Hitesh Date: Mon, 22 Jul 2019 22:45:32 -0700 Subject: [PATCH] bug fixes --- lib/index.js | 47 +++++++++++++++++++++++++++++++---------------- test.js | 2 +- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/lib/index.js b/lib/index.js index 798a005..c508854 100644 --- a/lib/index.js +++ b/lib/index.js @@ -11,7 +11,7 @@ const PRODUCTION_URL = 'https://api.namecheap.com/xml.response'; var defaults = { - baseUrl: PRODUCTION_URL + baseUrl: SANDBOX_URL }; function extend(obj) { @@ -53,7 +53,9 @@ module.exports.create = function (config) { assign(requestParams,params); var url = requestUrl(baseUrl, requestParams); - // console.log(url); + console.log('DEBUG >>> url: ' + url); + console.log('DEBUG >>> requestParams: ' + JSON.stringify(requestParams, null, 2)); + return request({ method: 'POST', url: url, @@ -64,7 +66,7 @@ module.exports.create = function (config) { // check response status if (result['ApiResponse']['$']['Status'] === 'ERROR') { for (let i = 0; i < result['ApiResponse']['Errors'].length; i++) { - console.log(result['ApiResponse']['Errors'][i]) + console.log('DEBUG >>> error: ' + JSON.stringify(result['ApiResponse']['Errors'][i]['Error'][0], null, 2)); } throw new Error('API Error'); } else { // Status="OK" @@ -92,15 +94,29 @@ module.exports.create = function (config) { }, set: function (data) { + console.log(`DEBUG >>> data: ${JSON.stringify(data, null, 2)}`); var ch = data.challenge; var txt = ch.dnsAuthorization; var params = {}; - var zone = ch.dnsZone; + // var zone = ch.dnsZone; + var zone = ch.identifier.value; + console.log(`DEBUG >>> zone: ${zone}`); + // the domain is the first part - params['SLD'] = zone.split('.')[0]; + // params['SLD'] = zone.split('.')[0]; // the rest of the components are the TLD - params['TLD'] = zone.split('.').splice(1).join('.'); + // params['TLD'] = zone.split('.').splice(1).join('.'); + + var domains = zone.split('.'); + console.log('DEBUG >>> ' + domains); + + // if you have subdomain foo.blah.com, SLD = blah and TLD = com + params['TLD'] = domains[domains.length - 1]; + params['SLD'] = domains[domains.length - 2]; + + console.log(`DEBUG >>> SLD: ${params['SLD']}`); + console.log(`DEBUG >>> TLD: ${params['TLD']}`); // setting a host record overwrites all existing, // adding a new records means you've have to send back all previous records too @@ -139,11 +155,11 @@ module.exports.create = function (config) { var ch = data.challenge; var params = {}; - var zone = ch.dnsZone; - // the domain is the first part - params['SLD'] = zone.split('.')[0]; - // the rest of the components are the TLD - params['TLD'] = zone.split('.').splice(1).join('.'); + var zone = ch.identifier.value; + var domains = zone.split('.'); + + params['TLD'] = domains[domains.length - 1]; + params['SLD'] = domains[domains.length - 2]; // setting a host record overwrites all existing, // removing a new records means you've have to send back all previous records without removed @@ -176,12 +192,11 @@ module.exports.create = function (config) { var ch = data.challenge; var params = {}; - var zone = ch.dnsZone; + var zone = ch.identifier.value; + var domains = zone.split('.'); - // the domain is the first part - params['SLD'] = zone.split('.')[0]; - // the rest of the components are the TLD - params['TLD'] = zone.split('.').splice(1).join('.'); + params['TLD'] = domains[domains.length - 1]; + params['SLD'] = domains[domains.length - 2]; return api('namecheap.domains.dns.getHosts',params).then(function (hostsResponse) { // console.log('hosts'); diff --git a/test.js b/test.js index 18e9e18..3ee9c40 100644 --- a/test.js +++ b/test.js @@ -11,7 +11,7 @@ var challenger = require('./index.js').create({ apiUser:process.argv[3], apiKey : process.argv[4], clientIp:process.argv[5], - username: process.argv[6]||process.argv[3] + username: process.argv[6] || process.argv[3] });