0.67.0 • Published 3 months ago

@rnw-community/nestjs-typed-config v0.67.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

NestJS Typed config

NestJS typed configuration with full TypeScript support.

npm version npm downloads

Installation

Install additional peer dependencies:

Configuration

  • Create enum with names for all the required environment variable names, interface with type for every environment variable and validation schema using joi.
export enum EnvironmentVariablesEnum {
    POSTGRES_USERNAME = 'POSTGRES_USERNAME',
    POSTGRES_PASSWORD = 'POSTGRES_PASSWORD',
    POSTGRES_HOST = 'POSTGRES_HOST',
}

export interface EnvironmentVariablesInterface {
    [EnvironmentVariablesEnum.POSTGRES_USERNAME]: string;
    [EnvironmentVariablesEnum.POSTGRES_PASSWORD]: string;
    [EnvironmentVariablesEnum.POSTGRES_HOST]: string;
}

import Joi from 'joi';

export const environmentVariablesValidationSchema = Joi.object({
    [EnvironmentVariablesEnum.POSTGRES_USERNAME]: Joi.string().required(),
    [EnvironmentVariablesEnum.POSTGRES_PASSWORD]: Joi.string().required(),
    [EnvironmentVariablesEnum.POSTGRES_HOST]: Joi.string().required(),
});
  • Create config module and service for NestJS DI, this module and service should be used in the project:
import { Inject, Injectable } from '@nestjs/common';

import { NestJSTypedConfigModule } from '@rnw-community/nestjs-typed-config';

const [BaseConfigModule, BaseConfigService] = NestJSTypedConfigModule.create<
    EnvironmentVariablesEnum,
    EnvironmentVariablesInterface
>(environmentVariablesValidationSchema);

@Injectable()
export class ConfigService extends BaseConfigService {}

@Module({
    imports: [BaseConfigModule],
    providers: [ConfigService],
    exports: [ConfigService],
})
export class ConfigModule {}

Usage

import { ConfigService } from './config.module.ts';

@Injectable()
class DomainService {
    private readonly postgresPassword: string;

    constructor(private readonly config: ConfigService) {
        this.postgresPassword = config.get(EnvironmentVariablesEnum.POSTGRES_PASSWORD);
    }

    // ...
}

Reading values from files

To read an environment variable from file, the environment variable name should end with _FILE:

export enum EnvironmentVariablesEnum {
    POSTGRES_PASSWORD_FILE = 'POSTGRES_PASSWORD_FILE',
}

Value of that environment variable should be a path to the file:

echo $POSTGRES_PASSWORD_FILE
/secrets/postgres_pwd

License

This library is licensed under The MIT License.

0.67.0

3 months ago

0.66.0

3 months ago

0.65.2

4 months ago

0.65.3

4 months ago

0.64.0

5 months ago

0.65.0

5 months ago

0.63.0

5 months ago

0.62.4

5 months ago

0.62.1

6 months ago

0.62.0

7 months ago

0.62.2

6 months ago

0.60.0

7 months ago

0.59.3

7 months ago

0.57.2

7 months ago

0.57.3

7 months ago

0.57.0

7 months ago

0.57.1

7 months ago

0.55.0

7 months ago

0.59.6

7 months ago

0.59.4

7 months ago

0.59.5

7 months ago

0.54.12

10 months ago

0.61.0

7 months ago

0.56.0

7 months ago

0.53.0

1 year ago

0.51.0

1 year ago

0.52.3

1 year ago

0.54.0

1 year ago

0.52.0

1 year ago

0.50.0

1 year ago

0.49.1

1 year ago

0.49.2

1 year ago

0.49.0

1 year ago

0.43.0

2 years ago

0.48.0

2 years ago

0.46.0

2 years ago

0.44.0

2 years ago

0.42.0

2 years ago

0.42.1

2 years ago

0.47.0

2 years ago

0.43.1

2 years ago

0.45.0

2 years ago

0.43.2

2 years ago

0.41.0

2 years ago

0.38.2

2 years ago

0.38.1

2 years ago

0.38.0

2 years ago

0.36.1

2 years ago

0.36.0

2 years ago

0.34.0

2 years ago

0.32.1

2 years ago

0.38.3

2 years ago

0.32.0

2 years ago

0.40.0

2 years ago

0.39.0

2 years ago

0.37.0

2 years ago

0.35.1

2 years ago

0.35.0

2 years ago

0.33.0

2 years ago

0.31.0

2 years ago

0.21.0

2 years ago

0.20.0

2 years ago

0.19.0

2 years ago

0.18.1

2 years ago

0.19.1

2 years ago

0.18.3

2 years ago

0.18.4

2 years ago

0.11.0

2 years ago

0.12.0

2 years ago

0.13.0

2 years ago

0.12.1

2 years ago

0.14.0

2 years ago

0.15.0

2 years ago

0.14.1

2 years ago

0.16.0

2 years ago

0.14.2

2 years ago

0.17.0

2 years ago

0.18.0

2 years ago

0.17.1

2 years ago

0.30.0

2 years ago

0.29.0

2 years ago

0.27.2

2 years ago

0.28.0

2 years ago

0.27.1

2 years ago

0.27.0

2 years ago

0.26.1

2 years ago

0.26.0

2 years ago

0.24.2

2 years ago

0.25.0

2 years ago

0.24.1

2 years ago

0.24.0

2 years ago

0.23.1

2 years ago

0.23.0

2 years ago

0.22.0

2 years ago

0.27.4

2 years ago

0.27.3

2 years ago

0.10.1

2 years ago

0.10.0

2 years ago