mirror of
https://github.com/tiennm99/is-a-dev.git
synced 2026-05-14 08:58:25 +00:00
Refactors domain service
This commit is contained in:
@@ -20,6 +20,8 @@ const toHostList = R.chain(data => {
|
||||
|
||||
const registerDomains = async ({ domainService, getDomains }) => {
|
||||
const domains = await getDomains().then(toHostList);
|
||||
|
||||
console.log(domains);
|
||||
};
|
||||
|
||||
const main = () => {
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
const R = require('ramda');
|
||||
const { getDomainService } = require('../utils/domain-service');
|
||||
|
||||
const getNcClass = ({ onSet, onGet } = {}) => class Namecheap {
|
||||
dns = {
|
||||
const getNc = ({ onSet, onGet } = {}) => ({
|
||||
dns: {
|
||||
setHosts: (_, list) => onSet(list),
|
||||
getHosts: (_) => onGet(),
|
||||
};
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
describe('Domain service', () => {
|
||||
describe('getHosts', () => {
|
||||
@@ -16,7 +16,7 @@ describe('Domain service', () => {
|
||||
{ Name: 'xx', Type: 'A', Address: '111.1.1212.1' },
|
||||
];
|
||||
const onGet = async () => ({ hosts })
|
||||
const mockDomainService = getDomainService({ Namecheap: getNcClass({ onGet }) });
|
||||
const mockDomainService = getDomainService({ nc: getNc({ onGet }) });
|
||||
const list = await mockDomainService.getHosts();
|
||||
|
||||
expect(list).toEqual([
|
||||
@@ -35,7 +35,7 @@ describe('Domain service', () => {
|
||||
return Promise.resolve(null);
|
||||
});
|
||||
|
||||
const mockDomainService = getDomainService({ Namecheap: getNcClass({ onSet }) });
|
||||
const mockDomainService = getDomainService({ nc: getNc({ onSet }) });
|
||||
await mockDomainService.setHosts(records);
|
||||
expect(onSet).toBeCalledTimes(1);
|
||||
});
|
||||
@@ -51,7 +51,7 @@ describe('Domain service', () => {
|
||||
const onGet = () => Promise.resolve({ hosts: records });
|
||||
const onSet = jest.fn(async () => ({}));
|
||||
|
||||
const mockDomainService = getDomainService({ Namecheap: getNcClass({ onSet, onGet }) });
|
||||
const mockDomainService = getDomainService({ nc: getNc({ onSet, onGet }) });
|
||||
await mockDomainService.updateHosts([
|
||||
{ HostName: 'a', RecordType: 'CNAME', Address: 'boo' },
|
||||
{ HostName: 'b', RecordType: 'CNAME', Address: 'goo' },
|
||||
@@ -76,7 +76,7 @@ describe('Domain service', () => {
|
||||
const onGet = () => Promise.resolve({ hosts: records });
|
||||
const onSet = jest.fn(async () => ({}));
|
||||
|
||||
const mockDomainService = getDomainService({ Namecheap: getNcClass({ onSet, onGet }) });
|
||||
const mockDomainService = getDomainService({ nc: getNc({ onSet, onGet }) });
|
||||
await mockDomainService.updateHosts([
|
||||
{ HostName: 'a', RecordType: 'CNAME', Address: 'boo' },
|
||||
{ HostName: 'b', RecordType: 'CNAME', Address: 'googoogaga' },
|
||||
@@ -100,7 +100,7 @@ describe('Domain service', () => {
|
||||
const onGet = () => Promise.resolve({ hosts: records });
|
||||
const onSet = jest.fn(async () => ({}));
|
||||
|
||||
const mockDomainService = getDomainService({ Namecheap: getNcClass({ onSet, onGet }) });
|
||||
const mockDomainService = getDomainService({ nc: getNc({ onSet, onGet }) });
|
||||
await mockDomainService.updateHosts([
|
||||
{ HostName: 'a', RecordType: 'CNAME', Address: 'boo' },
|
||||
{ HostName: 'b', RecordType: 'CNAME', Address: 'googoogaga' },
|
||||
|
||||
+13
-1
@@ -1,5 +1,6 @@
|
||||
const { toHostList } = require('../scripts/register-domains');
|
||||
const { toHostList, registerDomains } = require('../scripts/register-domains');
|
||||
const { TTL } = require('../utils/constants');
|
||||
const { getDomainService } = require('../utils/domain-service');
|
||||
|
||||
describe('toHostList', () => {
|
||||
it('should flatten domain data to list of hosts (without https)', () => {
|
||||
@@ -18,3 +19,14 @@ describe('toHostList', () => {
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('registerDomains', () => {
|
||||
it('should register the new set of hosts generated from domains list', () => {
|
||||
const localHosts = [];
|
||||
const remoteHosts = [];
|
||||
|
||||
//const domainService = getDomainService({ Namecheap: });
|
||||
//registerDomains({ getDomains: async () => localHosts, domainService });
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
+12
-11
@@ -4,14 +4,7 @@ const { NC_DOMAIN, NC_USER, NC_API_KEY, ENV, IP_ADDRESS } = require('../utils/co
|
||||
|
||||
const IS_SANDBOX = ENV === 'sandbox';
|
||||
|
||||
const getDomainService = ({ Namecheap }) => {
|
||||
const nc = new Namecheap({
|
||||
user: NC_USER,
|
||||
key: NC_API_KEY,
|
||||
ip: IP_ADDRESS,
|
||||
sandbox: IS_SANDBOX,
|
||||
});
|
||||
|
||||
const getDomainService = ({ nc }) => {
|
||||
let hostList = [];
|
||||
|
||||
const getHosts = async () => {
|
||||
@@ -57,10 +50,18 @@ const getDomainService = ({ Namecheap }) => {
|
||||
};
|
||||
|
||||
return { getHosts, setHosts, updateHosts };
|
||||
}
|
||||
};
|
||||
|
||||
const nc = new Namecheap({
|
||||
user: NC_USER,
|
||||
key: NC_API_KEY,
|
||||
ip: IP_ADDRESS,
|
||||
sandbox: IS_SANDBOX,
|
||||
});
|
||||
|
||||
const domainService = getDomainService({ nc });
|
||||
|
||||
module.exports = {
|
||||
getDomainService,
|
||||
domainService: getDomainService({ Namecheap }),
|
||||
domainService,
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user