format code

This commit is contained in:
Hitesh 2019-07-26 21:47:54 -07:00
parent e1c9c0ef08
commit 0dac689012
2 changed files with 96 additions and 52 deletions

View File

@ -1,77 +1,121 @@
'use strict'; 'use strict';
var request; var request;
var defaults = {}; var defaults = {
baseUrl: 'https://api.dnsimple.com/v2/'
};
module.exports.create = function(config) { module.exports.create = function(config) {
var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, '');
var token = config.token;
var account = config.account;
function api(method, path, form) {
var req = {
method: method,
url: baseUrl + path,
headers: {
Authorization: 'Bearer ' + token,
'Content-Type': 'application/json'
},
json: true,
form: form
};
return request(req).then(function(resp) {
if (2 !== Math.floor(resp.statusCode / 100)) {
console.error(resp.statusCode, req.url);
console.error();
console.error('Request:');
console.error(req);
console.error();
console.error('Response:');
console.error(resp.body);
console.error();
throw new Error(
'Error response. Check token, baseUrl, domains, etc.'
);
}
return resp;
});
}
return { return {
init: function(opts) { init: function(opts) {
request = opts.request; request = opts.request;
return null; return null;
}, },
zones: function(data) { zones: function(data) {
<<<<<<< HEAD
// console.info('List Zones', data); // console.info('List Zones', data);
return api('GET', '/' + account + '/zones') return api('GET', '/' + account + '/zones').then(function(resp) {
.then(function(resp) {
return resp['body']['data'].map(function(elem) { return resp['body']['data'].map(function(elem) {
//console.log('DEBUG >>> elem.name: ' + elem.name); //console.log('DEBUG >>> elem.name: ' + elem.name);
return elem.name; return elem.name;
}); });
}); });
=======
//console.info('List Zones', data);
throw Error('listing zones not implemented');
>>>>>>> parent of 16db087... integration tests passed
}, },
set: function(data) { set: function(data) {
// console.info('Add TXT', data); // console.info('Add TXT', data);
throw Error('setting TXT not implemented'); var ch = data.challenge;
}, var txt = ch.dnsAuthorization;
remove: function(data) {
// console.info('Remove TXT', data); return api(
throw Error('removing TXT not implemented'); 'POST',
'/' + account + '/zones/' + ch.dnsZone + '/records',
{
name: '',
type: 'TXT',
content: txt
}
).then(function(resp) {
if (resp.statusCode === 201) {
return true;
}
throw new Error(
'record did not set. check subdomain, api key, etc'
);
});
}, },
get: function(data) { get: function(data) {
// console.info('List TXT', data); // console.info('List TXT', data);
<<<<<<< HEAD
return api('GET', '/' + account + '/zones/' + data.challenge.dnsZone + '/records')
.then(function(resp) {
return api(
'GET',
'/' + account + '/zones/' + data.challenge.dnsZone + '/records'
).then(function(resp) {
var record = resp.body.data.filter(function(record) { var record = resp.body.data.filter(function(record) {
return data.challenge.dnsAuthorization === record.content; return data.challenge.dnsAuthorization === record.content;
})[0]; })[0];
if(txtRecord) return { dnsAuthorization: record.content }; if (record) return { dnsAuthorization: record.content };
else return null; else return null;
}); });
}, },
remove: function(data) { remove: function(data) {
return api(
return api('GET', '/' + account + '/zones/' + data.challenge.dnsZone + '/records') 'GET',
.then(function(resp) { '/' + account + '/zones/' + data.challenge.dnsZone + '/records'
).then(function(resp) {
var record = resp.body.data.filter(function(record) { var record = resp.body.data.filter(function(record) {
return data.challenge.dnsAuthorization === record.content; return data.challenge.dnsAuthorization === record.content;
})[0]; })[0];
if (record) { if (record) {
return api('DELETE', '/' + account + '/zones/' + data.challenge.dnsZone + '/records/' + record.id) return api(
.then(function(resp) { 'DELETE',
'/' +
account +
'/zones/' +
data.challenge.dnsZone +
'/records/' +
record.id
).then(function(resp) {
// console.info('DEBUG >>> resp: ', JSON.stringify(resp, null, 2)); // console.info('DEBUG >>> resp: ', JSON.stringify(resp, null, 2));
return true; return true;
}); });
} } else {
else {
throw new Error('Txt Record not found for removal'); throw new Error('Txt Record not found for removal');
} }
}); });
=======
throw Error('listing TXTs not implemented');
>>>>>>> parent of 16db087... integration tests passed
} }
}; };
}; };

View File

@ -5,11 +5,11 @@
var tester = require('acme-challenge-test'); var tester = require('acme-challenge-test');
require('dotenv').config(); require('dotenv').config();
// Usage: node ./test.js example.com username xxxxxxxxx // Usage: node ./test.js example.com token account
var zone = process.argv[2] || process.env.ZONE; var zone = process.argv[2] || process.env.ZONE;
var challenger = require('./index.js').create({ var challenger = require('./index.js').create({
username: process.argv[3] || process.env.USERNAME, token: process.argv[3] || process.env.TOKEN,
password: process.argv[4] || process.env.PASSWORD account: process.argv[4] || process.env.ACCOUNT
}); });
// The dry-run tests can pass on, literally, 'example.com' // The dry-run tests can pass on, literally, 'example.com'