0

I am using config module from npm with JS or TS. let's imagine I have the following default.json:

{
  "database": {
    "username": "admin",
    "password": "admin"
 }
}

then somewhere in my code I write

import config from "config";
// ...
config.get('database.username') //

Thing I would like to have is some kind of hint during coding of what I can get from config. I can think of usage of constants like

const CONFIG = {
  DATABASE: {
    USERNAME: "database.username",
    PASSWORD: "database.password"
  }
}
// and then
const { DATABASE } = CONFIG;
config.get(DATABASE.USERNAME)

but maybe there is some other way, more automatic than manual as we already have this information in configuration files.

Is there a way to achieve this?

zmii
  • 4,123
  • 3
  • 40
  • 64
  • You can use a [schema](https://json-schema.org/learn/getting-started-step-by-step.html). Many IDEs support it. – jabaa Sep 20 '21 at 08:30
  • @jabaa but how does this apply in my case? to get value from `config` I need to get it's string name with dot accessors. How schema will improve intellisence ? – zmii Sep 20 '21 at 15:07
  • Usually a config file has a static, documented structure. This structure is described in the schema and your IDE will read this schema. – jabaa Sep 20 '21 at 15:23

0 Answers0