0.0.26 • Published 2 years ago

feature-management v0.0.26

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

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

0.0.26

2 years ago

0.0.25

2 years ago

0.0.24

2 years ago

0.0.23

2 years ago

0.0.22

2 years ago

0.0.21

2 years ago

0.0.20

2 years ago

0.0.19

2 years ago

0.0.18

2 years ago

0.0.17

2 years ago

0.0.16

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.11

2 years ago

0.0.12

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago