npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rafaelortegabueno/nestjs-feature-toggle

v2.1.1

Published

Nest - modern, fast, powerful node.js web framework (@jwt)

Downloads

413

Readme

NestJS Feature Toggle

Dynamic NestJS module to work with feature toggles with ease.

codecov

Features

  • Create and configure feature toggles via module configuration;
  • Enable and disable feature toggles in a single HTTP request using its headers;
  • Use your feature toggles by injecting a provider or via TypeScript Decorators.

Installation

$ npm i --save @rafaelortegabueno/nestjs-feature-toggle

Quick Start

Import and configure the module in your application:

// app.module.ts
import {
  DataSourceEnum,
  FeatureToggleModule,
} from '@rafaelortegabueno/nestjs-feature-toggle';

@Module({
  imports: [
    FeatureToggleModule.register({
      dataSource: DataSourceEnum.MODULE_CONFIG,
      featureSettings: []
    })
  ]
})

Add your feature toggles to the featureSettings array:

// app.module.ts

featureSettings: [
  {
    name: 'YOUR_FEATURE_TOGGLE',
    value: false
  }
]

Inject the FeatureToggleService in your class constructor and use isFeatureEnabled() to assert the value of your feature togggles.

// class.ts
export class Class {
  constructor(
    private readonly featureToggleService: FeatureToggleService
  ) {}

  async method(): Promise<string> {
    if (await this.featureToggleService.isFeatureEnabled('YOUR_FEATURE_TOGGLE')) {
      return 'YOUR_FEATURE_TOGGLE is enabled!'
    }
    
    return 'YOUR_FEATURE_TOGGLE is disabled!';
  }
}

Request scoped feature toggles

Request scoped feature toggles allows testing a new feature without changing environment variables or modifying the featureSettings array, so we don't need to deploy the application in order to enable or disable a feature toggle.

Notice: Request scoped feature toggles are disabled by default. It uses the request interceptor, which can cause a small performance loss.

To enable request scoped feature toggles, add the following configuration in your module seetings:

// app.module.ts

@Module({
  imports: [
    FeatureToggleModule.register({
      dataSource: DataSourceEnum.MODULE_CONFIG,
      httpRequestContext: {
        enabled: true,
      },
      featureSettings: [
        {
          name: 'YOUR_FEATURE_TOGGLE',
          value: false,
          acceptHttpRequestContext: true,
        }
      ]
    })
  ]
})

Notice: It is necessary to set acceptHttpRequestContext to true in each request scoped feature toggle.

Enable/disable a request scoped feature toggle

Add the feature toggle to the headers section of your request with the feature_ prefix:

feature_YOUR_FEATURE_TOGGLE = 1

The feature toggle values sent in the headers section will overwrite the previously configured values (at the module settings) only in the current request. Any other requests (without the headers keys) are going to use the module settings values.

Notice: The request interceptor searches for the string feature_ by default. However, it is possible to set a custom prefix.

To set a custom feature toggle prefix, add the following configuration to the httpRequestContext key:

// app.module.ts

httpRequestContext: {
  enabled: true,
  keywordToBeSearchedInHeader: 'custom_prefix_',
}

Enable/disable feature toggles with TypeScript Decorators

It is also possible to use TypeScript Decorators to enable/disable feature toggles without injecting the FeatureToggleService provider. It is specially useful to enabling/disabling access to a whole endpoint, for instance.

To do so, add the FeatureToggleGuard to your module configuration:

// app.module.ts

@Module({
  imports: [
    FeatureToggleModule.register({
      dataSource: DataSourceEnum.MODULE_CONFIG,
      featureSettings: [
        {
          name: 'ALLOW_CAT_CREATION',
          value: true,
        }
      ]
    })
  ],
  providers: [
    {
      provide: APP_GUARD,
      useClass: FeatureToggleGuard,
    },
  ],
})

Then use the FeatureEnabled decorator to assert the feature toggle value.

// cat.controller.ts

@Post()
@FeatureEnabled('ALLOW_CAT_CREATION')
async create(@Body() createCatDto: CreateCatDto) {
  this.catsService.create(createCatDto);
}

License

MIT licensed.