0.0.11 • Published 1 year ago

@nonfig/nestjs-config v0.0.11

Weekly downloads
8
License
MIT
Repository
github
Last release
1 year ago

CircleCI

Summary

:package: Installation

  • Using Nest CLI:
nest add @nonfig/nestjs-config
  • Using Package Manager:
npm install --save @nonfig/nestjs-config
  • Using Yarn
yarn add @nonfig/nestjs-config

:wrench: Setup

Explain your library setup.

import { Module } from '@nestjs/common';
import { NonfigModule, NonfigOptions } from '@nonfig/nestjs-config';

const CONFIG: NonfigOptions = {
  appId: '<Your Application ID>',
  appSecret: '<Your Application Secret>',
  cacheTtl: 60000  
}

@Module({
  imports: [
    ...
    NonfigModule.register(CONFIG)
  ],
  controllers: [ ... ],
  providers: [ ... ],
})
export class AppModule {}

:control_knobs: Config

NameTypeDefaultDescriptionRequired
appIdstring<DEFAULT>Nonfig consumer's app IDYes
appSecretstring<DEFAULT>Nonfig consumer's app SecretYes
cacheTtlnumber60000Cache time to live in millisecondsNo

Usage

Retrieve single configuration

import { NonfigService } from '@nonfig/nestjs-config';


export class MyRepoService {
    constructor(private nonfig: NonfigService) {}

    async getPricing() {
        const name = '/path/to/pricing/config'
        return this.nonfig.findByName(name)
    }

}

export class MyFacadeService {

    constructor(private repo: MyRepoService) {}
    
    async applyPricing() {
        const config = await this.repo.getPricing()
        
        // write your code here to use pricingConfig
    }   

}

Retrieve multiple configurations

Example: Fetching the list of supported languages of application

// Application Controller
export class AppController {
    constructor(private service: AppService) {}

    @Get()
    async getLanguageList() {
        return this.service.getLanguageList()
    }   
}


import { NonfigService } from '@nonfig/nestjs-config';

//Application Service
export class AppService {

    constructor(private nonfig: NonfigService) {}

    async getLanguageList() {
        return this.nonfig.findByPath('/languages/list')
    }   

}

Retrieve configuration using ID

import { NonfigService } from '@nonfig/nestjs-config';

//Application Service
export class AppService {

    constructor(private nonfig: NonfigService) {}

    async getSpecificTranslation(id: string) {
        return this.nonfig.findById(id)
    }   

}

Retrieve multiple configurations using Labels

Example: Fetching the language of application using specific labels

// Application Controller
export class AppController {
    constructor(private service: AppService) {}

    @Get('language')
    async language(@Param('label') label: string) {
        return this.service.getLanguageByLabel(label.split(','))
    }   
}


import { NonfigService } from '@nonfig/nestjs-config';

//Application Service
export class AppService {

    constructor(private nonfig: NonfigService) {}

    async getLanguageByLabel(label: string[]): Promise<Languages[]> {
        return this.nonfig.findByLabels<Language>(label)
    }   

}