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

0.1.1 • Public • Published

Feature Toggle

A feature toggle library for enabling or disabling features in your application based on configuration.

Installation

npm install feature-toggle

Initialization

To initialize the feature toggle service, you need to provide a configuration source. This can be a URL, a file path, or a configuration object.

import { FeatureToggleService } from './service/feature-toggle.service.js';

const configSource = 'path/to/config.json'; // or a URL or a configuration object
const options = {
    cacheDuration: 600000, // 10 minutes
    headers: {
        'Authorization': 'Bearer token'
    },
    errorHandler: (error) => console.error(error)
};

const service = FeatureToggleService.getInstance();
await service.initialize(configSource, options);

Usage

Middleware

Use the middleware to protect routes based on feature toggles.

import express from 'express';
import { featureCheckMiddleware } from './middleware/feature-toggle.middleware.js';

const app = express();

app.use('/api/feature', featureCheckMiddleware('moduleName', 'submoduleName', 'featureName'), (req, res) => {
    res.send('Feature is enabled');
});

app.listen(3000, () => {
    console.log('Server is running on port 3000');
});

React Hook

Use the React hook to check feature toggles in functional components.

import React from 'react';
import { useFeatureToggle } from './hook/useFeatureToggle.js';

const configSource = 'path/to/config.json';

const MyComponent = () => {
    const { error, loading } = useFeatureToggle(configSource);

    if (loading) return <div>Loading...</div>;
    if (error) return <div>Error: {error.message}</div>;

    return <div>Feature Toggle Initialized</div>;
};

React Component

Use the React component to conditionally render based on feature toggles.

import React from 'react';
import { FeatureToggle } from './components/FeatureToggle.js';

const MyComponent = () => (
    <FeatureToggle moduleName="moduleName" submoduleName="submoduleName" featureName="featureName" fallback={<div>Feature Disabled</div>}>
        <div>Feature Enabled</div>
    </FeatureToggle>
);

API Reference

FeatureToggleService

getInstance()

Returns the singleton instance of the FeatureToggleService.

initialize(configSource: string | AppConfig, options?: FeatureToggleOptions): Promise<void>

Initializes the feature toggle service with the provided configuration source and options.

isFeatureEnabled(moduleName: string, submoduleName: string, featureName?: string): boolean

Checks if a feature is enabled based on the module, submodule, and feature name.

setConfig(config: AppConfig): void

Sets the configuration manually.

getConfig(): AppConfig

Returns the current configuration.

Middleware

featureCheckMiddleware(moduleName: string, submoduleName: string, featureName?: string)

Returns an Express middleware function that checks if a feature is enabled.

React Hook

useFeatureToggle(configSource: string | AppConfig, options?: FeatureToggleOptions)

A React hook that initializes the feature toggle service and returns the loading and error state.

React Component

FeatureToggle

A React component that conditionally renders its children based on the feature toggle state.

Props:

  • moduleName: The name of the module.
  • submoduleName: The name of the submodule.
  • featureName?: The name of the feature (optional).
  • children: The content to render if the feature is enabled.
  • fallback?: The content to render if the feature is disabled (optional).

Configuration Format

The configuration should follow this format:

{
    "modules": {
        "moduleName": {
            "enabled": true,
            "submodules": {
                "submoduleName": {
                    "enabled": true,
                    "features": {
                        "featureName": true
                    }
                }
            }
        }
    }
}

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i feature-toggle-test

Weekly Downloads

3

Version

0.1.1

License

ISC

Unpacked Size

19.6 kB

Total Files

19

Last publish

Collaborators

  • abhijeet-chattar