mirror of https://github.com/Seich/Beau.git
This adds environment variables.
Allows you to set variables that you can then use throughout your configuration file.
This commit is contained in:
parent
44205d488c
commit
90752c47b1
|
|
@ -1,11 +1,15 @@
|
||||||
version: 1
|
version: 1
|
||||||
endpoint: 'http://jsonplaceholder.typicode.com'
|
endpoint: 'http://jsonplaceholder.typicode.com'
|
||||||
|
|
||||||
|
environment:
|
||||||
|
the:
|
||||||
|
post: 1
|
||||||
|
|
||||||
defaults:
|
defaults:
|
||||||
headers:
|
headers:
|
||||||
hello: $posts.body.0.userId
|
hello: $posts.body.0.userId
|
||||||
|
|
||||||
GET /posts/1: get-post
|
GET /posts/$env.the.post: get-post
|
||||||
|
|
||||||
GET /posts/:
|
GET /posts/:
|
||||||
alias: posts
|
alias: posts
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ Beau {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"ENDPOINT": "http://jsonplaceholder.typicode.com",
|
"ENDPOINT": "http://jsonplaceholder.typicode.com",
|
||||||
|
"ENVIRONMENT": Object {},
|
||||||
"PLUGINS": Array [],
|
"PLUGINS": Array [],
|
||||||
"VERSION": 1,
|
"VERSION": 1,
|
||||||
},
|
},
|
||||||
|
|
@ -19,6 +20,7 @@ Beau {
|
||||||
"ENDPOINT",
|
"ENDPOINT",
|
||||||
"PLUGINS",
|
"PLUGINS",
|
||||||
"DEFAULTS",
|
"DEFAULTS",
|
||||||
|
"ENVIRONMENT",
|
||||||
],
|
],
|
||||||
"defaults": Object {
|
"defaults": Object {
|
||||||
"CACHE": false,
|
"CACHE": false,
|
||||||
|
|
@ -28,12 +30,15 @@ Beau {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"ENDPOINT": "http://jsonplaceholder.typicode.com",
|
"ENDPOINT": "http://jsonplaceholder.typicode.com",
|
||||||
|
"ENVIRONMENT": Object {},
|
||||||
"PLUGINS": Array [],
|
"PLUGINS": Array [],
|
||||||
"VERSION": 1,
|
"VERSION": 1,
|
||||||
},
|
},
|
||||||
"requests": RequestList {
|
"requests": RequestList {
|
||||||
"cache": RequestCache {
|
"cache": RequestCache {
|
||||||
"$cache": Object {},
|
"$cache": Object {
|
||||||
|
"$env": Object {},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
"config": Object {
|
"config": Object {
|
||||||
"CACHE": false,
|
"CACHE": false,
|
||||||
|
|
@ -43,6 +48,7 @@ Beau {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"ENDPOINT": "http://jsonplaceholder.typicode.com",
|
"ENDPOINT": "http://jsonplaceholder.typicode.com",
|
||||||
|
"ENVIRONMENT": Object {},
|
||||||
"PLUGINS": Array [],
|
"PLUGINS": Array [],
|
||||||
"VERSION": 1,
|
"VERSION": 1,
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,8 @@ class Beau {
|
||||||
CACHE: false,
|
CACHE: false,
|
||||||
ENDPOINT: '',
|
ENDPOINT: '',
|
||||||
PLUGINS: [],
|
PLUGINS: [],
|
||||||
DEFAULTS: []
|
DEFAULTS: [],
|
||||||
|
ENVIRONMENT: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.configKeys = Object.keys(this.defaults);
|
this.configKeys = Object.keys(this.defaults);
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,10 @@ class RequestCache {
|
||||||
this.$cache = {};
|
this.$cache = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exists(key) {
|
||||||
|
return typeof this.$cache[key] !== 'undefined';
|
||||||
|
}
|
||||||
|
|
||||||
add(key, value) {
|
add(key, value) {
|
||||||
this.$cache[key] = value;
|
this.$cache[key] = value;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,15 @@ class RequestList {
|
||||||
this.modifiers = this.loadPlugins();
|
this.modifiers = this.loadPlugins();
|
||||||
this.list = this.loadRequests(doc);
|
this.list = this.loadRequests(doc);
|
||||||
this.cache = new RequestCache();
|
this.cache = new RequestCache();
|
||||||
|
|
||||||
|
this.cache.add(`$env`, this.config.ENVIRONMENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
async execByAlias(alias) {
|
async execByAlias(alias) {
|
||||||
|
if (this.cache.exists(`$${alias}`)) {
|
||||||
|
return this.applyPostResponseModifiers(this.cache.get(`$${alias}`));
|
||||||
|
}
|
||||||
|
|
||||||
const request = this.list.find(r => r.ALIAS === alias);
|
const request = this.list.find(r => r.ALIAS === alias);
|
||||||
|
|
||||||
if (typeof request === 'undefined') {
|
if (typeof request === 'undefined') {
|
||||||
|
|
@ -23,13 +29,7 @@ class RequestList {
|
||||||
await this.fetchDependencies(Array.from(request.DEPENDENCIES));
|
await this.fetchDependencies(Array.from(request.DEPENDENCIES));
|
||||||
const response = await request.exec(this.modifiers, this.cache);
|
const response = await request.exec(this.modifiers, this.cache);
|
||||||
|
|
||||||
this.modifiers.forEach(mod => {
|
return this.applyPostResponseModifiers(response);
|
||||||
if (typeof mod.postResponse !== 'undefined') {
|
|
||||||
mod.postResponse(response);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return response;
|
|
||||||
} catch (reason) {
|
} catch (reason) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Request: ${request.VERB} ${request.ENDPOINT} FAILED. \n${reason}`
|
`Request: ${request.VERB} ${request.ENDPOINT} FAILED. \n${reason}`
|
||||||
|
|
@ -76,6 +76,16 @@ class RequestList {
|
||||||
return new (requireg(name))(settings);
|
return new (requireg(name))(settings);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
applyPostResponseModifiers(response) {
|
||||||
|
this.modifiers.forEach(mod => {
|
||||||
|
if (typeof mod.postResponse !== 'undefined') {
|
||||||
|
mod.postResponse(response);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = RequestList;
|
module.exports = RequestList;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue