Skip to content

ashishkpoudel/feature-management

Repository files navigation

Feature Management

Status: Experimental

Feature Management (also called feature toggle, feature flag) makes it easier to enable or disable feature based on specified configuration.

Installation

You can install the package via npm:

npm install feature-management

Usage

Feature must be declared as a class. Feature flag configuration is then applied by using @FeatureFlag decorator.

Example

Basic usage

@FeatureFlag('production', false)
@FeatureFlag('staging', true)
class HostReport implements IFeature {}

Above we've declared a feature called HostReport with feature flag configuration applied for multiple environment. Last step is to initialize feature manager and check if specified feature is enabled or not based on current environment.

if (await featureManager.isEnabled(HostReport)) {
  // run this block if enabled
}

Advance usage with strategies

Strategy helps you enable a feature based on condition defined. You can assign multiple strategies to a feature.

interface FeatureManagerContext {
  readonly email?: string;
}

class AllowUsers {
  constructor(readonly emails: readonly string[]) {}
}

@StrategyHandler(AllowUsers)
class AllowUsersHandler implements IStrategyHandler {
  async evaluate(strategy: AllowUsers, context: FeatureManagerContext) {
    if (!context?.email) {
      throw new Error('AllowUsers Strategy requires param: email');
    }

    return strategy.emails.includes(context.email);
  }
}

Assign strategy to a feature

@FeatureFlag('production', [new AllowUsers(['[email protected]', '[email protected]'])])
@FeatureFlag('staging', true)
class HostReport implements IFeature {}

Finally, you can check if feature is enabled using feature manager.

const context = {
  email: '[email protected]'
};

if (await featureManager.isEnabled(HostReport, context)) {
  // run this block if enabled
}

Testing

npm run test

Credits

About

Feature Management aka feature toggle, feature flag

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published