Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A Client library to use the Unleash platform in your NestJS Apps to manage the feature flags.

License

NotificationsYou must be signed in to change notification settings

Alpha018/nestjs-unleash-client

Repository files navigation

Nest Logo

A NestJS module for Unleash, providing feature flagging with a global Guard and decorators.

Built with NestJS

Table of Contents

⚠️ Important: Starting from this version, the minimum required Node.js version is20.

Installation

$ npm i @alpha018/nestjs-unleash-client

Usage

Import The Module

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.

Static Registration

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{}

Async Registration

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{}

Parameter Options

Options forforRoot andforRootAsync:

ParameterTypeRequiredDescription
configUnleashConfigYesThe configuration object for theunleash-client. See options below.
isGlobalbooleanNoIftrue, 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 ParameterTypeRequiredDescription
urlstringYesThe URL of your Unleash server API.
appNamestringYesThe name of your application.
customHeadersobjectNoCustom headers to be sent to the Unleash API. UseAuthorization for the API key generated by the Unleash panel.
...anyNoAny other valid option from theofficial unleash-client-node documentation.

Protecting a Route with a Feature Toggle

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.';}}

Injecting the Unleash Client

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!';}}}

Resources

Check out a few resources that may come in handy when working with NestJS:

Stay in touch

License

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

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp