- Notifications
You must be signed in to change notification settings - Fork0
A Client library to use the Unleash platform in your NestJS Apps to manage the feature flags.
License
Alpha018/nestjs-unleash-client
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
⚠️ Important: Starting from this version, the minimum required Node.js version is20.
$ npm i @alpha018/nestjs-unleash-client
To use the Unleash client in your application, import the module into your main module. The module registers a global guard that will automatically protect routes decorated with@UnleashToggle.
import{UnleashClientModule}from'@alpha018/nestjs-unleash-client';@Module({imports:[// ...UnleashClientModule.forRoot({config:{url:'http://unleash.herokuapp.com/api/',appName:'my-nestjs-app',},isGlobal:true,// Make the module and its providers global}),// ...],})exportclassAppModule{}
import{UnleashClientModule}from'@alpha018/nestjs-unleash-client';import{ConfigModule,ConfigService}from'@nestjs/config';@Module({imports:[// ...UnleashClientModule.forRootAsync({imports:[ConfigModule],useFactory:(configService:ConfigService)=>({config:{url:configService.get('UNLEASH_URL'),appName:configService.get('UNLEASH_APP_NAME'),customHeaders:{Authorization:configService.get('UNLEASH_API_TOKEN')},}}),inject:[ConfigService],isGlobal:true,// Make the module and its providers global}),// ...],})exportclassAppModule{}
Options forforRoot andforRootAsync:
| Parameter | Type | Required | Description |
|---|---|---|---|
config | UnleashConfig | Yes | The configuration object for theunleash-client. See options below. |
isGlobal | boolean | No | Iftrue, the module will be registered as a global module. Defaults tofalse. |
Theconfig object is passed directly to theunleash-client. The most common options are:
config Parameter | Type | Required | Description |
|---|---|---|---|
url | string | Yes | The URL of your Unleash server API. |
appName | string | Yes | The name of your application. |
customHeaders | object | No | Custom headers to be sent to the Unleash API. UseAuthorization for the API key generated by the Unleash panel. |
... | any | No | Any other valid option from theofficial unleash-client-node documentation. |
To protect an endpoint with a feature toggle, use the@UnleashToggle decorator. The globalUnleashGuard will automatically deny access if the feature is disabled.
import{Controller,Get}from'@nestjs/common';import{UnleashToggle}from'@alpha018/nestjs-unleash-client';@Controller()exportclassAppController{ @Get('hello') @UnleashToggle('my-feature-toggle')getHello():string{// This route is only accessible if 'my-feature-toggle' is enabled in Unleashreturn'Hello World!';} @Get('unprotected')getUnprotectedHello():string{// This route is always accessible as it does not have the@UnleashToggle decoratorreturn'This is an unprotected route.';}}
If you need to check a feature toggle directly in your code, you can inject theUnleashClientProvider.
import{Controller,Get}from'@nestjs/common';import{UnleashClientProvider}from'@alpha018/nestjs-unleash-client';@Controller()exportclassAppController{constructor(privatereadonlyunleashProvider:UnleashClientProvider){} @Get('check')checkFeature(){if(this.unleashProvider.isEnabled('my-other-feature')){// Logic for when the feature is enabledreturn'Feature is enabled!';}else{// Logic for when the feature is disabledreturn'Feature is disabled!';}}}
Check out a few resources that may come in handy when working with NestJS:
- Visit theNestJS Documentation to learn more about the framework.
- Visualize your application graph and interact with the NestJS application in real-time usingNestJS Devtools.
- Author -Tomás Alegre
This project isMIT licensed.
About
A Client library to use the Unleash platform in your NestJS Apps to manage the feature flags.
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Contributors2
Uh oh!
There was an error while loading.Please reload this page.