diff --git a/lib/index.js b/lib/index.js index 83cec0e..794014c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -59,7 +59,7 @@ module.exports.create = function (config) { url: url, }).then(function (response) { var responseBody = response.body; - console.log(responseBody); + // console.log(responseBody); return parseString(responseBody).then(function (result) { // check response status if (result['ApiResponse']['$']['Status'] === 'ERROR') { @@ -84,7 +84,6 @@ module.exports.create = function (config) { return api('namecheap.domains.getList',{}).then(function (zonesResponse) { // console.log('zones'); // console.log(zonesResponse); - return zonesResponse['DomainGetListResult'].map(function (x) { return x['Domain'][0]['$']['Name']; }); @@ -103,18 +102,36 @@ module.exports.create = function (config) { // the rest of the components are the TLD params['TLD'] = zone.split('.').splice(1).join('.'); - params['HostName1'] = ch.dnsPrefix; - params['RecordType1'] = 'TXT'; - params['Address1'] = txt; - params['TTL1'] = 1; // in minutes + // setting a host record overwrites all existing, + // adding a new records means you've have to send back all previous records too - return api('namecheap.domains.dns.setHosts',params).then(function (setHostResponse) { - // console.log('setHost'); - // console.log(setHostResponse); - return true + return api('namecheap.domains.dns.getHosts',params).then(function (hostsResponse) { + var currentHostRecordsCount = hostsResponse['DomainDNSGetHostsResult'][0]['host'].length; - }).catch(function (err) { - throw new Error('record did not set. check subdomain, api key, etc'); + for (var i = 0; i < currentHostRecordsCount; i++) { + // console.log(hostsResponse['DomainDNSGetHostsResult'][i]['host'][0]); + var currentEntry = hostsResponse['DomainDNSGetHostsResult'][0]['host'][i]['$']; + + params['HostName'+(i+1)] = currentEntry['Name']; + params['RecordType'+(i+1)] = currentEntry['Type']; + params['Address'+(i+1)] = currentEntry['Address']; + params['TTL'+(i+1)] = currentEntry['TTL']; + } + + params['HostName'+(currentHostRecordsCount+1)] = ch.dnsPrefix; + params['RecordType'+(currentHostRecordsCount+1)] = 'TXT'; + params['Address'+(currentHostRecordsCount+1)] = txt; + params['TTL'+(currentHostRecordsCount+1)] = 100; // in minutes + + // console.log(params); + + return api('namecheap.domains.dns.setHosts',params).then(function (setHostResponse) { + // console.log('setHost'); + // console.log(setHostResponse); + return true + }).catch(function (err) { + throw new Error('record did not set. check subdomain, api key, etc'); + }); }); }, @@ -137,11 +154,11 @@ module.exports.create = function (config) { params['TLD'] = zone.split('.').splice(1).join('.'); return api('namecheap.domains.dns.getHosts',params).then(function (hostsResponse) { - console.log('hosts'); - console.log(hostsResponse); - - var entries = hostsResponse['DomainDNSGetHostsResult'].filter(function (x) { + // console.log('hosts'); + // console.log(hostsResponse); + var currentHostRecords = hostsResponse['DomainDNSGetHostsResult'][0]['host']; + var entries = currentHostRecords.filter(function (x) { return x['$']['Type'] === 'TXT'; });