adonisjs / env

Framework agnostic environment variables parser and validator
https://docs.adonisjs.com/guides/environment-variables
MIT License
37 stars 10 forks source link

feat(parser): allow to customize interpolation of value #34

Closed RomainLanz closed 12 months ago

RomainLanz commented 12 months ago

Hey there! 👋🏻

This PR adds a way to set up a custom hook, helping to interpolate the value of your env.

It can be useful in multiple scenarios.

  1. To manage your secrets, you may use a Secrets Vault like Docker Secret or HashiCorp Vault. Doing so it will generate a path for your secret and won't give you any value directly. You can write a custom logic to read the file content.

    DB_PASSWORD=file:/run/secrets/db_password
  2. You may encrypt your secrets. You will be able to decrypt them on the fly.

    APP_KEY=encrypted:ksadskjadsjkdasjkdsjdjaskjkdjkasdkj

In user-land, they will be able to pass an option to their Env.create() call.

Env.create(new URL('../', import.meta.url), {
  ....
}, {
  async onVariableRead(key, value) {
    if (value.startWidth('file:') {
      // read from file...
    }

    return value
  }
})

I am unsure if passing the key to the custom hook is useful. Also, if we do, I don't know if the value should be the first argument since people will often use it and disregard the env key.

RomainLanz commented 12 months ago

After a discussion internally, we are closing this PR. A new one will be published with a better DX. ✌🏼