npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

feature-toggle-test

v0.1.1

Published

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

Readme

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