Moved all of the uppercase stuff into the code and out of the config.

This makes it easier to write the config file and makes everything easier to reason about on the code.
This commit is contained in:
Sergio Díaz 2017-03-07 18:01:11 -06:00
parent 5a1f42827f
commit fde0fc6543
9 changed files with 75 additions and 59 deletions

View File

@ -4,21 +4,21 @@ const yaml = require('js-yaml');
describe(`Beau's config Loader.`, () => {
it ('Should only load valid configuration keys', () => {
let HOST = 'http:/martianwabbit.com';
let VERSION = 2;
let CACHE = false;
let host = 'http://martianwabbit.com';
let version = 1;
let cache = false;
let shouldntBeAdded = true;
let beau = new Beau({
VERSION,
HOST,
CACHE,
version,
host,
cache,
shouldntBeAdded
});
expect(beau.config.HOST).toBe(HOST);
expect(beau.config.CACHE).toBe(CACHE);
expect(beau.config.VERSION).toBe(VERSION);
expect(beau.config.HOST).toBe(host);
expect(beau.config.CACHE).toBe(cache);
expect(beau.config.VERSION).toBe(version);
expect(beau.config.shouldntBeAdded).toBeUndefined();
});
});

View File

@ -10,14 +10,15 @@ describe('Request', () => {
beforeEach(() => {
req = {
request: 'POST /user',
HOST: 'http://martianwabbit.com',
PARAMS: {
host: 'http://martianwabbit.com',
alias: 'profile',
params: {
userId: '$profile.UserId'
},
HEADERS: {
headers: {
authentication: 'BEARER $session.token'
},
PAYLOAD: {
payload: {
username: 'seich'
}
};
@ -30,16 +31,16 @@ describe('Request', () => {
});
test('It should load up the given request', () => {
expect(request.$verb).toBe('POST');
expect(request.$endpoint).toBe(req.HOST + '/user');
expect(request.$headers).toBeDefined();
expect(request.$payload).toBeDefined();
expect(request.$params).toBeDefined();
expect(request.VERB).toBe('POST');
expect(request.ENDPOINT).toBe(req.host + '/user');
expect(request.HEADERS).toBeDefined();
expect(request.PAYLOAD).toBeDefined();
expect(request.PARAMS).toBeDefined();
});
test('It should list all of its dependencies', () => {
expect(request.$dependencies.size).toBe(2);
expect(request.$dependencies).toContain('$session');
expect(request.$dependencies).toContain('$profile');
expect(request.DEPENDENCIES.size).toBe(2);
expect(request.DEPENDENCIES).toContain('$session');
expect(request.DEPENDENCIES).toContain('$profile');
});
});

View File

@ -6,8 +6,8 @@ describe('RequestList', () => {
'POST /session': null,
'Not a Request': null,
'POST /user': {
ALIAS: '$user',
PAYLOAD: {
alias: 'user',
payload: {
name: 'Sergio',
lastname: 'Diaz'
}
@ -20,7 +20,7 @@ describe('RequestList', () => {
let request = requests.list[0];
expect(requests.list.length).toBe(2);
expect(request.$verb).toBe('POST');
expect(request.$endpoint).toBe(host + '/session');
expect(request.VERB).toBe('POST');
expect(request.ENDPOINT).toBe(host + '/session');
});
});

View File

@ -37,11 +37,11 @@ if (program.list) {
.column('Endpoint', 20, [clc.cyan])
.output();
beau.requests.list.forEach(({$verb, $alias, $endpoint}) =>
beau.requests.list.forEach(({VERB, ALIAS, ENDPOINT}) =>
new Line().padding(2)
.column($verb, 20, [clc.yellow])
.column($alias, 30, [clc.yellow])
.column($endpoint)
.column(VERB, 20, [clc.yellow])
.column(ALIAS, 30, [clc.yellow])
.column(ENDPOINT)
.output()
);
@ -49,13 +49,12 @@ if (program.list) {
}
if (program.request) {
const request = `$${program.request}`;
const spinner = new Spinner(clc.yellow(`Requesting: ${request}`));
const spinner = new Spinner(clc.yellow(`Requesting: ${program.request}`));
spinner.start();
beau.requests
.execByAlias(request)
.execByAlias(program.request)
.then(res => {
spinner.stop();

View File

@ -0,0 +1,11 @@
version: 1
host: 'https://jsonplaceholder.typicode.com'
GET /posts/:
alias: posts
POST /posts/:
alias: new-post
GET /users/$posts.response.body.0.userId:
alias: post-user

View File

@ -2,27 +2,27 @@ VERSION: '1'
HOST: https://slack.com/api
auth: &auth
token: xoxp-139455775026-139455775090-140286212801-4a786b8b600db898df76302a0bf0784c
token: xoxp-139455775026-139455775090-147751461120-f224ed6ffee029869a0f138d0859e7d6
GET /users.getPresence:
ALIAS: $presence
ALIAS: presence
PARAMS:
<<: *auth
GET /channels.list:
ALIAS: $channel-list
ALIAS: channel-list
PARAMS:
<<: *auth
exclude_archived: true
GET /channels.info:
ALIAS: $channel-info
ALIAS: channel-info
PARAMS:
<<: *auth
channel: $channel-list.response.body.channels.0.id
POST /chat.postMessage:
ALIAS: $new-message
ALIAS: new-message
PARAMS:
<<: *auth
channel: $channel-info.response.body.channel.id
@ -32,6 +32,6 @@ POST /chat.postMessage:
username: Beau
GET /users.list:
ALIAS: $user-list
ALIAS: user-list
PARAMS:
<<: *auth

View File

@ -5,18 +5,23 @@ const RequestCache = require('./requestCache');
class Request {
constructor(req, list) {
let { request, ALIAS, PAYLOAD, HOST, PARAMS, HEADERS } = req;
let { verb, endpoint } = this.parseRequest(request);
this.$verb = verb;
this.$endpoint = HOST + endpoint;
let config = {};
this.$headers = HEADERS;
this.$payload = PAYLOAD;
this.$params = PARAMS;
Object.keys(req).forEach(k => config[k.toUpperCase()] = req[k]);
this.$alias = ALIAS;
this.$dependencies = this.findDependencies(req);
let { REQUEST, ALIAS, PAYLOAD, HOST, PARAMS, HEADERS } = config;
let { verb, endpoint } = this.parseRequest(REQUEST);
this.VERB = verb;
this.ENDPOINT = HOST + endpoint;
this.HEADERS = HEADERS;
this.PAYLOAD = PAYLOAD;
this.PARAMS = PARAMS;
this.ALIAS = ALIAS;
this.DEPENDENCIES = this.findDependencies(req);
this.list = list;
}
@ -33,14 +38,14 @@ class Request {
findDependencies(request, set = new Set()) {
if (typeof request === 'object') {
Object.keys(request).forEach(key => {
if (key === 'ALIAS' || key.startsWith('$'))
if (key === 'ALIAS')
return;
set = this.findDependencies(request[key], set);
});
} else if (typeof request === 'string') {
let matches = request.match(replacementRegex) || [];
let deps = matches.map(m => m.split('.')[0]);
let deps = matches.map(m => m.split('.')[0].substring(1));
return new Set([...set, ...deps]);
}
@ -49,15 +54,15 @@ class Request {
}
exec() {
let dependencies = Array.from(this.$dependencies);
let dependencies = Array.from(this.DEPENDENCIES);
return this.list.fetchDependencies(dependencies).then(cache => {
let endpoint = cache.parse(this.$endpoint);
let request = unirest(this.$verb, endpoint);
let endpoint = cache.parse(this.ENDPOINT);
let request = unirest(this.VERB, endpoint);
request.headers(cache.parse(this.$headers));
request.query(cache.parse(this.$params));
request.send(cache.parse(this.$payload));
request.headers(cache.parse(this.HEADERS));
request.query(cache.parse(this.PARAMS));
request.send(cache.parse(this.PAYLOAD));
return new Promise((resolve, reject) => {
request.end(res => {
@ -74,7 +79,7 @@ class Request {
}
};
cache.add(this.$alias, results);
cache.add(`$${this.ALIAS}`, results);
resolve(results);
});

View File

@ -10,7 +10,7 @@ class RequestList {
}
execByAlias(alias) {
let request = this.list.find(r => r.$alias === alias);
let request = this.list.find(r => r.ALIAS === alias);
if (typeof request === 'undefined') {
return Promise.reject(`${alias} not found among the requests.`);
@ -20,7 +20,7 @@ class RequestList {
.exec()
.catch(reason => {
return Promise
.reject(`${request.$verb} ${request.$endpoint} FAILED. \nDependencies not met:\n${reason}`);
.reject(`${request.VERB} ${request.ENDPOINT} FAILED. \nDependencies not met:\n${reason}`);
});
}

View File

@ -1,6 +1,6 @@
const httpVerbs = ['POST', 'GET', 'PUT', 'PATCH', 'DELETE'];
const requestRegex = new RegExp(`(${httpVerbs.join('|')})\\s(.*)`, 'i');
const replacementRegex = /(\$[a-zA-Z\.\d\-\_]*)/g;
const replacementRegex = /\$([a-zA-Z\.\d\-\_]*)/g;
module.exports = {
httpVerbs,