npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@itgorillaz/configify

v1.2.3

Published

NestJS Config on Steroids

Downloads

820

Readme

Description

configify is a NestJS configuration module that makes it easier to deal with configuration files and secrets.

Installation

$ npm install --save @itgorillaz/configify

Usage

To start using the configify module in your application import the module by calling the forRootAsync function:

@Module({
  imports: [ConfigifyModule.forRootAsync()],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

By default, when bootstraping, the module will lookup for a .env or an application.yml file at the root folder of the project:

my-web-app
| .env
| application.yml

You can also provide the location of the configuration files by overring the configuration options.

Mapping Configuration Classes

This module will lookup for every class decorated with @Configuration and it will make its instance globally available for the application.

Example of a .env file mapped to a class:

APPLICATION_CLIENT_ID=ABC
APPLICATION_CLIENT_TOKEN=TEST
@Configuration()
export class ApplicationClientConfig {
  @Value('APPLICATION_CLIENT_ID')
  appClientId: string;

  @Value('APPLICATION_CLIENT_TOKEN')
  appClientToken: string
}

Example of a .yml file mapped to a class:

database:
  host: localhost
  port: 3306
  username: test
  password: test
  metadata: |
    {
      "label": "staging"
    }
@Configuration()
export class DatabaseConfiguration {
  @Value('database.host')
  host: string;

  @Value('database.port', {
    parse: (value: any) => parseInt(value)
  })
  port: number;

  @Value('database.metadata', {
    parse: (value: any) => JSON.parse(value)
  })
  metadata: MetadataType;
}

You can map your configuration file to multiple configuration classes:

# database config
DATABASE_HOST=localhost
DATABASE_USER=test
DATABASE_PASSWORD=test

# okta config
OKTA_API_TOKEN=test
OKTA_CLIENT_ID=test
@Configuration()
export class DatabaseConfiguration {
  // database configuration attributes
}
@Configuration()
export class OktaConfiguration {
  // okta configuration attributes
}

Dependency Injection

This module makes all the configuration instances globally available to the application, to access it you just need to declare the configuration class as an argument in the class constructor:

export class AppService {
  private readonly LOGGER = new Logger(AppService.name);

  constructor(private readonly config: MyConfig) {
    this.LOGGER.log(JSON.stringify(config));
  }

}

Variables Expansion

You can make use of variable expasion in your configuration files:

MY_API_KEY=${MY_SECRET} // --> MY_API_KEY=TEST
ANY_OTHER_CONFIG=TEST
MY_SECRET=TEST
APP_CLIENT_ID=${NON_EXISTING_ENV:-DEFAULT_ID} // --> APP_CLIENT_ID=DEFAULT_ID

Dealing with Secrets

Out of the box, this module can resolve AWS Secrets Manager and Parameter Store secrets.

Every configuration attribute stating with AWS_SECRETS_MANAGER, AWS_PARAMETER_STORE, aws-secrets-manager and aws-parameter-store will be considered a special configuration attribute and the module will try to resolve it's remote value.

E.g.: .env

MY_DB_PASSWORD=${AWS_SECRETS_MANAGER_DB_PASSWORD}
MY_API_TOKEN=${AWS_PARAMETER_STORE_API_TOKEN}

AWS_SECRETS_MANAGER_DB_PASSWORD=<secret-id-here>
AWS_PARAMETER_STORE_API_TOKEN=<parameter-name-here>

application.yml

my-db-password: ${aws-secrets-manager.db.password}
my-api-token: ${aws-parameter-store.api.token}

aws-secrets-manager:
  db:
    password: <secret-id-here>

aws-parameter-store:
  api:
    token: <parameter-name-here>
@Configuration()
export class SecretConfiguration {
  @Value('my-db-password') // or @Value('aws-secrets-manager.db.password')
  myDbPassword: string;

  @Value('my-api-token') // or @Value('aws-parameter-store.api.token')
  myApiToken: string;
}

Parsing Configuration Values

Parsing a configuration value can be easily done by using a parse callback function available as argument of the @Value() decorator:

db-json-config: |
  {
    "host": "localhost",
    "user": "test",
    "password": "test"
  }
export interface MyDBConfig {
  host: string;
  user: string;
  password: string;
}

@Configuration()
export class SuperSecretConfiguration {
  @Value('db-json-config', {
    parse: (value: any) => JSON.parse(value)
  })
  myDbConfig: MyDBConfig;
}

Validating Configuration Classes

Depending on how critical a configuration is, you may want to validate it before bootstraping the application, for that you can use class-validator to make sure your configuration is loaded correctly:

@Configuration()
export class MyConfiguration {
  @IsEmail()
  @Value('SENDER_EMAIL')
  senderEmail: string;

  @IsNotEmpty()
  @Value('my-api-token')
  myApiToken: string;
}

Overwrite Default Options

You can overwrite default module otions by providing an object as argumento to the forRootAsync() method:

/**
* Ignores any config file.
* The default value is false;
*/
ignoreConfigFile?: boolean;

/**
* Ignores environment variables
* The default value is false;
*/
ignoreEnvVars?: boolean;

/**
* The path of the configuration files
*/
configFilePath?: string | string[];

/**
* Expands variables
* The default value is true
*/
expandConfig?: boolean;

/**
* The AWS Secrets Manager Client
* If no client is provided, the module will create one.
*/
secretsManagerClient?: SecretsManagerClient;

/**
* The AWS Systems Manager Client
* If no client is provided, the module will create one.
*/
ssmClient?: SSMClient;

License

This code is licensed under the MIT License.

All files located in the node_modules and external directories are externally maintained libraries used by this software which have their own licenses; we recommend you read them, as their terms may differ from the terms in the MIT License.