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

@uptechworks/uptech-growthbook-sdk-typescript

v0.2.1

Published

A thin wrapper around GrowthBook's Javascript SDK to support automated testing

Downloads

180

Readme

Uptech GrowthBook SDK Flutter Wrapper

This project is a thin wrapper around the GrowthBook Flutter SDK so that we can use the GrowthBook service to manage feature toggles while also being able to manage toggle states properly within automated test suites.

Development

Get dependencies

npm i

Run tests

npm test

Setup

To set this up you need an account on GrowthBook or to be hosting it yourself.

Once you have an account and have setup your Project and the environments the way you want. You need to get the read-only API key for each of the environments and configure them in your app per environment.

Then you need to setup a singleton in your app to to house the shared instance of the UptechGrowthBookTypescriptWrapper. Note: This is what needs the apiKeyUrl that should come from your environment config and not be hard coded in your app. This might look as follows maybe in a file called, src/togls.ts. It is really up to you how you do this. This is just a suggestion.

class Togls extends UptechGrowthBookTypescriptWrapper {
  // In GrowthBook dashboard > SDK Endpoints url: https://cdn.growthbook.io/api/features/dev_Y1WwxOm9sDnIsO1DLvwJk76z3ribr3VoiTsaOs?project=prj_29g61lbb6s8290
  // Include the entire url
  static instance: Togls = new Togls('your-api-key-url');
}

Once you have the Togls class you have two options for initializing the library. One you can do in main.ts as follows.

import express from "express";
const app = express();
const port = 8080; // default port to listen

// ...

app.listen( port, () => {
    Togls.instance.init({
		seeds: new Map([['example-toggle-higher-fee', false]]),
	});
});

// ...

In the above examples we provide seeds which are values that are used to evaulate the toggles prior to it having fetched the toggles from the remote server. In the happy path this window of time is extremely small to the point where you won't even notice these values. However, in the case that user launched the app and the network connection is not working or the GrowthBook service was down then the toggles would evaluate to the value specified in the seeds.

Adding overrides

If you want to overrides, you have two options. First, you can add them to the init call:

// ...

app.listen( port, () => {
    Togls.instance.init({
		seeds:  new Map([['example-toggle-higher-fee', false]]),
    overrides:  new Map([['example-toggle-higher-fee', true]]),
	});
});

// ...

Alternatively, you can add override values to your .env file for specific features in Growthbook. With this method, overrides do not need to be passed into the init method. To ensure that your overrides perform as intended, please use UPPER_CASE_SNAKE_CASE with TOGL_ appended to the beginning Example: example-toggle-higher-fee becomes TOGL_EXAMPLE_TOGGLE_HIGHER_FEE

Adding attributes

If you want to add attributes at itialization, you can add values into the attributes key in the init function. This is useful if, for instance, you are only allowing certain versions of your app to access a feature.

// ...

app.listen( port, () => {
    const version = getVersionNumber(); // this is a method you create and provide the logic for
    Togls.instance.init({
		  seeds:  new Map([['example-toggle-higher-fee', false]]),
      overrides:  new Map([['example-toggle-higher-fee', true]]),
      attributes: attributes: new Map([['version', version]]),
	});
});

// ...

Usage

Once you have it setup you are ready to start using it. The following examples assume that you followed the suggestion above in terms of creating the singleton. If you did something different you should still be able to use these as rough examples of how to evaluate a feature and how to control toggles in automated tests.

Evaluate Feature

import { Togls } from 'yourproject/togls.ts';

sampleApplyFee(amount: number): number {
  if (Togls.instance.isOn('example-toggle-higher-fee')) {
    return amount + 20;
  } else {
    return amount + 10;
  }
}

Evaluate Feature value

import { Togls } from 'yourproject/togls.ts';

sampleApplyFee(amount: number): number {
  // Note: feature value can be of any type
  const featureValue = Togls.instance.value('example-toggle-higher-fee');
  if (featureValue != 0) {
    return amount + 20;
  } else {
    return amount + 10;
  }
}

Set attributes

Additional attributes can be set after initialization. This is a common use case in which an id attribute is set after user login (useful for canary testing).

import { Togls } from 'yourproject/togls.ts';
sampleLogIn(): void {
  const userId = await login(); // Fake method that logs in user and gets user id
  Togls.instance.setAttributes(new Map([['id', userId]]));
}

Control toggles in automated tests

import { sampleApplyFee } from 'yourproject/toggle_samples.ts';
import { Togls } from 'yourproject/togls.ts';

  describe('Toggle Samples', () {
    describe('sampleApplyFee', () {
      describe('when example-toggle-higher-fee is off', () {
        beforeEach(() {
          Togls.instance
              .initForTests({
                seeds:  new Map([['example-toggle-higher-fee', false]]),
              });
        });

       it('returns amount with fee of 10 added', () {
          const res = sampleApplyFee(2);
          expect(res).toEqual(12);
        });
      });

      describe('when example-toggle-higher-fee is on', () {
        beforeEach(() {
          Togls.instance.initForTests({
            seeds:  new Map([['example-toggle-higher-fee', true]]),
          });
        });

        it('returns amount with fee of 20 added', () {
          const res = sampleApplyFee(2);
          expect(res).toEqual(22);
        });
      });
    });
  });