json-server icon indicating copy to clipboard operation
json-server copied to clipboard

Feat: Expose program config (when used on cli)

Open b4dnewz opened this issue 6 years ago • 1 comments

My proposal is to expose a config property together with the db property on the express application created by json-server cli with the content of the configuration parsed by yargs, useful in particular when using the -c, --config flag since yargs will set all properties as arguments, this allow plugins to have their namespace of options in the config file without any other changes and it will open many customization possibilities to plugins and third party modules based on json-server.

For example take this json-server plugin which right now expose middlewares (to hook into json-server cli) with this update the user can define for example a auth property in the configuration file:

{
  "port": 3001,
  "auth:  {
    jwtExpiration: '1h',
    // more plugin config
  }
}

And this configuration can be later accessed, for example in the /login route exposed by the middleware, like so:

// pseudo code
router.get('/login', (req, res) => {
  const config = req.app.config.auth || defaultConfig
  const token = jwt.sign(user, 'secret', {
    exp: config.jwtExpiration
  })
  res.send(token)
})

This will resolve many of the issues the plugin is facing right now, which depends basically on the possibility to pass custom configurations.

Pros

  • [x] Backward compatibility
  • [x] Extend plugins possibilities
  • [x] Enrich middleware use

The third point is about having this middleware features to be capable of handling the basic needs otherwise implemented by the modules which requires the author of the plugin to depend on a (per package) version of json-server instead of simply hook up to the user choice version as middleware.

Cons

Any consequence? I'm open to feedbacks.


My english is not very good and I've helped myself with g.translator, so if something is not clear about what I tried to explain, please tell me 😄

b4dnewz avatar Mar 09 '19 11:03 b4dnewz

Travis build may failed cause of this, but I think is more related to the jest version

b4dnewz avatar Mar 09 '19 11:03 b4dnewz