0.1.6 • Published 1 year ago

features-flagger v0.1.6

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
1 year ago

Flagger

npm.io StackBlitz

Flagger is a core library designed for feature flags. You can easily manage your features in separation, almost every feature can be modular.

Usage

const featureManagerConfig = {
    //Define features
    features: [
        {
            name: 'Feature1',
            description: 'Test feature 1',
            version: '0.1',
            default: false // Inactive by default
        }
    ]
};
const featureManager = new FlaggerFeaturesManager(featureManagerConfig);

await featureManager.loadFeatures(); // Load features and check constraint.

Feature definition

const featureDefinition = {
    name: '', // min 4 letters and max 25 letters
    default: false, // boolean, optional - means, that feature can be active for initial state
    version: string, // min 1 and max 10 letters
    hidden: false, // boolean, optional
    description: '', // string, min 5 letters and max 250 letters
    tags: undefined, // Array<string>, optional
    activators: [], // FlaggerActivator, optional
    constraint: undefined // FlaggerConstraint, optional
};

Load configuration

// Via constructor - internal config
const featureManager = new FlaggerFeaturesManager(featureManagerConfig);

// Via method - internal config
const featureManager = new FlaggerFeaturesManager();
featureManager.loadConfig(featureManagerConfig);

// Via method - external, imported as object config
// In this case constraint should be a string
const featureManager = new FlaggerFeaturesManager();
await featureManager.loadExternalConfig(featureManagerConfig);

Constraints

Constraints are used to activate feature immediately after load itself. You can define this one inside script as singular or FlaggerChainConstraint. Another possibility is string sentence but not every kind of constraint supports this capacity.

Example in js (chaining):

const flagChainConstraint = new FlaggerChainConstraint(new FlaggerOnlineConstraint())
    .and(new FlaggerDateIntervalConstraint(
        {
            startDate: new Date(2019, 9, 6),
            endDate: new Date(2022, 10, 11)
        }
    ));

Example in json:

{
   "features": [
       {
          "name": "SomeFeature",
          "version": "0.0.1",
          "description": "Some existing feature",
          "constraint": "betweenDate('2022-09-01', '2022-10-14') and isOnline"
       }
   ],
  "constraintDeserializers": [
      
  ]
}

Example in js (custom constraint):

const flaggerCustomConstraint = new FlaggerCustomConstraint({
    checker: async () => window.navigator.language.startsWith('en')
});

List constraints to use

  • FlaggerSupportsConstraint - Use feature when browser supports some features.
  • FlaggerOnlineConstraint - Use feature when user is online.
  • FlaggerDateIntervalConstraint - Use feature between date ranges.

Realtime Constraints

Realtime constraint can be used to make feature disposable according to some requirements. It can be usefully especially when your feature must be available only for some conditions.

{
    "name": "OnlineFeature",
    "description": "Feature will be available when user is online",
    "version": "0.14",
    "realtimeConstraint": [
        "whenOnline"
    ]
}

Constraint Deserializers (External config)

Custom Deserializers

externalConfig.json:

{
  "features": [
    {
      "name": "SomeFeature",
      "version": "0.0.2",
      "description": "Real feature :)",
      "constraint": "representativeName('sm')"
    }
  ],
  "constraintDeserializers": [
    "pathToExternalSerializerScript.js"
  ]
}

pathToExternalSerializerScript.js:

import FlaggerCustomConstraint from 'features-flagger';

export default {
    representativeName: 'representativeName',

    deserialize(sm) {
        return new FlaggerCustomConstraint({
            checker: async () => sm === 'sw'
        });
    }  
};