0.1.1 • Published 6 months ago
@jagodacc/nestjs-aws-config v0.1.1
Installation
npm install --save @jagodacc/nestjs-aws-config
Usage
Module initialization
import { ConfigModule, ConfigModuleOptionsInterface } from '@jagodacc/nestjs-aws-config';
import { Global, Logger, MiddlewareConsumer, Module, NestModule, OnApplicationBootstrap } from '@nestjs/common';
@Module({
imports: [
ConfigModule.forRootAsync({
useFactory: (): ConfigModuleOptionsInterface => {
for (const env of ['AWS_APP_CONFIG_APPLICATION_ID', 'AWS_APP_CONFIG_PROFILE_ID', 'AWS_APP_CONFIG_ENVIRONMENT_ID']) {
if (!process.env[env]) {
throw new Error(`Missing environment variable: ${env}`);
}
}
return {
applicationId: process.env.AWS_APP_CONFIG_APPLICATION_ID!,
profileId: process.env.AWS_APP_CONFIG_PROFILE_ID!,
environmentId: process.env.AWS_APP_CONFIG_ENVIRONMENT_ID!
};
}
})
]
})
export class AppModule {}
Configuration subscription retrieval
Subscribing to configuration updates allows changes to be applied without restarting the application.
import { Inject, Injectable, Logger, OnApplicationBootstrap } from '@nestjs/common';
import { ConfigService } from '@jagodacc/nestjs-aws-config';
import { AppConfigInterface } from '../interfaces';
@Injectable()
export class ExampleService implements OnApplicationBootstrap {
private appConfig!: AppConfigInterface;
constructor(private readonly configService: ConfigService<AppConfigInterface>) {}
public onApplicationBootstrap(): Promise<void> {
return new Promise<void>(resolve => {
this.configService.asObservable().subscribe((value) => {
this.appConfig = value;
resolve();
});
});
}
}
Configuration asynchronously retrieval
import { Inject, Injectable } from '@nestjs/common';
import { ConfigService } from '@jagodacc/nestjs-aws-config';
@Injectable()
export class ExampleService {
constructor(private readonly configService: ConfigService) {}
public async getConfig(): Promise<void> {
const config = await this.configService.getValueAsync();
console.log(config);
}
}