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

[ENHANCEMENT] Add an option to load values from different source #2

Open
vnenkpet opened this issue Oct 26, 2023 · 1 comment
Open
Assignees
Labels
enhancement New feature or request

Comments

@vnenkpet
Copy link
Collaborator

vnenkpet commented Oct 26, 2023

Something like this:

@ValueFrom([
    new SecretFromFileCustomGetter().getValue('JWT_SECRET'), // priority
    new EnvValueGetter().getValue('JWT_SECRET'), // fallback
])
readonly jwtSecret: string;

where SecretFromFileCustomGetter, EnvValueGetter would be classes implementing interface like this:

export interface ValueGetter {
   getValue(key: string): string;
}

and this:

@Env('JWT_SECRET')
readonly jwtSecret: string;

would then simply be a shorthand for this:

@ValueFrom([
    new EnvValueGetter('JWT_SECRET'),
])
readonly jwtSecret: string;

(@Env, @ValueFrom, @EnvValueGetter and ValueGetter interface would all be provided by this library, and devs can implement their own value getter, like SecretFromFileCustomGetter).

The purpose for this is for people to be able to read config values from different sources than environment variables or .env file.

They should then be able to write their own shorthand for simplicity and this should be documented, like:

@SecretFromFile('JWT_SECRET')
readonly jwtSecret: string;
@vnenkpet vnenkpet added the enhancement New feature or request label Oct 26, 2023
@vnenkpet vnenkpet self-assigned this Oct 26, 2023
@ondra-m
Copy link

ondra-m commented Oct 26, 2023

What if I need value from 3 sources?

For example DB password

  1. For PRD it will be stored as secret
  2. For DEV it will be stored as env
  3. For local it will be stored as default

Something like this?

@Secret("DB_PASSWORD")
@Env("DB_PASSWORD")
@Default("postgres")
@IsString()
readonly dbPassword: string

I thinking that decorators is too "heavy" for such as simple task. What about this?

class Config extends Configuration {
  readonly dbPassword = value({
    fromSecret: "DB_PASSWORD",
    fromEnv: "DB_PASSWORD",
    default: () => "default",
    type: "string",
    trim: true,
    parseAs: "password",
    exposeInDevConsole: false,
  })
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants