Skip to content

Feat: Expose program config (when used on cli) #927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

b4dnewz
Copy link

@b4dnewz b4dnewz commented Mar 9, 2019

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

  • Backward compatibility
  • Extend plugins possibilities
  • 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 😄

Add to the express application object the program configuration so they are exposed to any existing plugin
@b4dnewz
Copy link
Author

b4dnewz commented Mar 9, 2019

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

@b4dnewz b4dnewz changed the title Expose program config (when used on cli) Feat: Expose program config (when used on cli) Mar 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant