GuilhermeMLS / nestjs-feature-toggle

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nest Logo

NPM Version Package License NPM Downloads codecov

Description

Dynamic NestJS module that provides facilities in working with Feature Toggles.

This module was built in order to facilitate the implementation of feature management tools without compromising their use in projects.

Features

  • Feature Toggles settings via module configuration
  • Possibility of enabling features via HTTP request header

Installation

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

Quick Start

Register the Module settings in your application:

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

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

To work with a new feature, just add it in the settings:

// app.module.ts

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

In the implementation of your feature, just inject the FeatureToggleService in your constructor and use the "isFeatureEnabled()" method.

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

  async method() {
    if(await this.featureToggleService.isFeatureEnabled('FEATURE_TEST')) {
      return 'FEATURE TEST IMPLEMENTED!'
    }

    return 'FEATURE TEST NOT IMPLEMENTED!';
  }
}

Enabling features via HTTP request header

You might want to test a feature in production without affecting the environment. For this, the possibility of enabling a feature through HTTP request headers was implemented.

Note: This feature is disabled by default, as it uses a request interceptor which can cause a small performance loss.

To enable it, add the configuration in your application:

// app.module.ts

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

Note that it is necessary to enable the functionality so that it is possible to process the configured features, but it is necessary to specify in each feature whether it is allowed to change it via HTTP request.

To enable the FEATURE_TEST feature via request, just send in the header:

feature_test = 1

The value sent in the header will overwrite the configured value and will be valid only for the request in question.

Note: the interceptor searches by default for the string 'feature_' in the header keys.

It is possible to configure a custom string:

// app.module.ts

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

Decorator

Is possible to use a decorator instead of import feature toggle service.

// app.module.ts

@Module({
  imports: [
    FeatureToggleModule.register({
      dataSource: DataSourceEnum.MODULE_CONFIG,
      httpRequestContext: {
        enabled: true,
      },
      featureSettings: [
        {
          name: 'FEATURE_TEST',
          value: false,
          acceptHttpRequestContext: true,
        }
      ]
    })
  ],
  providers: [
    {
      provide: APP_GUARD,
      useClass: FeatureToggleGuard,
    },
  ],
})
// cat.controller.ts

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

License

MIT licensed.

About

License:MIT License


Languages

Language:TypeScript 96.8%Language:JavaScript 3.2%