From 0aa6f28f85460d1e934ed8bb8fa64ed3edb2d322 Mon Sep 17 00:00:00 2001 From: nyaundi brian Date: Thu, 18 Jul 2019 08:13:31 +0000 Subject: [PATCH] add remove --- lib/index.js | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 794014c..798a005 100644 --- a/lib/index.js +++ b/lib/index.js @@ -136,10 +136,40 @@ module.exports.create = function (config) { }, remove: function (data) { - var domainname = data.challenge.altname; - var zone = domainname; + var ch = data.challenge; - throw new Error('not supported'); + 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('.'); + + // setting a host record overwrites all existing, + // removing a new records means you've have to send back all previous records without removed + + return api('namecheap.domains.dns.getHosts',params).then(function (hostsResponse) { + var currentHostRecordsCount = hostsResponse['DomainDNSGetHostsResult'][0]['host'].length; + + for (var i = 0; i < currentHostRecordsCount; i++) { + // console.log(hostsResponse['DomainDNSGetHostsResult'][i]['host'][0]); + var currentEntry = hostsResponse['DomainDNSGetHostsResult'][0]['host'][i]['$']; + if(currentEntry['Address'] != ch.dnsAuthorization){ + params['HostName'+(i+1)] = currentEntry['Name']; + params['RecordType'+(i+1)] = currentEntry['Type']; + params['Address'+(i+1)] = currentEntry['Address']; + params['TTL'+(i+1)] = currentEntry['TTL']; + } + } + + 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 remove. check subdomain, api key, etc'); + }); + }); }, get: function (data) {