1.0.8 • Published 4 months ago

@showroomprive/azure-appconfig v1.0.8

Weekly downloads
-
License
ISC
Repository
-
Last release
4 months ago

@showroomprive/azure-appconfig

A library to use Azure App Configuration in NestJS.

It will decompose Azure App Configuration Data into an interface.

Table of Contents

Requirements

To use this library, the following requirements need to be met :

Library NameVersionDescription
@nestjs/common^10.4.8Common utilities used by NestJS applications.
@nestjs/core^10.0.0The core package of the NestJS framework.
rxjs^7.8.1A library for reactive programming using Observables, to make it easier to compose asynchronous or callback-based code.
@nestjs/config^3.3.0A module for managing application configuration in NestJS.

Dependencies

DependencyVersion
@nestjs/common^11.0.11
@azure/app-configuration^1.8.0
@azure/identity^4.7.0
@azure/keyvault-secrets^4.9.0
@nestjs/config^4.0.1
@nestjs/core^11.0.11
rxjs^7.8.2

Dev Dependencies

DependencyVersion
@types/node^22.13.9
typescript^5.8.2

Peer Dependencies

DependencyVersion
@nestjs/common^11.0.11
@nestjs/core^11.0.11
rxjs^7.8.2
@nestjs/config^4.0.1

Data Description

The data is described like this :

export interface ApplicationSettings<T> {
    [key: string]: T | GeneralSettings;
    General: GeneralSettings;
    Application: T;
}

General property will have all data that can be used by any application, his prefix in Azure App Configuration is : SRP.

Application property is a configuration that will be unique for each application, his prefix in Azure App Configuration is equals to the value of APP_CONFIG_NAME environnement variable.

Installation

To install the package, run:

npm install @showroomprive/azure-appconfig

Configuration

To use the library, several environment variables need to be provided in a .env file at the root:

Variable NameDescription
APP_CONFIG_URLThe URL of the Azure App Configuration instance.
ENVIRONMENTThe current environment (e.g., dev, prod, test, local).
APP_CONFIG_NAMEThe name of the Azure App Configuration prefix.

For local developpement, the variable ENVIRONMENT is mandatory, we won't reach the Azure App Configuration, we use a json file that is parsed instead.

The configuration is splitted in 2 parts :

  • General part (prefix in Azure App Configuration : SRP)
  • Application part (prefix in Azure App Configuration: value of APP_CONFIG_NAME environnement variable)

The local configuration should be at the root of the project (near package.json and .env files):

The file should be named : local.config.json :

{
    "SampleSettings": {
        "SampleSetting1": "Value1",
        "SampleSetting2": "Value2"
    },
    "SRP": {
        "InternalBasePaths": {
            "BetaWebApp": "/beta/webapp"
        }
    }
}

Warning: This file may contain sensitive information. Ensure it is not committed to version control and is kept local to your development environment.

app.module.ts

We need imports 2 modules:

  • ConfigModule (global way to share configuration through the whole application)
  • AzureAppconfigModule

We need to add one provider to access the data :

  • ApplicationSettingsService

So the changes will be like that :

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { ConfigModule } from '@nestjs/config';
import { ApplicationSettingsService, AzureAppconfigModule } from '@showroomprive/azure-appconfig';
import { azureAppconfigLoader } from "@showroomprive/azure-appconfig/dist/azure-appconfig-loader";

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true,
        load: [
          azureAppconfigLoader
        ],
     }),
    AzureAppconfigModule],
  controllers: [AppController],
  providers: [ ApplicationSettingsService],
})
export class AppModule {}

Usage

To access the data we need to use Dependency injection for the ApplicationSettingsService and then we will be able to access the General and Application properties.

For example :

import { Controller, Get } from '@nestjs/common';
import { ApplicationSettingsService } from '@showroomprive/azure-appconfig';
import { SampleSettings } from './models/sample-settings';
import { GeneralSettings } from '@showroomprive/azure-appconfig/dist/models/general-settings';

@Controller()
export class AppController {

  constructor(private readonly azureAppConfiguration: ApplicationSettingsService<SampleSettings>) {
  }

  @Get("general")
  getGeneralSettings(): GeneralSettings | undefined {
    return this.azureAppConfiguration.General;
  }

  @Get("application")
  getApplicationSettings(): SampleSettings | undefined {
    return this.azureAppConfiguration.Application;
  }
}

Where SampleSettings is an interface :

export interface SampleSettings {
    SampleSetting1: string;
    SampleSetting2: string;
}

The ApplicationSettingsService<SampleSettings> use ConfigService to get the configurations and configure 2 properties, General and Application :

these 2 get the values like this :

  • General : configService.get<SampleSettings>('General')
  • Application : configService.get<SampleSettings>('Application')

For factory, we can access the configuration on ConfigService from @nestjs/config

There is a sample of how to do it with knex :

import { Global, Module } from '@nestjs/common';
import Knex from 'knex';
import { Model } from 'objection';
import { ConfigService } from '@nestjs/config';
import { SampleSettings } from 'src/models/sample-settings';

@Global()
@Module({
  providers: [   
    {
      provide: 'KnexConnection',
      useFactory: async (configService : ConfigService) => {
        const config = configService.get<SampleSettings>('Application');
        const knex = Knex({
          client: 'mssql',
          connection: {
            host: config.DatabaseSettings.Host,
            user: config.DatabaseSettings.User,
            password: config.DatabaseSettings.Password,
            database: config.DatabaseSettings.Name,
            port: Number(config.DatabaseSettings.Port),
          }
        });
        Model.knex(knex);
        return knex;
      },
      inject: [ConfigService],
    },
  ],
  exports: ['KnexConnection'],
})
export class DatabaseModule {}
1.0.8

4 months ago

1.0.7

6 months ago

1.0.6

7 months ago

1.0.5

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago