Skip to content

feat: add env prefix option #428

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

Merged
merged 2 commits into from
Mar 3, 2025

Conversation

dderevjanik
Copy link
Contributor

This pull request introduces a new feature to env store prefix, allowing environment variables to be filtered and stripped of a specified prefix.

ref #157

based on https://www.dynaconf.com/envvars/

@dderevjanik
Copy link
Contributor Author

Hi @indexzero,

just wanted to kindly follow up on this PR to see if there’s anything else I can do to help move it forward? Thank you :)

@dderevjanik
Copy link
Contributor Author

Hi @mhamann,

just wanted to kindly follow up on this PR to see if there’s anything else I can do to help move it forward? Thank you :)

@mhamann
Copy link
Collaborator

mhamann commented Mar 3, 2025

Hey @dderevjanik, thanks for your submission. Seems like a good feature that adds some parity with libraries in other languages. I'll go ahead and merge it, but it may take a bit of time to get it released due to my current workload.

@mhamann mhamann merged commit 6bb5561 into indexzero:master Mar 3, 2025
3 checks passed
@rodw
Copy link

rodw commented Mar 12, 2025

@mhamann thanks for merging this enhancement and for the nconf library in general

would it be possible to publish a new release of nconf to npm to make this feature available for import?

@mhamann
Copy link
Collaborator

mhamann commented Mar 18, 2025

I'll try to get this released by end of week. Thanks!

@rodw
Copy link

rodw commented Mar 20, 2025

Thanks again @mhamann I appreciate the work you have and continue to put in on this module.

Incidentally, as may already be obvious to some in this thread, I realized that one can emulate this prefix-stripping (and the corresponding "ignore everything without the prefix) behavior by adding a transform to the env() options map.

E.g. something like this:

// the prefix to strip from env var keys; e.g., `foo_bar` -> `bar`
const envVarPrefix = "foo_";

// when true, only include env vars that start with the prefix
const excludeWhenMissingPrefix = true;

const envOptions = {
  transform: (pair: { key: string, value: unknown }): { key: string, value: unknown } | null => {
    if (pair.key.startsWith(envVarPrefix)) {
      pair.key = pair.key.substring(envVarPrefix.length);
      return pair;
    } else {
      return excludeWhenMissingPrefix ? null : pair;
    }
  }
}

nconf.env(envOptions);

This prefix flag in PR#428 is more convenient and straightforward than a custom transform, but it's hard to overstate how powerful/flexible the transform functions are

@dderevjanik
Copy link
Contributor Author

@rodw You're absolutely right that the transform function provides a powerful way to modify environment variable keys dynamically. However, one key limitation is that using transform prevents you from leveraging parseValues and separator, since transform overrides the default processing logic.

const envOptions = {
  prefix: "foo_",
  separator: "__",
  parseValues: true
};

nconf.env(envOptions);

With this PR, the prefix option allows you to strip a prefix while still benefiting from parseValues and separator (and others aswell).

This approach keeps the configuration clean and declarative while avoiding the need for a custom transform function. It ensures that prefix stripping works seamlessly with other nconf features rather than requiring trade-offs.

@dderevjanik
Copy link
Contributor Author

Hi @mhamann,

Just checking in, do you have any plans to make a new release that includes this feature?

Thanks in advance! :)

mhamann pushed a commit that referenced this pull request Apr 13, 2025
* feat: add env prefix option

* chore: remove console.log from test
@mhamann
Copy link
Collaborator

mhamann commented Apr 14, 2025

This is now released in v0.13 and v1.0.0-beta.2

@dderevjanik
Copy link
Contributor Author

Hi @mhamann, thanks for releasing v0.13!
Unfortunately, it looks like there was a bad merge: c0c8787.
The line this.prefix = options.prefix || ''; is missing, which causes the code not to work at all.

@mhamann
Copy link
Collaborator

mhamann commented Apr 29, 2025

Hmm, interesting. All of the tests pass. Are we missing coverage there?

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.

3 participants