mirror of https://github.com/Seich/Beau.git
Merge pull request #4 from Seich/environment-variables
This adds environment variables.
This commit is contained in:
commit
28ddf88a41
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -10,7 +10,8 @@ class Beau {
|
|||
CACHE: false,
|
||||
ENDPOINT: '',
|
||||
PLUGINS: [],
|
||||
DEFAULTS: []
|
||||
DEFAULTS: [],
|
||||
ENVIRONMENT: {}
|
||||
};
|
||||
|
||||
this.configKeys = Object.keys(this.defaults);
|
||||
|
|
|
|||
|
|
@ -5,6 +5,10 @@ class RequestCache {
|
|||
this.$cache = {};
|
||||
}
|
||||
|
||||
exists(key) {
|
||||
return typeof this.$cache[key] !== 'undefined';
|
||||
}
|
||||
|
||||
add(key, value) {
|
||||
this.$cache[key] = value;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in New Issue