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

@wisemen/nestjs-feature-flags

v0.0.6

Published

Feature flags for NestJS applications backed by OpenFeature and Go Feature Flag.

Downloads

1,428

Readme

@wisemen/nestjs-feature-flags

Feature flags for NestJS applications backed by OpenFeature and Go Feature Flag.

Overview

This package provides:

  • typed flag definitions via createFlag(...)
  • NestJS module registration through FeatureFlagModule
  • flag evaluation through FeatureFlags
  • request-scoped evaluation context through FeatureFlagContext
  • boolean route guards through RequireFlag(...)
  • config synchronization through FeatureFlags.synchronizeConfig(...)
  • test overrides through FeatureFlagsStub

Define Flags

Define flags in exported *.flag.ts files so they can be discovered by the module flagsGlob.

import { createFlag } from '@wisemen/nestjs-feature-flags'

export const SearchCollectionsFlag = createFlag({
  type: 'boolean',
  defaultValue: true,
  name: 'global_search'
})
import { createFlag } from '@wisemen/nestjs-feature-flags'
import { MailProvider } from '#src/modules/mail/enums/mail-provider.enum.js'

export const MailProviderFlag = createFlag({
  type: 'string',
  enum: MailProvider,
  defaultValue: MailProvider.SCALEWAY,
  name: 'mail_provider'
})

Register The Module

Wrap the package module in a local app module so the rest of the application imports a single feature-flag module.

import { join } from 'node:path'
import { Global, Module } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import {
  EvaluationType,
  FeatureFlagModule as FlagModule,
  type FeatureFlagModuleOptions
} from '@wisemen/nestjs-feature-flags'
import { SyncFeatureFlagConfigModule } from '#src/modules/feature-flag/use-cases/sync-feature-flag-config/sync-feature-flag-config.module.js'

@Global()
@Module({
  imports: [
    SyncFeatureFlagConfigModule,
    FlagModule.forRootAsync({
      inject: [ConfigService],
      useFactory: (cfg: ConfigService): FeatureFlagModuleOptions => {
        const endpoint = cfg.get<string>('GO_FEATURE_FLAG_URI')?.trim()
        const flagsGlob = join(process.cwd(), 'dist', '**', '*.flag.js')

        if (endpoint === undefined) {
          return { flagsGlob }
        }

        return {
          flagsGlob,
          defaultProvider: {
            apiKey: cfg.get<string>('GO_FEATURE_FLAG_API_KEY')?.trim(),
            endpoint,
            evaluationType: EvaluationType.InProcess,
            flagChangePollingIntervalMs: 30_000
          }
        }
      }
    })
  ],
  exports: [FlagModule]
})
export class FeatureFlagModule {}

If defaultProvider is omitted, flag evaluation falls back to the default values defined in code.

Register The TypeORM Entity

If the application synchronizes flag config into the database, include FeatureFlagEntity in the datasource entities.

import { FeatureFlagEntity } from '@wisemen/nestjs-feature-flags'

entities: ['dist/src/**/*.entity.js', FeatureFlagEntity]

Set Request Context

Set the OpenFeature transaction context once in middleware, then evaluate flags later without passing a context object around.

import { Injectable, type NestMiddleware } from '@nestjs/common'
import type { FastifyReply, FastifyRequest } from 'fastify'
import { FeatureFlagContext } from '@wisemen/nestjs-feature-flags'
import { AuthorizationResolver } from '#src/modules/auth/services/authorization-resolver.js'
import { AuthContext } from '#src/modules/auth/auth.context.js'

@Injectable()
export class AuthMiddleware implements NestMiddleware {
  constructor(
    private authContext: AuthContext,
    private flagContext: FeatureFlagContext,
    private authResolver: AuthorizationResolver
  ) {}

  async use(req: FastifyRequest, _res: FastifyReply, next: () => void): Promise<void> {
    const auth = await this.authResolver.fromAuthorization(req.headers.authorization)
    const cb = () => this.flagContext.run({ userUuid: auth.userUuid }, next)

    this.authContext.runWithAuthorization(auth, cb)
  }
}

Evaluate Flags

After the middleware sets the context, inject FeatureFlags and call get(...) directly.

import { ConfigService } from '@nestjs/config'
import { FeatureFlags } from '@wisemen/nestjs-feature-flags'
import { MailProvider } from '#src/modules/mail/enums/mail-provider.enum.js'
import { MailProviderFlag } from './mail-provider.flag.js'

export async function mailClientFactory(
  cfg: ConfigService,
  flags: FeatureFlags
): Promise<MailClient> {
  const provider = await flags.get(MailProviderFlag)

  switch (provider) {
    case MailProvider.SCALEWAY:
      return new ScalewayMailClient(cfg)
    case MailProvider.SEND_GRID:
      return new SendGridMailClient(cfg)
    default:
      exhaustiveCheck(provider)
  }
}

You can also pass an explicit evaluation context to get(...) when needed.

Guard Controllers

import { Controller, Get } from '@nestjs/common'
import { RequireFlag } from '@wisemen/nestjs-feature-flags'
import { SearchCollectionsFlag } from './search-collections.flag.js'

@Controller('search-collections')
export class SearchCollectionsController {
  @Get()
  @RequireFlag(SearchCollectionsFlag)
  async index(): Promise<void> {}
}

RequireFlag(...) only accepts boolean flags.

Synchronize Flag Config

Use FeatureFlags.synchronizeConfig(...) from an app use case or scheduled job to upsert the registered flag definitions into the feature flag store.

import { Injectable } from '@nestjs/common'
import { FeatureFlags } from '@wisemen/nestjs-feature-flags'
import { DataSource } from 'typeorm'

@Injectable()
export class SyncFeatureFlagConfigUseCase {
  constructor(
    private dataSource: DataSource,
    private flags: FeatureFlags
  ) {}

  async execute(): Promise<void> {
    await this.flags.synchronizeConfig(this.dataSource)
  }
}

Test Overrides

Create one FeatureFlagsStub from the Nest application container and expose it through test setup.

import { FeatureFlags, FeatureFlagsStub } from '@wisemen/nestjs-feature-flags'

export class TestSetup {
  private flagsStub: FeatureFlagsStub

  private async initialize(): Promise<void> {
    const flags = this.app.get(FeatureFlags, { strict: false })
    this.flagsStub = new FeatureFlagsStub(flags)
  }

  get flags(): FeatureFlagsStub {
    return this.flagsStub
  }
}
setup.flags.mockFlag(SearchCollectionsFlag, true)
setup.flags.mockFlag(MailProviderFlag, MailProvider.SEND_GRID)