feature-management
TypeScript icon, indicating that this package has built-in type declarations

0.0.26 • Public • Published

Feature Management

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(['john@gmail.com', 'doe@gmail.com'])])
@FeatureFlag('staging', true)
class HostReport implements IFeature {}

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

const context = {
  email: 'john@gmail.com'
};

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

Testing

npm run test

Credits

Package Sidebar

Install

npm i feature-management

Weekly Downloads

33

Version

0.0.26

License

ISC

Unpacked Size

26 kB

Total Files

47

Last publish

Collaborators

  • routeasis