From 3c2691b164c64d98dfcb6b86a7d2f53102d1f286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20D=C3=ADaz?= Date: Wed, 5 Apr 2017 22:55:09 -0600 Subject: [PATCH] Added basic support for plugins. Plugins can now be included, they can modify the requests before they are made and modify the result. They are very simple to write and work with. I also wrote and example plugin over at seich/beau-jwt. --- examples/plugins.yml | 11 +++++++++++ package.json | 1 + request.js | 29 +++++++++++++++++++++-------- requestList.js | 28 +++++++++++++++++++++++++++- yarn.lock | 34 +++++++++++++++++++++++++++++++++- 5 files changed, 93 insertions(+), 10 deletions(-) create mode 100644 examples/plugins.yml diff --git a/examples/plugins.yml b/examples/plugins.yml new file mode 100644 index 0000000..a23af73 --- /dev/null +++ b/examples/plugins.yml @@ -0,0 +1,11 @@ +host: http://localhost:10080 + +plugins: + - beau-jwt: + data: + userId: 12 + name: Sergio + secret: 'asdfasdf+asdfasdf/asdfasdfasdfasdf==' + +GET /test: + alias: test diff --git a/package.json b/package.json index 5b46405..de37d30 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "commander": "^2.9.0", "eyes": "^0.1.8", "js-yaml": "^3.7.0", + "requireg": "^0.1.6", "unirest": "^0.5.1" }, "repository": "git@github.com:Seich/Beau.git", diff --git a/request.js b/request.js index 0af23f3..15b535b 100644 --- a/request.js +++ b/request.js @@ -5,8 +5,8 @@ const RequestCache = require('./requestCache'); class Request { constructor(req, list) { - let config = {}; + this.originalRequest = req; Object.keys(req).forEach(k => config[k.toUpperCase()] = req[k]); @@ -53,16 +53,29 @@ class Request { return set; } - exec() { + exec(modifiers = []) { 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 settings = { + endpoint: cache.parse(this.ENDPOINT), + method: this.VERB, + headers: cache.parse(this.HEADERS), + query: cache.parse(this.PARAMS), + payload: cache.parse(this.PAYLOAD) + }; - request.headers(cache.parse(this.HEADERS)); - request.query(cache.parse(this.PARAMS)); - request.send(cache.parse(this.PAYLOAD)); + modifiers.forEach(mod => { + if (typeof mod.preRequest !== 'undefined') { + mod.preRequest(settings, this.originalRequest); + } + }); + + let request = unirest(settings.method, settings.endpoint); + + request.headers(settings.headers); + request.query(settings.query); + request.send(settings.payload); return new Promise((resolve, reject) => { request.end(res => { @@ -75,7 +88,7 @@ class Request { request: { headers: res.request.headers, body: res.request.body, - endpoint: endpoint + endpoint: settings.endpoint }, response: { status: res.status, diff --git a/requestList.js b/requestList.js index af766af..2f2eb60 100644 --- a/requestList.js +++ b/requestList.js @@ -1,10 +1,13 @@ const Request = require('./request'); const RequestCache = require('./requestCache'); const httpVerbs = require('./shared').httpVerbs; +const requireg = require('requireg'); class RequestList { constructor(doc = {}, config = {}) { this.config = config; + + this.modifiers = this.loadPlugins(); this.list = this.loadRequests(doc); this.cache = new RequestCache(); } @@ -17,7 +20,16 @@ class RequestList { } return request - .exec() + .exec(this.modifiers) + .then(res => { + this.modifiers.forEach(mod => { + if (typeof mod.postResponse !== 'undefined') { + mod.postResponse(res); + } + }); + + return res; + }) .catch(reason => { return Promise .reject(`Request: ${request.VERB} ${request.ENDPOINT} FAILED. \n${reason}`); @@ -46,6 +58,20 @@ class RequestList { return new Request(doc[key], this); }); } + + loadPlugins() { + return this.config.PLUGINS.map(plugin => { + let name = plugin; + let settings = null; + + if (typeof plugin === 'object') { + name = Object.keys(plugin)[0]; + settings = plugin[name]; + } + + return new (requireg(name))(settings) + }); + } } module.exports = RequestList; diff --git a/yarn.lock b/yarn.lock index dfff9f5..c29b3cd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -512,6 +512,10 @@ decamelize@^1.0.0, decamelize@^1.1.1: version "1.2.0" resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290" +deep-extend@~0.2.5: + version "0.2.11" + resolved "https://registry.yarnpkg.com/deep-extend/-/deep-extend-0.2.11.tgz#7a16ba69729132340506170494bc83f7076fe08f" + deep-is@~0.1.3: version "0.1.3" resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.3.tgz#b369d6fb5dbc13eecf524f91b070feedc357cf34" @@ -900,6 +904,10 @@ inherits@2, inherits@~2.0.1: version "2.0.3" resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" +ini@~1.3.0: + version "1.3.4" + resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.4.tgz#0537cb79daf59b59a1a517dff706c86ec039162e" + invariant@^2.2.0: version "2.2.2" resolved "https://registry.yarnpkg.com/invariant/-/invariant-2.2.2.tgz#9e1f56ac0acdb6bf303306f338be3b204ae60360" @@ -1580,7 +1588,7 @@ minimatch@2.x: dependencies: brace-expansion "^1.0.0" -minimist@0.0.8, minimist@~0.0.1: +minimist@0.0.8, minimist@~0.0.1, minimist@~0.0.7: version "0.0.8" resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d" @@ -1815,6 +1823,15 @@ randomatic@^1.1.3: is-number "^2.0.2" kind-of "^3.0.2" +rc@~1.0.0: + version "1.0.3" + resolved "https://registry.yarnpkg.com/rc/-/rc-1.0.3.tgz#51bf28d21f13a9324528a9633460161ad9a39f77" + dependencies: + deep-extend "~0.2.5" + ini "~1.3.0" + minimist "~0.0.7" + strip-json-comments "0.1.x" + read-pkg-up@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/read-pkg-up/-/read-pkg-up-1.0.1.tgz#9d63c13276c065918d57f002a57f40a1b643fb02" @@ -1906,10 +1923,21 @@ require-main-filename@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-1.0.1.tgz#97f717b69d48784f5f526a6c5aa8ffdda055a4d1" +requireg@^0.1.6: + version "0.1.6" + resolved "https://registry.yarnpkg.com/requireg/-/requireg-0.1.6.tgz#205052aec2eaa2d8eb128abacf833b6a380a99b6" + dependencies: + rc "~1.0.0" + resolve "~0.6.1" + resolve@1.1.7, resolve@1.1.x, resolve@^1.1.6: version "1.1.7" resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b" +resolve@~0.6.1: + version "0.6.3" + resolved "https://registry.yarnpkg.com/resolve/-/resolve-0.6.3.tgz#dd957982e7e736debdf53b58a4dd91754575dd46" + right-align@^0.1.1: version "0.1.3" resolved "https://registry.yarnpkg.com/right-align/-/right-align-0.1.3.tgz#61339b722fe6a3515689210d24e14c96148613ef" @@ -2046,6 +2074,10 @@ strip-bom@^2.0.0: dependencies: is-utf8 "^0.2.0" +strip-json-comments@0.1.x: + version "0.1.3" + resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-0.1.3.tgz#164c64e370a8a3cc00c9e01b539e569823f0ee54" + supports-color@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7"