Node.js & browser
The @dev-team-tool/feature-switches-node library works in Node.js services and in the browser. It's written in TypeScript, so types are included, and it has no dependencies.
npm install @dev-team-tool/feature-switches-node
You'll need Node 18 or later (or any runtime with fetch). The source lives on GitHub.
Which client?
There are two clients, one per kind of API key:
createServerClient |
createBrowserClient |
|
|---|---|---|
| Key | Server key (fs_srv_…) |
Client key (fs_cli_…) |
| For | Backend services | Browsers, mobile and desktop apps |
| How | Downloads your rules and works answers out in-process | Asks Dev Team Tools for one user's answers |
| Checking a switch | A function call, no network | Instant, from the last answers fetched |
| Context | Passed to every check | Set once, changed with identify() |
Server keys can read your rules, which may list user or company ids. So never ship a server key to a browser. createBrowserClient will refuse one if you try.
Server
import { createServerClient } from "@dev-team-tool/feature-switches-node";const switches = await createServerClient({ apiKey: process.env.FEATURE_SWITCHES_KEY,});if (switches.isEnabled("newCheckout", { userId: user.id, companyId: user.companyId })) { // new checkout}
Create one client when your service starts, and share it. Every check after that is synchronous, with no network involved.
The client checks for rule changes every 30 seconds. It uses a conditional request, so when nothing's changed, nothing is downloaded.
Attribute reporting
Because a server client works things out locally, Dev Team Tools never sees the contexts you check against. So about once a minute, the client reports which attributes it's seen, with a small sample of their values (at most 20 per attribute). That's how the rule editor knows what to suggest.
If an attribute is personal, turn off Remember values for it on the Attributes page. Or turn reporting off entirely with reportAttributes: false.
Browser
import { createBrowserClient } from "@dev-team-tool/feature-switches-node";const switches = await createBrowserClient({ apiKey: "fs_cli_…", context: { userId: user.id, plan: user.plan },});switches.isEnabled("newCheckout");// When the user changes, e.g. after signing in or upgrading:await switches.identify({ userId: user.id, plan: "pro" });
A browser client asks Dev Team Tools for the answers for its context, then serves isEnabled from those. When you call identify(), the previous answers keep being served until the new ones arrive, so nothing flickers.
It checks for changes every 5 minutes. It doesn't check at all while its tab is hidden, and refreshes as soon as the tab is shown again, so people get current answers whenever they come back. Every open tab is its own client, so these defaults stop a tab that's been left open all day from checking for nothing.
Context
userId is required. Add whatever else your rules check, as strings, numbers, booleans, or lists of strings:
{ userId: "u_123", companyId: "42", plan: "pro", seats: 25, roles: ["admin"] }
A context can have up to 50 attributes. Strings can be up to 1,000 characters, and lists up to 100 items.
It never throws
Network problems never throw or reject. Ever. Feature switches shouldn't be the reason your app falls over.
- If Dev Team Tools can't be reached, the client keeps serving the last switches it loaded.
- If it never loaded any, each check returns its fallback, which is
falseunless you say otherwise.
switches.isEnabled("newCheckout", context, /* fallback */ false); // serverswitches.isEnabled("newCheckout", /* fallback */ false); // browser
createServerClient and createBrowserClient wait up to 3 seconds for the first load, then resolve anyway. The only thing that does throw is a configuration mistake at start-up, like using the wrong kind of key. Better to find that out straight away.
Bootstrapping
If you need the right answers from the very first call, even if Dev Team Tools is down, pass bootstrap:
- Server: a snapshot saved earlier, for example at deploy time, from
GET /sdk/v1/config. - Browser: values your server has already worked out, like
{ newCheckout: true }. Handy if you render pages on the server.
Why is it that value?
When a switch isn't doing what you expect, explain tells you why:
switches.explain("newCheckout", context);// { value: true, reason: "RULE_MATCH", ruleId: "…", version: 12 }
reason |
Meaning |
|---|---|
RULE_MATCH |
A rule matched and gave this value |
ROLLOUT |
A percentage rollout matched; value says whether this context is in it |
DEFAULT |
No rule matched, so the environment's default was used |
NOT_FOUND |
No switch has this key, so the fallback was used |
NOT_READY |
Switches haven't loaded yet, so the fallback was used |
BOOTSTRAP |
(Browser) the bootstrap value, until the first answer arrives |
NOT_FOUND is the one that catches people out. Nine times out of ten it's a typo in the key, or an API key for a different environment.
Reacting to changes
const stop = switches.on("change", (changedKeys) => { console.log("Changed:", changedKeys);});// laterstop();
Server clients report switches whose rules or defaults changed. Browser clients report switches whose values changed for their context.
Options
| Option | Default | |
|---|---|---|
apiKey |
required | The environment's API key |
context |
required (browser) | Who the answers are for |
baseUrl |
https://api.dev-team-tools.com |
|
pollIntervalMs |
30000 server, 300000 browser |
0 turns polling off; call refresh() yourself |
refreshOnFocus |
true |
(Browser) pause polling while the page is hidden, and refresh when it's shown |
reportAttributes |
true |
(Server) report the attributes seen, for suggestions in the rule editor |
requestTimeoutMs |
5000 |
|
initialLoadTimeoutMs |
3000 |
How long create…Client waits for the first load |
onError |
console.warn |
Called when a request fails. The client carries on regardless |
bootstrap |
none | See Bootstrapping |
fetch |
global fetch |
For runtimes without one |
A few other methods:
getAll()returns every switch's value.refresh()checks for changes now.close()stops polling. A closed client keeps answering from whatever it last had.
Found a bug?
Raise it on the GitHub issues page, and we'll take a look.