Merge pull request #4 from Seich/environment-variables

This adds environment variables.
This commit is contained in:
Sergio Díaz 2018-01-10 16:52:43 -06:00 committed by GitHub
commit 28ddf88a41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 10 deletions

View File

@ -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

View File

@ -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,
},

View File

@ -10,7 +10,8 @@ class Beau {
CACHE: false,
ENDPOINT: '',
PLUGINS: [],
DEFAULTS: []
DEFAULTS: [],
ENVIRONMENT: {}
};
this.configKeys = Object.keys(this.defaults);

View File

@ -5,6 +5,10 @@ class RequestCache {
this.$cache = {};
}
exists(key) {
return typeof this.$cache[key] !== 'undefined';
}
add(key, value) {
this.$cache[key] = value;
}

View File

@ -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;