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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@openfeature/nestjs-sdk

v0.1.5-experimental

Published

OpenFeature Nest.js SDK

Downloads

1,144

Readme

OpenFeature is an open specification that provides a vendor-agnostic, community-driven API for feature flagging that works with your favorite feature flag management tool or in-house solution.

Overview

The OpenFeature NestJS SDK is a package that provides a NestJS wrapper for the OpenFeature Server SDK.

Capabilities include:

  • Providing a NestJS global module to simplify OpenFeature configuration and usage within NestJS
  • Setting up logging, event handling, hooks and providers directly when registering the module
  • Injecting feature flags directly into controller route handlers by using decorators
  • Injecting transaction evaluation context for flag evaluations directly from execution context (HTTP header values, client IPs, etc.)
  • Injecting OpenFeature clients into NestJS services and controllers by using decorators

🚀 Quick start

Requirements

  • Node.js version 18+
  • NestJS version 8+

Install

npm

npm install --save @openfeature/nestjs-sdk

yarn

# yarn requires manual installation of the peer dependencies (see below)
yarn add @openfeature/nestjs-sdk @openfeature/server-sdk @openfeature/core

Required peer dependencies

The following list contains the peer dependencies of @openfeature/nestjs-sdk with its expected and compatible versions:

  • @openfeature/server-sdk: >=1.7.5
  • @nestjs/common: ^8.0.0 || ^9.0.0 || ^10.0.0
  • @nestjs/core: ^8.0.0 || ^9.0.0 || ^10.0.0
  • rxjs: ^6.0.0 || ^7.0.0 || ^8.0.0

The minimum required version of @openfeature/server-sdk currently is 1.7.5.

Usage

The example below shows how to use the OpenFeatureModule with OpenFeature's InMemoryProvider.

import { Module } from '@nestjs/common';
import { OpenFeatureModule, InMemoryProvider } from '@openfeature/nestjs-sdk';

@Module({
  imports: [
    OpenFeatureModule.forRoot({
      defaultProvider: new InMemoryProvider({
        testBooleanFlag: {
          defaultVariant: 'default',
          variants: { default: true },
          disabled: false,
        },
      }),
      providers: {
        differentProvider: new InMemoryProvider(),
      },
    }),
  ],
})
export class AppModule {}

With the OpenFeatureModule configured, it's possible to inject flag evaluation details into route handlers like in the following code snippet.

import { Controller, ExecutionContext, Get } from '@nestjs/common';
import { map, Observable } from 'rxjs';
import { BooleanFeatureFlag, EvaluationDetails } from '@openfeature/nestjs-sdk';
import { Request } from 'express';

@Controller()
export class OpenFeatureController {
  @Get('/welcome')
  public async welcome(
    @BooleanFeatureFlag({
      flagKey: 'testBooleanFlag',
      defaultValue: false,
    })
    feature: Observable<EvaluationDetails<boolean>>,
  ) {
    return feature.pipe(
      map((details) =>
        details.value ? 'Welcome to this OpenFeature-enabled NestJS app!' : 'Welcome to this NestJS app!',
      ),
    );
  }
}

It is also possible to inject the default or domain scoped OpenFeature clients into a service via Nest dependency injection system.

import { Injectable } from '@nestjs/common';
import { FeatureClient, Client } from '@openfeature/nestjs-sdk';

@Injectable()
export class OpenFeatureTestService {
  constructor(
    @FeatureClient() private defaultClient: Client,
    @FeatureClient({ domain: 'my-domain' }) private scopedClient: Client,
  ) {}

  public async getBoolean() {
    return await this.defaultClient.getBooleanValue('testBooleanFlag', false);
  }
}

Module additional information

Flag evaluation context injection

Whenever a flag evaluation occurs, context can be provided with information like user e-mail, role, targeting key, etc. in order to trigger specific evaluation rules or logic. The OpenFeatureModule provides a way to configure context for each request using the contextFactory option. The contextFactory is run in a NestJS interceptor scope to configure the evaluation context, and then it is used in every flag evaluation related to this request. By default, the interceptor is configured globally, but it can be changed by setting the useGlobalInterceptor to false. In this case, it is still possible to configure a contextFactory that can be injected into route, module or controller bound interceptors.