Skip to content

@sourceloop/feature-toggle / Exports

@sourceloop/feature-toggle

LoopBack

A simple loopback-next extension for checking the disabled features. Here a new decorator is introduced it has a simplistic approach to check if a particular feature is allowed or not to that user.

Working and Flow

This extension provides a method level decorator @featureFlag that takes the name of the feature that needs to be checked as metadata and verifies if that particular feature is allowed or not. What it expects is that a list of disabled features is saved for the current user of the type IAuthUserWithDisabledFeat and compares that with the one passed in the metadata of the decorator. The metadata also accepts an optional options that has a handler name parameter. If that handler name is provided that using an extendion point HandlerService appropriate handler is called. All the handlers must implement FeatureHandler interface.

Only feature check

 @featureFlag({featureKey: 'feature_key'})

If you want to skip the check:

@featureFlag({featureKey: '*'})

Multiple feature check with operator

1
2
3
4
5
6
@featureFlag({
    featureKey: ['feature_key1', 'feature_key2'],
    options: {
      operator: 'AND' | 'OR'
    },
  })

Feature Check plus handler call

1
2
3
4
5
6
@featureFlag({
    featureKey: 'feature_key',
    options: {
      handler: 'handler_name',
    },
  })

This particular handler_name will be matched with the one in handler

1
2
3
4
5
6
7
8
@injectable(asFeatureHandler)
export class MyHandler implements FeatureHandler {
  handlerName = 'handler_name';

  handle(): void {
    // your logic here
  }
}

A good practice is to keep all feature strings in a separate enum file like this.

1
2
3
4
5
export enum FEATUREKEY {
  CALENDAR = '1',
  CHAT = '2',
  CONTRACT = '3',
}

Install

npm install @sourceloop/feature-toggle

Basic Use

Setup

In order to use this component into your application follow the easy steps given below.

  • While authentication save the list of disabled features for that particular user. Like this
authUser.disabledFeatures = ['1', '3'];
  • Add the FeatureToggleComponent to your Loopback4 Application (in application.ts) where you need to check the features availability.
1
2
3
4
5
// import the FeatureToggleComponent
import {FeatureToggleComponent} from '@sourceloop/feature-toggle';

// add Component for FeatureToggle
this.component(FeatureToggleComponent);
  • Adding handlers to the extension points
this.add(createBindingFromClass(MyHandler));
  • Then add the decorator over all the APIs where feature needs to be checked. As shown above.

Feedback

If you've noticed a bug or have a question or have a feature request, search the issue tracker to see if someone else in the community has already created a ticket. If not, go ahead and make one! All feature requests are welcome. Implementation time may vary. Feel free to contribute the same, if you can. If you think this extension is useful, please star it. Appreciation really helps in keeping this project alive.

Contributing

Please read CONTRIBUTING.md for details on the process for submitting pull requests to us.

Code of conduct

Code of conduct guidelines here.

License

MIT