make prettier
This commit is contained in:
		
							parent
							
								
									a6a7f6b95e
								
							
						
					
					
						commit
						ff8c41b994
					
				
							
								
								
									
										163
									
								
								lib/index.js
									
									
									
									
									
								
							
							
						
						
									
										163
									
								
								lib/index.js
									
									
									
									
									
								
							| @ -5,11 +5,9 @@ var request; // = require('@root/request'); | |||||||
| var parseString = require('xml2js').parseString; | var parseString = require('xml2js').parseString; | ||||||
| parseString = util.promisify(parseString); | parseString = util.promisify(parseString); | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| const SANDBOX_URL = 'https://api.sandbox.namecheap.com/xml.response'; | const SANDBOX_URL = 'https://api.sandbox.namecheap.com/xml.response'; | ||||||
| const PRODUCTION_URL = 'https://api.namecheap.com/xml.response'; | const PRODUCTION_URL = 'https://api.namecheap.com/xml.response'; | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| var defaults = { | var defaults = { | ||||||
| 	baseUrl: PRODUCTION_URL | 	baseUrl: PRODUCTION_URL | ||||||
| }; | }; | ||||||
| @ -24,19 +22,25 @@ function extend(obj) { | |||||||
| 	return newObj; | 	return newObj; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function assign(obj1,obj2) { | 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) { | function requestUrl(baseUrl, params) { | ||||||
|     var queryString = Object.keys(params).map(function (key) { | 	var queryString = Object.keys(params) | ||||||
|         return encodeURIComponent(key) + '=' + encodeURIComponent(params[key]); | 		.map(function(key) { | ||||||
|     }).join('&'); | 			return ( | ||||||
|  | 				encodeURIComponent(key) + '=' + encodeURIComponent(params[key]) | ||||||
|  | 			); | ||||||
|  | 		}) | ||||||
|  | 		.join('&'); | ||||||
| 	// console.debug(queryString);
 | 	// console.debug(queryString);
 | ||||||
| 	return baseUrl + '?' + queryString; | 	return baseUrl + '?' + queryString; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| module.exports.create = function (config) { | module.exports.create = function(config) { | ||||||
| 	// config = { baseUrl, token }
 | 	// config = { baseUrl, token }
 | ||||||
| 	var baseUrl = config.baseUrl || defaults.baseUrl; | 	var baseUrl = config.baseUrl || defaults.baseUrl; | ||||||
| 
 | 
 | ||||||
| @ -50,48 +54,54 @@ module.exports.create = function (config) { | |||||||
| 	function api(command, params) { | 	function api(command, params) { | ||||||
| 		var requestParams = extend(globalParams); | 		var requestParams = extend(globalParams); | ||||||
| 		requestParams['Command'] = command; | 		requestParams['Command'] = command; | ||||||
|         assign(requestParams,params); | 		assign(requestParams, params); | ||||||
| 
 | 
 | ||||||
| 		var url = requestUrl(baseUrl, requestParams); | 		var url = requestUrl(baseUrl, requestParams); | ||||||
| 		// console.log(url);
 | 		// console.log(url);
 | ||||||
| 		return request({ | 		return request({ | ||||||
| 			method: 'POST', | 			method: 'POST', | ||||||
|             url: url, | 			url: url | ||||||
|         }).then(function (response) { | 		}).then(function(response) { | ||||||
| 			var responseBody = response.body; | 			var responseBody = response.body; | ||||||
| 			// console.log(responseBody);
 | 			// console.log(responseBody);
 | ||||||
|             return parseString(responseBody).then(function (result) { | 			return parseString(responseBody).then(function(result) { | ||||||
| 				// check response status
 | 				// check response status
 | ||||||
| 				if (result['ApiResponse']['$']['Status'] === 'ERROR') { | 				if (result['ApiResponse']['$']['Status'] === 'ERROR') { | ||||||
|                     for (let i = 0; i < result['ApiResponse']['Errors'].length; i++) { | 					for ( | ||||||
|                         console.log(result['ApiResponse']['Errors'][i]) | 						let i = 0; | ||||||
|  | 						i < result['ApiResponse']['Errors'].length; | ||||||
|  | 						i++ | ||||||
|  | 					) { | ||||||
|  | 						console.log(result['ApiResponse']['Errors'][i]); | ||||||
| 					} | 					} | ||||||
| 					throw new Error('API Error'); | 					throw new Error('API Error'); | ||||||
|                 } else { // Status="OK"
 | 				} else { | ||||||
|                     return result['ApiResponse']['CommandResponse'][0] | 					// Status="OK"
 | ||||||
|  | 					return result['ApiResponse']['CommandResponse'][0]; | ||||||
| 				} | 				} | ||||||
| 			}); | 			}); | ||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	return { | 	return { | ||||||
|         init: function (deps) { | 		init: function(deps) { | ||||||
| 			request = deps.request; | 			request = deps.request; | ||||||
| 			return null; | 			return null; | ||||||
| 		}, | 		}, | ||||||
| 
 | 
 | ||||||
| 		zones: function(data) { | 		zones: function(data) { | ||||||
|             return api('namecheap.domains.getList',{}).then(function (zonesResponse) { | 			return api('namecheap.domains.getList', {}).then(function( | ||||||
|  | 				zonesResponse | ||||||
|  | 			) { | ||||||
| 				// console.log('zones');
 | 				// console.log('zones');
 | ||||||
| 				// console.log(zonesResponse);
 | 				// console.log(zonesResponse);
 | ||||||
|                 return zonesResponse['DomainGetListResult'].map(function (x) { | 				return zonesResponse['DomainGetListResult'].map(function(x) { | ||||||
| 					return x['Domain'][0]['$']['Name']; | 					return x['Domain'][0]['$']['Name']; | ||||||
| 				}); | 				}); | ||||||
| 
 |  | ||||||
| 			}); | 			}); | ||||||
| 		}, | 		}, | ||||||
| 
 | 
 | ||||||
|         set: function (data) { | 		set: function(data) { | ||||||
| 			var ch = data.challenge; | 			var ch = data.challenge; | ||||||
| 			var txt = ch.dnsAuthorization; | 			var txt = ch.dnsAuthorization; | ||||||
| 
 | 
 | ||||||
| @ -100,42 +110,55 @@ module.exports.create = function (config) { | |||||||
| 			// the domain is the first part
 | 			// the domain is the first part
 | ||||||
| 			params['SLD'] = zone.split('.')[0]; | 			params['SLD'] = zone.split('.')[0]; | ||||||
| 			// the rest of the components are the TLD
 | 			// the rest of the components are the TLD
 | ||||||
|             params['TLD'] = zone.split('.').splice(1).join('.'); | 			params['TLD'] = zone | ||||||
|  | 				.split('.') | ||||||
|  | 				.splice(1) | ||||||
|  | 				.join('.'); | ||||||
| 
 | 
 | ||||||
| 			// setting a host record overwrites all existing,
 | 			// setting a host record overwrites all existing,
 | ||||||
| 			// adding a new records means you've have to send back all previous records too
 | 			// 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) { | 			return api('namecheap.domains.dns.getHosts', params).then(function( | ||||||
|                 var currentHostRecordsCount = hostsResponse['DomainDNSGetHostsResult'][0]['host'].length; | 				hostsResponse | ||||||
|  | 			) { | ||||||
|  | 				var currentHostRecordsCount = | ||||||
|  | 					hostsResponse['DomainDNSGetHostsResult'][0]['host'].length; | ||||||
| 
 | 
 | ||||||
| 				for (var i = 0; i < currentHostRecordsCount; i++) { | 				for (var i = 0; i < currentHostRecordsCount; i++) { | ||||||
| 					// console.log(hostsResponse['DomainDNSGetHostsResult'][i]['host'][0]);
 | 					// 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['HostName' + (i + 1)] = currentEntry['Name']; | ||||||
|                     params['RecordType'+(i+1)] = currentEntry['Type']; | 					params['RecordType' + (i + 1)] = currentEntry['Type']; | ||||||
|                     params['Address'+(i+1)] = currentEntry['Address']; | 					params['Address' + (i + 1)] = currentEntry['Address']; | ||||||
|                     params['TTL'+(i+1)] = currentEntry['TTL']; | 					params['TTL' + (i + 1)] = currentEntry['TTL']; | ||||||
| 				} | 				} | ||||||
| 
 | 
 | ||||||
|                 params['HostName'+(currentHostRecordsCount+1)] = ch.dnsPrefix; | 				params['HostName' + (currentHostRecordsCount + 1)] = | ||||||
|                 params['RecordType'+(currentHostRecordsCount+1)] = 'TXT'; | 					ch.dnsPrefix; | ||||||
|                 params['Address'+(currentHostRecordsCount+1)] = txt; | 				params['RecordType' + (currentHostRecordsCount + 1)] = 'TXT'; | ||||||
|                 params['TTL'+(currentHostRecordsCount+1)] = 100; // in minutes
 | 				params['Address' + (currentHostRecordsCount + 1)] = txt; | ||||||
|  | 				params['TTL' + (currentHostRecordsCount + 1)] = 100; // in minutes
 | ||||||
| 
 | 
 | ||||||
| 				// console.log(params);
 | 				// 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('setHost');
 | ||||||
| 						// console.log(setHostResponse);
 | 						// console.log(setHostResponse);
 | ||||||
|                     return true | 						return true; | ||||||
|                 }).catch(function (err) { | 					}) | ||||||
|                     throw new Error('record did not set. check subdomain, api key, etc'); | 					.catch(function(err) { | ||||||
|  | 						throw new Error( | ||||||
|  | 							'record did not set. check subdomain, api key, etc' | ||||||
|  | 						); | ||||||
| 					}); | 					}); | ||||||
| 			}); | 			}); | ||||||
| 
 |  | ||||||
| 		}, | 		}, | ||||||
|         remove: function (data) { | 		remove: function(data) { | ||||||
| 			var ch = data.challenge; | 			var ch = data.challenge; | ||||||
| 
 | 
 | ||||||
| 			var params = {}; | 			var params = {}; | ||||||
| @ -143,36 +166,48 @@ module.exports.create = function (config) { | |||||||
| 			// the domain is the first part
 | 			// the domain is the first part
 | ||||||
| 			params['SLD'] = zone.split('.')[0]; | 			params['SLD'] = zone.split('.')[0]; | ||||||
| 			// the rest of the components are the TLD
 | 			// the rest of the components are the TLD
 | ||||||
|             params['TLD'] = zone.split('.').splice(1).join('.'); | 			params['TLD'] = zone | ||||||
|  | 				.split('.') | ||||||
|  | 				.splice(1) | ||||||
|  | 				.join('.'); | ||||||
| 
 | 
 | ||||||
| 			// setting a host record overwrites all existing,
 | 			// setting a host record overwrites all existing,
 | ||||||
| 			// removing a new records means you've have to send back all previous records without removed
 | 			// 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) { | 			return api('namecheap.domains.dns.getHosts', params).then(function( | ||||||
|                 var currentHostRecordsCount = hostsResponse['DomainDNSGetHostsResult'][0]['host'].length; | 				hostsResponse | ||||||
|  | 			) { | ||||||
|  | 				var currentHostRecordsCount = | ||||||
|  | 					hostsResponse['DomainDNSGetHostsResult'][0]['host'].length; | ||||||
| 
 | 
 | ||||||
| 				for (var i = 0; i < currentHostRecordsCount; i++) { | 				for (var i = 0; i < currentHostRecordsCount; i++) { | ||||||
| 					// console.log(hostsResponse['DomainDNSGetHostsResult'][i]['host'][0]);
 | 					// console.log(hostsResponse['DomainDNSGetHostsResult'][i]['host'][0]);
 | ||||||
|                     var currentEntry =  hostsResponse['DomainDNSGetHostsResult'][0]['host'][i]['$']; | 					var currentEntry = | ||||||
|                     if(currentEntry['Address'] != ch.dnsAuthorization){ | 						hostsResponse['DomainDNSGetHostsResult'][0]['host'][i][ | ||||||
|                         params['HostName'+(i+1)] = currentEntry['Name']; | 							'$' | ||||||
|                         params['RecordType'+(i+1)] = currentEntry['Type']; | 						]; | ||||||
|                         params['Address'+(i+1)] = currentEntry['Address']; | 					if (currentEntry['Address'] != ch.dnsAuthorization) { | ||||||
|                         params['TTL'+(i+1)] = currentEntry['TTL'];     | 						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) { | 				return api('namecheap.domains.dns.setHosts', params) | ||||||
|  | 					.then(function(setHostResponse) { | ||||||
| 						// console.log('setHost');
 | 						// console.log('setHost');
 | ||||||
| 						// console.log(setHostResponse);
 | 						// console.log(setHostResponse);
 | ||||||
|                     return true | 						return true; | ||||||
|                 }).catch(function (err) { | 					}) | ||||||
|                     throw new Error('record did not remove. check subdomain, api key, etc'); | 					.catch(function(err) { | ||||||
|  | 						throw new Error( | ||||||
|  | 							'record did not remove. check subdomain, api key, etc' | ||||||
|  | 						); | ||||||
| 					}); | 					}); | ||||||
| 			}); | 			}); | ||||||
| 
 |  | ||||||
| 		}, | 		}, | ||||||
|         get: function (data) { | 		get: function(data) { | ||||||
| 			var ch = data.challenge; | 			var ch = data.challenge; | ||||||
| 
 | 
 | ||||||
| 			var params = {}; | 			var params = {}; | ||||||
| @ -181,31 +216,35 @@ module.exports.create = function (config) { | |||||||
| 			// the domain is the first part
 | 			// the domain is the first part
 | ||||||
| 			params['SLD'] = zone.split('.')[0]; | 			params['SLD'] = zone.split('.')[0]; | ||||||
| 			// the rest of the components are the TLD
 | 			// the rest of the components are the TLD
 | ||||||
|             params['TLD'] = zone.split('.').splice(1).join('.'); | 			params['TLD'] = zone | ||||||
|  | 				.split('.') | ||||||
|  | 				.splice(1) | ||||||
|  | 				.join('.'); | ||||||
| 
 | 
 | ||||||
|             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('hosts');
 | ||||||
| 				// console.log(hostsResponse);
 | 				// console.log(hostsResponse);
 | ||||||
|                 var currentHostRecords = hostsResponse['DomainDNSGetHostsResult'][0]['host']; | 				var currentHostRecords = | ||||||
|  | 					hostsResponse['DomainDNSGetHostsResult'][0]['host']; | ||||||
| 
 | 
 | ||||||
|                 var entries = currentHostRecords.filter(function (x) { | 				var entries = currentHostRecords.filter(function(x) { | ||||||
| 					return x['$']['Type'] === 'TXT'; | 					return x['$']['Type'] === 'TXT'; | ||||||
| 				}); | 				}); | ||||||
| 
 | 
 | ||||||
|                 var entry = entries.filter(function (x) { | 				var entry = entries.filter(function(x) { | ||||||
| 					// console.log('data', x.data);
 | 					// console.log('data', x.data);
 | ||||||
| 					// console.log('dnsAuth', ch.dnsAuthorization, ch);
 | 					// console.log('dnsAuth', ch.dnsAuthorization, ch);
 | ||||||
| 					return x['$']['Address'] === ch.dnsAuthorization; | 					return x['$']['Address'] === ch.dnsAuthorization; | ||||||
| 				})[0]; | 				})[0]; | ||||||
| 
 | 
 | ||||||
| 				if (entry) { | 				if (entry) { | ||||||
|                     return {dnsAuthorization: entry['$']['Address']}; | 					return { dnsAuthorization: entry['$']['Address'] }; | ||||||
| 				} else { | 				} else { | ||||||
| 					return null; | 					return null; | ||||||
| 				} | 				} | ||||||
| 
 |  | ||||||
| 			}); | 			}); | ||||||
| 
 |  | ||||||
| 		} | 		} | ||||||
| 	}; | 	}; | ||||||
| }; | }; | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user