From b033031a433d0f2adc22c13568fa932fae12a21d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20D=C3=ADaz?= Date: Mon, 13 Feb 2017 18:05:47 -0600 Subject: [PATCH] Doing some basic refactoring to make the code nicer. Removed all methods we weren't user anymore. --- __tests__/requestCache.spec.js | 9 ++------- bin/beau | 1 + request.js | 19 +++++-------------- requestCache.js | 29 +++++++++++++++++------------ requestList.js | 13 ++++++++----- 5 files changed, 33 insertions(+), 38 deletions(-) diff --git a/__tests__/requestCache.spec.js b/__tests__/requestCache.spec.js index d818d87..8d9243e 100644 --- a/__tests__/requestCache.spec.js +++ b/__tests__/requestCache.spec.js @@ -33,7 +33,6 @@ describe('Request Cache', () => { }); }); - describe('parse', () => { it('should transform variables in strings using it\'s cache', () => { expect(cache.parse('Hello $session.hello')).toBe('Hello World'); @@ -56,18 +55,14 @@ describe('Request Cache', () => { it('should parse arrays as well', () => { let parsed = cache.parse({ hello: '$array.0.name' }) expect(parsed.hello).toBe('Sergio'); - console.log([parsed]); }); - }); - describe('safely', () => { it('should return an object when given an undefined value', () => { - expect(Object.keys(cache.safely(undefined)).length).toBe(0) + expect(Object.keys(cache.parse(undefined)).length).toBe(0) }); it('should parse any value other than undefined', () => { - expect(cache.safely('Hello $session.hello')).toBe('Hello World'); + expect(cache.parse('Hello $session.hello')).toBe('Hello World'); }); }); - }); diff --git a/bin/beau b/bin/beau index b7aeec5..1d5426e 100755 --- a/bin/beau +++ b/bin/beau @@ -88,6 +88,7 @@ if (program.request) { process.exit(0); }).catch(function(err) { + new Line().output(); console.error(err); process.exit(1); }); diff --git a/request.js b/request.js index 4ef1b25..5fd9e17 100644 --- a/request.js +++ b/request.js @@ -47,21 +47,15 @@ class Request { } exec(list = new RequestList(), cache = new RequestCache()) { - let dependencies = []; - - if (this.$dependencies.size > 0) { - dependencies = Array.from(this.$dependencies).map(dep => { - return list.execByAlias(dep); - }); - } + let dependencies = Array.from(this.$dependencies).map(dep => list.execByAlias(dep)); return Promise.all(dependencies).then(() => { let endpoint = cache.parse(this.$endpoint); let request = unirest(this.$verb, endpoint); - request.headers(cache.safely(this.$headers)); - request.query(cache.safely(this.$params)); - request.send(cache.safely(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 => { @@ -78,10 +72,7 @@ class Request { } }; - - if (typeof this.$alias !== 'undefined') { - cache.add(this.$alias, results); - } + cache.add(this.$alias, results); resolve(results); }); diff --git a/requestCache.js b/requestCache.js index 14d5159..72cd672 100644 --- a/requestCache.js +++ b/requestCache.js @@ -18,28 +18,33 @@ class RequestCache { }); if (typeof result === 'undefined') { - throw new Error('Key not found in cache: ', path); + throw new Error(`${path} not found in cache: `, path); } return result; } - safely(val) { - if (typeof val === 'undefined') - return {}; - - return this.parse(val); - } - parse(item) { - if (typeof item === 'string') { + let type = typeof item; + + if (type === 'undefined') { + return {}; + } + + if (item === null) { + return null; + } + + if (type === 'string') { return item.replace(replacementRegex, key => this.get(key)); - } else if(typeof item === 'object' && item !== null) { + } + + if (type === 'object') { Object.keys(item).forEach(k => item[k] = this.parse(item[k])); return item; - } else { - return item; } + + return item; } } diff --git a/requestList.js b/requestList.js index 912b2e6..f7d25c8 100644 --- a/requestList.js +++ b/requestList.js @@ -8,10 +8,6 @@ class RequestList { this.cache = new RequestCache(); } - exec(request) { - return request.exec(this, this.cache); - } - execByAlias(alias) { let request = this.list.find(r => r.$alias === alias); @@ -19,7 +15,14 @@ class RequestList { return Promise.reject(`${alias} not found among the requests.`); } - return this.exec(request); + return request + .exec(this, this.cache) + .catch(reason => { + return Promise.reject(`${request.$verb} ${request.$endpoint} FAILED. +Dependencies not met: +${reason} + `); + }); } loadRequests(doc) {