diff --git a/examples/jsonplaceholder.yml b/examples/jsonplaceholder.yml index 0556dbf..8f703f7 100644 --- a/examples/jsonplaceholder.yml +++ b/examples/jsonplaceholder.yml @@ -1,11 +1,15 @@ version: 1 endpoint: 'http://jsonplaceholder.typicode.com' +environment: + the: + post: 1 + defaults: headers: hello: $posts.body.0.userId -GET /posts/1: get-post +GET /posts/$env.the.post: get-post GET /posts/: alias: posts diff --git a/src/__tests__/__snapshots__/beau.spec.js.snap b/src/__tests__/__snapshots__/beau.spec.js.snap index 6f7fbc0..b1abe4f 100644 --- a/src/__tests__/__snapshots__/beau.spec.js.snap +++ b/src/__tests__/__snapshots__/beau.spec.js.snap @@ -10,6 +10,7 @@ Beau { }, }, "ENDPOINT": "http://jsonplaceholder.typicode.com", + "ENVIRONMENT": Object {}, "PLUGINS": Array [], "VERSION": 1, }, @@ -19,6 +20,7 @@ Beau { "ENDPOINT", "PLUGINS", "DEFAULTS", + "ENVIRONMENT", ], "defaults": Object { "CACHE": false, @@ -28,12 +30,15 @@ Beau { }, }, "ENDPOINT": "http://jsonplaceholder.typicode.com", + "ENVIRONMENT": Object {}, "PLUGINS": Array [], "VERSION": 1, }, "requests": RequestList { "cache": RequestCache { - "$cache": Object {}, + "$cache": Object { + "$env": Object {}, + }, }, "config": Object { "CACHE": false, @@ -43,6 +48,7 @@ Beau { }, }, "ENDPOINT": "http://jsonplaceholder.typicode.com", + "ENVIRONMENT": Object {}, "PLUGINS": Array [], "VERSION": 1, }, diff --git a/src/beau.js b/src/beau.js index fadef24..35ead4f 100644 --- a/src/beau.js +++ b/src/beau.js @@ -10,7 +10,8 @@ class Beau { CACHE: false, ENDPOINT: '', PLUGINS: [], - DEFAULTS: [] + DEFAULTS: [], + ENVIRONMENT: {} }; this.configKeys = Object.keys(this.defaults); diff --git a/src/requestCache.js b/src/requestCache.js index d53e993..d10f0b3 100644 --- a/src/requestCache.js +++ b/src/requestCache.js @@ -5,6 +5,10 @@ class RequestCache { this.$cache = {}; } + exists(key) { + return typeof this.$cache[key] !== 'undefined'; + } + add(key, value) { this.$cache[key] = value; } diff --git a/src/requestList.js b/src/requestList.js index 949ed2c..8044a6a 100644 --- a/src/requestList.js +++ b/src/requestList.js @@ -10,9 +10,15 @@ class RequestList { this.modifiers = this.loadPlugins(); this.list = this.loadRequests(doc); this.cache = new RequestCache(); + + this.cache.add(`$env`, this.config.ENVIRONMENT); } async execByAlias(alias) { + if (this.cache.exists(`$${alias}`)) { + return this.applyPostResponseModifiers(this.cache.get(`$${alias}`)); + } + const request = this.list.find(r => r.ALIAS === alias); if (typeof request === 'undefined') { @@ -23,13 +29,7 @@ class RequestList { await this.fetchDependencies(Array.from(request.DEPENDENCIES)); const response = await request.exec(this.modifiers, this.cache); - this.modifiers.forEach(mod => { - if (typeof mod.postResponse !== 'undefined') { - mod.postResponse(response); - } - }); - - return response; + return this.applyPostResponseModifiers(response); } catch (reason) { throw new Error( `Request: ${request.VERB} ${request.ENDPOINT} FAILED. \n${reason}` @@ -76,6 +76,16 @@ class RequestList { return new (requireg(name))(settings); }); } + + applyPostResponseModifiers(response) { + this.modifiers.forEach(mod => { + if (typeof mod.postResponse !== 'undefined') { + mod.postResponse(response); + } + }); + + return response; + } } module.exports = RequestList;