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

@prefab-cloud/prefab-cloud-js

v0.4.2

Published

Feature Flags & Dynamic Configuration as a Service

Downloads

8,639

Readme

prefab-cloud-js

A client for Prefab

Installation

npm install @prefab-cloud/prefab-cloud-js or yarn add @prefab-cloud/prefab-cloud-js

If you'd prefer to use the standalone <script> tag approach, we recommend using jsDelivr for a minified/bundled version.

Usage in your app

Initialize prefab with your api key and a Context for the current user/visitor/device/request:

import { prefab, Context } from "@prefab-cloud/prefab-cloud-js";

const options = {
  apiKey: "1234",
  context: new Context({
    user: {
      email: "[email protected]",
    },
    device: { mobile: true },
  }),
};
await prefab.init(options);
// `prefab` is available globally on the window object
// `Context` is available globally as `window.prefabNamespace.Context`
const options = {
  apiKey: "1234",
  context: new prefabNamespace.Context({
    user: {
      email: "[email protected]",
    },
    device: { mobile: true },
  }),
};

prefab.init(options).then(() => {
  console.log(options);
  console.log("test-flag is " + prefab.get("test-flag"));

  console.log("ex1-copywrite " + prefab.get("ex1-copywrite"));
  $(".copywrite").text(prefab.get("ex1-copywrite"));
});

Now you can use prefab's config and feature flag evaluation, e.g.

if (prefab.isEnabled('cool-feature') {
  // ...
}

setTimeout(ping, prefab.get('ping-delay'));

Here's an explanation of each property

| property | example | purpose | | --------------- | ------------------------------------ | -------------------------------------------------------------------------------------------- | | isEnabled | prefab.isEnabled("new-logo") | returns a boolean (default false) if a feature is enabled based on the current context | | get | prefab.get('retry-count') | returns the value of a flag or config evaluated in the current context | | getDuration | prefab.getDuration('http.timeout') | returns a duration object {seconds: number, ms: number} | | loaded | if (prefab.loaded) { ... } | a boolean indicating whether prefab content has loaded | | shouldLog | if (prefab.shouldLog(...)) { | returns a boolean indicating whether the proposed log level is valid for the current context | | poll | prefab.poll({frequencyInMs}) | starts polling every frequencyInMs ms. | | stopPolling | prefab.stopPolling() | stops the polling process | | context | prefab.context | get the current context (after init()). | | updateContext | prefab.updateContext(newContext) | update the context and refetch. Pass false as a second argument to skip refetching |

shouldLog()

shouldLog allows you to implement dynamic logging. It takes the following properties:

| property | type | example | case-sensitive | | -------------- | ------ | --------------------- | -------------- | | loggerName | string | my.corp.widgets.modal | Yes | | desiredLevel | string | INFO | No | | defaultLevel | string | ERROR | No |

If you've configured a level value for loggerName (or a parent in the dot-notation hierarchy like "my.corp.widgets") then that value will be used for comparison against the desiredLevel. If no configured level is found in the hierarchy for loggerName then the provided defaultLevel will be compared against desiredLevel.

If desiredLevel is greater than or equal to the comparison severity, then shouldLog returns true. If the desiredLevel is less than the comparison severity, then shouldLog will return false.

Example usage:

const desiredLevel = "info";
const defaultLevel = "error";
const loggerName = "my.corp.widgets.modal";

if (shouldLog({ loggerName, desiredLevel, defaultLevel })) {
  console.info("...");
}

If no log level value is configured in Prefab for "my.corp.widgets.modal" or higher in the hierarchy, then the console.info will not happen. If the value is configured and is INFO or more verbose, the console.info will happen.

poll()

After prefab.init(), you can start polling. Polling uses the context you defined in init by default. You can update the context for future polling by setting it on the prefab object.

// some time after init
prefab.poll({frequencyInMs: 300000})

// we're now polling with the context used from `init`

// later, perhaps after a visitor logs in and now you have the context of their current user
prefab.context = new Context({...prefab.context, user: { email: user.email, key: user.trackingId })

// future polling will use the new context

Usage in your test suite

In your test suite, you probably want to skip the prefab.init altogether and instead use prefab.setConfig to set up your test state.

it("shows the turbo button when the feature is enabled", () => {
  prefab.setConfig({
    turbo: true,
    defaultMediaCount: 3,
  });

  const rendered = new MyComponent().render();

  expect(rendered).toMatch(/Enable Turbo/);
  expect(rendered).toMatch(/Media Count: 3/);
});