Skip to content
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

Injecting additional values into an existing env #88

Closed
kachkaev opened this issue Aug 22, 2018 · 3 comments
Closed

Injecting additional values into an existing env #88

kachkaev opened this issue Aug 22, 2018 · 3 comments

Comments

@kachkaev
Copy link
Contributor

kachkaev commented Aug 22, 2018

Hi 👋

I have a Node.js project with a few scripts (entrypoints). Some of them require additional parameters like auth tokens etc. and I would like to extend my env object inside some of the actions. This is not the same as #78 because the presence of extra values depends on the code logic rather than the values certain env variables have. An example:

// config.ts
import * as envalid from "envalid";

export const env = envalid.cleanEnv(process.env, {
  SHARED_THING: envalid.str({
    default: "this variable needs to present in all scripts",
  }),
}, { strict: true });
// peculiarTask.ts

import { env } from './config';

const extendedEnv = envalid.cleanEnv(env, {
  EXTRA_THING: envalid.str({
    default: "this variable should only be cleaned inside peculiarTask.ts",
  }),
});

There are a couple of problems at the moment:

  • Using { strict: true } to get extendedEnv clears out all variables that used to exist in env.
  • Even when there is no { strict: true } in the second case, TypeScript autocompletion for extendedEnv.SHARED_THING does not work.

What improvements could we make here? What do others do in similar cases?

@SimenB
Copy link
Collaborator

SimenB commented Aug 22, 2018

I think I'd merge the specifications (that is, export SHARED_THING: envalid.str(... instead of passing it to cleanEnv) then do a cleanEnv from your entrypoint.

Thoughts?

@kachkaev
Copy link
Contributor Author

I had that in mind as well, so that solution would be something like:

// config.ts
import * as envalid from "envalid";

export const sharedValidators = {
  SHARED_THING: envalid.str({
    default: "this variable needs to present in all scripts",
  }),
};
// peculiarTask.ts
import * as envalid from "envalid";
import { sharedValidators } from "./config";

const env = envalid.cleanEnv(process.env, {
  ...sharedValidators,
  EXTRA_THING: envalid.str({
    default: "this variable should only be cleaned inside peculiarTask.ts",
  }),
});

This looks rather clean so far, but has two issues:

  1. I have to import envalid from "envalid" in every entrypoint and run const env = envalid.cleanEnv(process.env, sharedValidators, { strict: true }) despite that most of my commands have no peculiar configs.

  2. I can't derive stuff from the shared env in my config.ts such as:

// config.ts
import * as envalid from "envalid";
import SharedThingManager from "things";
import * as path from "path";

export const env = envalid.cleanEnv(process.env, {
  SHARED_THING: envalid.str({
    default: "this variable needs to present in all scripts",
  }),
}, { strict: true });

// example 1
export const sharedThingManager = new SharedThingManager(env.SHARED_THING);

// example 2
export const resolvedSharedThing = path.resolve(__dirname, env.SHARED_THING);

@af
Copy link
Owner

af commented Jan 28, 2021

This has been open for a long while and I think the solution of sharing the config (like below) is the best way to go. If there's a more actionable proposal, please open a new issue, going to close this one now

const env = envalid.cleanEnv(process.env, {
  ...sharedValidators,
  EXTRA_THING: envalid.str({
    default: "this variable should only be cleaned inside peculiarTask.ts",
  }),
});

@af af closed this as completed Jan 28, 2021
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

No branches or pull requests

3 participants