Removed redundant fields from the config class.

This commit is contained in:
David Diaz 2018-06-14 19:15:52 -06:00
parent 2560403dff
commit 7aed5f0085
5 changed files with 48 additions and 193 deletions

View File

@ -45,25 +45,6 @@ Config {
"HOSTS", "HOSTS",
"COOKIEJAR", "COOKIEJAR",
], ],
"defaultConfigValues": Object {
"COOKIEJAR": false,
"DEFAULTS": Object {},
"ENDPOINT": "",
"ENVIRONMENT": Object {},
"HOSTS": Array [],
"PLUGINS": Array [],
"VERSION": 1,
},
"doc": Object {
"GET /anything": Object {
"alias": "anything",
"payload": Object {
"name": "$env.params.name",
},
},
"endpoint": "https://example.org/",
"version": 1,
},
} }
`; `;

View File

@ -51,31 +51,6 @@ Beau {
"HOSTS", "HOSTS",
"COOKIEJAR", "COOKIEJAR",
], ],
"defaultConfigValues": Object {
"COOKIEJAR": false,
"DEFAULTS": Object {},
"ENDPOINT": "",
"ENVIRONMENT": Object {},
"HOSTS": Array [],
"PLUGINS": Array [],
"VERSION": 1,
},
"doc": Object {
"GET /posts/1": "get-post",
"GET /user": Object {
"alias": "user",
"headers": Object {
"hello": "world",
},
},
"defaults": Object {
"headers": Object {
"authentication": "hello",
},
},
"endpoint": "http://jsonplaceholder.typicode.com",
"version": 1,
},
}, },
"requests": RequestList { "requests": RequestList {
"REQUESTS": Array [ "REQUESTS": Array [
@ -153,31 +128,6 @@ Beau {
"HOSTS", "HOSTS",
"COOKIEJAR", "COOKIEJAR",
], ],
"defaultConfigValues": Object {
"COOKIEJAR": false,
"DEFAULTS": Object {},
"ENDPOINT": "",
"ENVIRONMENT": Object {},
"HOSTS": Array [],
"PLUGINS": Array [],
"VERSION": 1,
},
"doc": Object {
"GET /posts/1": "get-post",
"GET /user": Object {
"alias": "user",
"headers": Object {
"hello": "world",
},
},
"defaults": Object {
"headers": Object {
"authentication": "hello",
},
},
"endpoint": "http://jsonplaceholder.typicode.com",
"version": 1,
},
}, },
"list": Array [ "list": Array [
Request { Request {

View File

@ -119,55 +119,6 @@ Config {
"HOSTS", "HOSTS",
"COOKIEJAR", "COOKIEJAR",
], ],
"defaultConfigValues": Object {
"COOKIEJAR": false,
"DEFAULTS": Object {},
"ENDPOINT": "",
"ENVIRONMENT": Object {},
"HOSTS": Array [],
"PLUGINS": Array [],
"VERSION": 1,
},
"doc": Object {
"GET /e1": "e1",
"defaults": Object {
"HEADERS": Object {
"hello": "mars",
},
},
"endpoint": "http://example.org",
"hosts": Array [
Object {
"GET /e2": "e2",
"GET /posts": "posts",
"defaults": Object {
"HEADERS": Object {
"hello": "world",
"world": "hello",
},
},
"endpoint": "http://example.com",
"host": "com",
},
Object {
"GET /e3": "e3",
"GET /posts": "posts",
"defaults": Object {
"HEADERS": Object {
"hello": "world",
"world": "bye",
},
},
"endpoint": "http://example.net",
"host": "net",
},
Object {
"GET /posts": "posts",
"endpoint": "http://example.info",
"host": "info",
},
],
},
} }
`; `;
@ -221,30 +172,5 @@ Config {
"HOSTS", "HOSTS",
"COOKIEJAR", "COOKIEJAR",
], ],
"defaultConfigValues": Object {
"COOKIEJAR": false,
"DEFAULTS": Object {},
"ENDPOINT": "",
"ENVIRONMENT": Object {},
"HOSTS": Array [],
"PLUGINS": Array [],
"VERSION": 1,
},
"doc": Object {
"GET /posts/1": "get-post",
"GET /user": Object {
"alias": "user",
"headers": Object {
"hello": "world",
},
},
"defaults": Object {
"HEADERS": Object {
"authentication": "hello",
},
},
"endpoint": "http://jsonplaceholder.typicode.com",
"version": 1,
},
} }
`; `;

View File

@ -5,21 +5,21 @@ const requireg = require('requireg');
requireg.resolving = false; requireg.resolving = false;
describe('Config', () => { describe('Config', () => {
it('should load valid config keys', () => { it('should load valid config keys', () => {
const doc = yaml.safeLoad(` const doc = yaml.safeLoad(`
version: 1 version: 1
endpoint: http://martianwabbit.com endpoint: http://martianwabbit.com
shouldntBeAdded: true shouldntBeAdded: true
`); `);
const config = new Config(doc); const config = new Config(doc);
expect(config.ENDPOINT).toBe(doc.endpoint); expect(config.ENDPOINT).toBe(doc.endpoint);
expect(config.VERSION).toBe(doc.version); expect(config.VERSION).toBe(doc.version);
expect(config.shouldntBeAdded).toBeUndefined(); expect(config.shouldntBeAdded).toBeUndefined();
}); });
it('should load requests', () => { it('should load requests', () => {
const doc = yaml.safeLoad(` const doc = yaml.safeLoad(`
endpoint: http://example.com endpoint: http://example.com
GET /profile: get-profile GET /profile: get-profile
@ -31,12 +31,12 @@ describe('Config', () => {
hello: world hello: world
`); `);
const config = new Config(doc); const config = new Config(doc);
expect(Object.keys(config.REQUESTS).length).toBe(4); expect(Object.keys(config.REQUESTS).length).toBe(4);
}); });
it('should set up defaults for all requests', () => { it('should set up defaults for all requests', () => {
const doc = yaml.safeLoad(` const doc = yaml.safeLoad(`
version: 1 version: 1
endpoint: 'http://jsonplaceholder.typicode.com' endpoint: 'http://jsonplaceholder.typicode.com'
@ -51,16 +51,17 @@ describe('Config', () => {
hello: world hello: world
`); `);
const config = new Config(doc); const config = new Config(doc);
expect(config).toMatchSnapshot(); expect(config).toMatchSnapshot();
Object.values(config.REQUESTS).forEach(r => { Object.values(config.REQUESTS).forEach(r => {
expect(r.HEADERS.authentication).toMatch('hello'); expect(r.HEADERS.authentication).toMatch('hello');
});
}); });
});
it('should load multiple hosts', () => { it('should load multiple hosts', () => {
const doc = yaml.safeLoad(` const doc = yaml.safeLoad(`
version: 1
endpoint: http://example.org endpoint: http://example.org
defaults: defaults:
@ -98,13 +99,13 @@ describe('Config', () => {
GET /posts: posts GET /posts: posts
`); `);
let config = new Config(doc); let config = new Config(doc);
expect(config).toMatchSnapshot(); expect(config).toMatchSnapshot();
}); });
it('should namespace all aliases within an host', () => { it('should namespace all aliases within an host', () => {
const doc = yaml.safeLoad(` const doc = yaml.safeLoad(`
hosts: hosts:
- host: test1 - host: test1
endpoint: http://example.com endpoint: http://example.com
@ -114,14 +115,14 @@ describe('Config', () => {
GET /posts: posts GET /posts: posts
`); `);
let config = new Config(doc); let config = new Config(doc);
expect(config.REQUESTS[0].ALIAS).toBe('test1:posts'); expect(config.REQUESTS[0].ALIAS).toBe('test1:posts');
expect(config.REQUESTS[1].ALIAS).toBe('test2:posts'); expect(config.REQUESTS[1].ALIAS).toBe('test2:posts');
}); });
it(`should throw if host doesn't have a host key`, () => { it(`should throw if host doesn't have a host key`, () => {
const doc = yaml.safeLoad(` const doc = yaml.safeLoad(`
hosts: hosts:
- endpoint: http://example.com - endpoint: http://example.com
GET /posts: posts GET /posts: posts
@ -131,11 +132,11 @@ describe('Config', () => {
GET /posts: posts GET /posts: posts
`); `);
expect(() => new Config(doc)).toThrow(); expect(() => new Config(doc)).toThrow();
}); });
it(`should merge host settings with global settings`, () => { it(`should merge host settings with global settings`, () => {
const doc = yaml.safeLoad(` const doc = yaml.safeLoad(`
defaults: defaults:
headers: headers:
hello: 1 hello: 1
@ -153,7 +154,7 @@ describe('Config', () => {
GET /posts: posts GET /posts: posts
`); `);
let config = new Config(doc); let config = new Config(doc);
expect(config.REQUESTS[0].HEADERS.hello).toBe(1); expect(config.REQUESTS[0].HEADERS.hello).toBe(1);
}); });
}); });

View File

@ -1,11 +1,12 @@
const deepMerge = require('deepmerge'); const deepMerge = require('deepmerge');
const { requestRegex, UpperCaseKeys } = require('./shared'); const { requestRegex, UpperCaseKeys } = require('./shared');
const Plugins = require('./plugins'); const Plugins = require('./plugins');
const version = parseInt(require('../package.json').version, 10);
class Config { class Config {
constructor(doc, env = {}) { constructor(doc, env = {}) {
this.defaultConfigValues = { const defaultConfigValues = {
VERSION: 1, VERSION: version,
ENDPOINT: '', ENDPOINT: '',
PLUGINS: [], PLUGINS: [],
DEFAULTS: {}, DEFAULTS: {},
@ -14,11 +15,10 @@ class Config {
COOKIEJAR: false COOKIEJAR: false
}; };
this.configKeys = Object.keys(this.defaultConfigValues); this.configKeys = Object.keys(defaultConfigValues);
this.doc = doc;
let config = this.loadConfig(doc); let config = this.loadConfig(doc);
Object.assign(this, this.defaultConfigValues, config); Object.assign(this, defaultConfigValues, config);
this.ENVIRONMENT = deepMerge(this.ENVIRONMENT, env); this.ENVIRONMENT = deepMerge(this.ENVIRONMENT, env);
@ -29,21 +29,18 @@ class Config {
ENDPOINT: this.ENDPOINT ENDPOINT: this.ENDPOINT
}); });
this.loadHosts(this.HOSTS, config); this.loadHosts(this.HOSTS, config, defaultConfigValues);
this.PLUGINS = new Plugins(this.PLUGINS); this.PLUGINS = new Plugins(this.PLUGINS);
} }
loadHosts(hosts, rootConfig) { loadHosts(hosts, rootConfig, defaultConfigValues) {
hosts.forEach(host => { hosts.forEach(host => {
if (typeof host.host === 'undefined') { if (typeof host.host === 'undefined') {
throw new Error(`Host doesn't indicate it's host name.`); throw new Error(`Host doesn't indicate it's host name.`);
} }
let config = deepMerge( let config = deepMerge(defaultConfigValues, this.loadConfig(host));
this.defaultConfigValues,
this.loadConfig(host)
);
config.DEFAULTS = deepMerge(rootConfig.DEFAULTS, config.DEFAULTS); config.DEFAULTS = deepMerge(rootConfig.DEFAULTS, config.DEFAULTS);