make prettier
This commit is contained in:
parent
d93093fe00
commit
0b387a150f
111
lib/index.js
111
lib/index.js
|
@ -5,11 +5,9 @@ var request; // = require('@root/request');
|
|||
var parseString = require('xml2js').parseString;
|
||||
parseString = util.promisify(parseString);
|
||||
|
||||
|
||||
const SANDBOX_URL = 'https://api.sandbox.namecheap.com/xml.response';
|
||||
const PRODUCTION_URL = 'https://api.namecheap.com/xml.response';
|
||||
|
||||
|
||||
var defaults = {
|
||||
baseUrl: SANDBOX_URL
|
||||
};
|
||||
|
@ -25,13 +23,19 @@ function extend(obj) {
|
|||
}
|
||||
|
||||
function assign(obj1, obj2) {
|
||||
for (var attrname in obj2) { obj1[attrname] = obj2[attrname]; }
|
||||
for (var attrname in obj2) {
|
||||
obj1[attrname] = obj2[attrname];
|
||||
}
|
||||
}
|
||||
|
||||
function requestUrl(baseUrl, params) {
|
||||
var queryString = Object.keys(params).map(function (key) {
|
||||
return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]);
|
||||
}).join('&');
|
||||
var queryString = Object.keys(params)
|
||||
.map(function(key) {
|
||||
return (
|
||||
encodeURIComponent(key) + '=' + encodeURIComponent(params[key])
|
||||
);
|
||||
})
|
||||
.join('&');
|
||||
// console.debug(queryString);
|
||||
return baseUrl + '?' + queryString;
|
||||
}
|
||||
|
@ -54,23 +58,39 @@ module.exports.create = function (config) {
|
|||
|
||||
var url = requestUrl(baseUrl, requestParams);
|
||||
console.log('DEBUG >>> url: ' + url);
|
||||
console.log('DEBUG >>> requestParams: ' + JSON.stringify(requestParams, null, 2));
|
||||
console.log(
|
||||
'DEBUG >>> requestParams: ' + JSON.stringify(requestParams, null, 2)
|
||||
);
|
||||
|
||||
return request({
|
||||
method: 'POST',
|
||||
url: url,
|
||||
url: url
|
||||
}).then(function(response) {
|
||||
var responseBody = response.body;
|
||||
// console.log(responseBody);
|
||||
return parseString(responseBody).then(function(result) {
|
||||
// check response status
|
||||
if (result['ApiResponse']['$']['Status'] === 'ERROR') {
|
||||
for (let i = 0; i < result['ApiResponse']['Errors'].length; i++) {
|
||||
console.log('DEBUG >>> error: ' + JSON.stringify(result['ApiResponse']['Errors'][i]['Error'][0], null, 2));
|
||||
for (
|
||||
let i = 0;
|
||||
i < result['ApiResponse']['Errors'].length;
|
||||
i++
|
||||
) {
|
||||
console.log(
|
||||
'DEBUG >>> error: ' +
|
||||
JSON.stringify(
|
||||
result['ApiResponse']['Errors'][i][
|
||||
'Error'
|
||||
][0],
|
||||
null,
|
||||
2
|
||||
)
|
||||
);
|
||||
}
|
||||
throw new Error('API Error');
|
||||
} else { // Status="OK"
|
||||
return result['ApiResponse']['CommandResponse'][0]
|
||||
} else {
|
||||
// Status="OK"
|
||||
return result['ApiResponse']['CommandResponse'][0];
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -83,13 +103,14 @@ module.exports.create = function (config) {
|
|||
},
|
||||
|
||||
zones: function(data) {
|
||||
return api('namecheap.domains.getList',{}).then(function (zonesResponse) {
|
||||
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'];
|
||||
});
|
||||
|
||||
});
|
||||
},
|
||||
|
||||
|
@ -121,12 +142,18 @@ module.exports.create = function (config) {
|
|||
// 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.getHosts',params).then(function (hostsResponse) {
|
||||
var currentHostRecordsCount = hostsResponse['DomainDNSGetHostsResult'][0]['host'].length;
|
||||
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]['$'];
|
||||
var currentEntry =
|
||||
hostsResponse['DomainDNSGetHostsResult'][0]['host'][i][
|
||||
'$'
|
||||
];
|
||||
|
||||
params['HostName' + (i + 1)] = currentEntry['Name'];
|
||||
params['RecordType' + (i + 1)] = currentEntry['Type'];
|
||||
|
@ -134,22 +161,26 @@ module.exports.create = function (config) {
|
|||
params['TTL' + (i + 1)] = currentEntry['TTL'];
|
||||
}
|
||||
|
||||
params['HostName'+(currentHostRecordsCount+1)] = ch.dnsPrefix;
|
||||
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) {
|
||||
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');
|
||||
return true;
|
||||
})
|
||||
.catch(function(err) {
|
||||
throw new Error(
|
||||
'record did not set. check subdomain, api key, etc'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
},
|
||||
remove: function(data) {
|
||||
var ch = data.challenge;
|
||||
|
@ -164,12 +195,18 @@ module.exports.create = function (config) {
|
|||
// 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;
|
||||
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]['$'];
|
||||
var currentEntry =
|
||||
hostsResponse['DomainDNSGetHostsResult'][0]['host'][i][
|
||||
'$'
|
||||
];
|
||||
if (currentEntry['Address'] != ch.dnsAuthorization) {
|
||||
params['HostName' + (i + 1)] = currentEntry['Name'];
|
||||
params['RecordType' + (i + 1)] = currentEntry['Type'];
|
||||
|
@ -178,15 +215,18 @@ module.exports.create = function (config) {
|
|||
}
|
||||
}
|
||||
|
||||
return api('namecheap.domains.dns.setHosts',params).then(function (setHostResponse) {
|
||||
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');
|
||||
return true;
|
||||
})
|
||||
.catch(function(err) {
|
||||
throw new Error(
|
||||
'record did not remove. check subdomain, api key, etc'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
},
|
||||
get: function(data) {
|
||||
var ch = data.challenge;
|
||||
|
@ -198,10 +238,13 @@ module.exports.create = function (config) {
|
|||
params['TLD'] = domains[domains.length - 1];
|
||||
params['SLD'] = domains[domains.length - 2];
|
||||
|
||||
return api('namecheap.domains.dns.getHosts',params).then(function (hostsResponse) {
|
||||
return api('namecheap.domains.dns.getHosts', params).then(function(
|
||||
hostsResponse
|
||||
) {
|
||||
// console.log('hosts');
|
||||
// console.log(hostsResponse);
|
||||
var currentHostRecords = hostsResponse['DomainDNSGetHostsResult'][0]['host'];
|
||||
var currentHostRecords =
|
||||
hostsResponse['DomainDNSGetHostsResult'][0]['host'];
|
||||
|
||||
var entries = currentHostRecords.filter(function(x) {
|
||||
return x['$']['Type'] === 'TXT';
|
||||
|
@ -218,9 +261,7 @@ module.exports.create = function (config) {
|
|||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
};
|
||||
};
|
||||
|
|
|
@ -26,15 +26,6 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"acme-dns-01-test": {
|
||||
"version": "3.3.1",
|
||||
"resolved": "https://registry.npmjs.org/acme-dns-01-test/-/acme-dns-01-test-3.3.1.tgz",
|
||||
"integrity": "sha512-di2/n19FDLc/pe4CDxd/FpxuuCZG7CHEQVjWr96vvtxe5XNNgdHi2eJqVP0z9WBf9s61zxslyRPrAWzTN8ZVWw==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"acme-challenge-test": "^3.3.1"
|
||||
}
|
||||
},
|
||||
"sax": {
|
||||
"version": "1.2.4",
|
||||
"resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz",
|
||||
|
|
|
@ -3,6 +3,10 @@
|
|||
"version": "3.0.0",
|
||||
"description": "Namecheap DNS for Let's Encrypt / ACME dns-01 challenges with ACME.js and Greenlock.js",
|
||||
"main": "index.js",
|
||||
"files": [
|
||||
"lib",
|
||||
"test.js"
|
||||
],
|
||||
"scripts": {
|
||||
"test": "node ./test.js"
|
||||
},
|
||||
|
@ -30,7 +34,6 @@
|
|||
"xml2js": "^0.4.19"
|
||||
},
|
||||
"devDependencies": {
|
||||
"acme-challenge-test": "^3.3.2",
|
||||
"acme-dns-01-test": "^3.2.1"
|
||||
"acme-challenge-test": "^3.3.2"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue