You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CoreID/app/routing/middleware/util/Config.middleware.js

41 lines
1.2 KiB

/*
* Config Middleware
* -------------------------------------------------------------
* Checks the specified configuration key (and optionally the value).
* If the configuration matches the required value, the request can
* proceed. Otherwise, a 404 will be returned.
*
* To use, add the call to your route's middleware:
* _flitter.mw('util:Config', {
* key: 'server.environment',
* value: 'development'
* })
*
* In this case, the request would be allowed to proceed in the case:
* _flitter.config('server.environment') === 'development'
*
* The 'value' attribute is optional. If none is provided, the request
* can proceed if the config value is truthy.
*/
const Middleware = require('libflitter/middleware/Middleware')
class Config extends Middleware {
static get services() {
return [...super.services, 'configs']
}
/*
* Run the middleware test.
*/
test(req, res, next, args = {}){
if ( !args.key ) return res.error(500)
const config = this.configs.get(args.key)
if ( !args.value && !config ) return res.error(404)
else if ( args.value && args.value !== config ) return res.error(404)
else return next()
}
}
module.exports = Config