@rnw-community/nestjs-typed-config
v3.0.5
Published
NestJS typed config
Maintainers
Readme
NestJS Typed config
NestJS typed configuration with full TypeScript support.
Installation
Install additional peer dependencies:
Supported combinations:
| @nestjs/common | @nestjs/config | joi |
| ---------------- | ---------------- | --------------------- |
| ^11.1.28 | ^4.0.4 | ^17.1.1 \|\| ^18.2.5 |
| ^12.0.0 | ^12.0.1 | ^17.1.1 \|\| ^18.2.5 |
The Joi schema is validated by this package itself (all errors reported at once, unknown variables allowed, values
coerced and defaulted) and handed to ConfigModule.forRoot through its validate callback, so behaviour is identical on
both config majors and does not depend on @nestjs/config 12's Standard Schema support.
Exports
NestJSTypedConfigModule
NestJSTypedConfigModule.create<Enum, Config>(validationSchema) returns a [DynamicModule, Type<NestJSTypedConfigService<Enum, Config>>] tuple — extend the returned service class and wire the module into your own @Module({ imports: [BaseConfigModule] }). See "Configuration" below for the full setup.
NestJSTypedConfigService
Injectable service with a single get<K extends keyof Config>(envVariable: K): Config[K] method; caches lookups and transparently reads _FILE-suffixed variables from disk. See "Usage" below.
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 { Injectable, Module } 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_pwdLicense
This library is licensed under The MIT License.
