Giter VIP home page Giter VIP logo

nestjs-feature-toggle's Introduction

NestJS Feature Toggle

Dynamic NestJS module to work with feature toggles with ease.

NPM Version Package License NPM Downloads 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.

nestjs-feature-toggle's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.