feature-management
Version:
Feature Flag Management
88 lines (63 loc) • 2.07 kB
Markdown
# 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 `` decorator.
## Example
### Basic usage
```typescript
('production', false)
('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.
```typescript
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.
```typescript
interface FeatureManagerContext {
readonly email?: string;
}
class AllowUsers {
constructor(readonly emails: readonly string[]) {}
}
(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
```typescript
('production', [new AllowUsers(['john.com', 'doe.com'])])
('staging', true)
class HostReport implements IFeature {}
```
Finally, you can check if feature is enabled using feature manager.
```typescript
const context = {
email: 'john.com'
};
if (await featureManager.isEnabled(HostReport, context)) {
// run this block if enabled
}
```
## Testing
```
npm run test
```
## Credits
- [Ashish K. Poudel](https://github.com/ashishkpoudel)
- [All Contributors](../../contributors)