@useboolean/boolean-js
v0.1.0
Published
![Boolean logo - real-time feature flags](https://avatars0.githubusercontent.com/u/53407144?s=128&v=4)
Downloads
1
Readme
Boolean
Feature management for modern devops teams by Boolean.
This is a generic JavaScript library to make it easy to create libraries for React, Vue, Node.js etc. Usually you shouldn't use this library directly unless you know what you're doing. :)
npm i @useboolean/boolean-js
Features
- Percentage-based rollout
- User targeting & segmentation
- Turn off features instantly
- Privacy-first
- Audit log
- SSO / SAML
⚠️ Create an account at Boolean. There is a free plan available.
Quick start
Call makeMakeTransport
and makeMakeBoolean
to configure the setup functions. Use those functions to actually create a Boolean instance.
const makeTransport = makeTransport({
WebSocket,
request,
});
const makeBoolean = makeMakeBoolean({
makeTransport,
});
const boolean = makeBoolean(`api key here`, {
// You can configure three types of transports: stream, fetch and none.
transport: {
type: `stream`,
timeout: 2000,
};
// Used when no data could be fetched for whatever reason.
initialData: initialData,
// In case you want to use your own cache system.
cache: cache,
});
const { isEnabled } = await boolean.evaluate(`my-feature-1`);
makeBoolean
Transports
Stream Transport
This transport is intended to be used when you want a persistent connection. If you want to fetch feature flag data once use the fetch transport instead.
Sets up a WebSocket connection. The WebSocket constructor must be passed in the makeMakeTransport
function. This makes it easier to use this library in browser or Node.js.
This transport automatically reconnects when an error occurs using a simple backoff algorithm.
When evaluating a feature and a connection has not been estabished yet it immediately returns a result. Even if the data is not up-to-date yet. You can wait for up-to-date data by calling await makeBoolean(..).promise()
, but it only waits until the specified timeout. If no fresh data is in it either falls back to the cache, if specified, or the initial data.
⚠️ It's highly recommended to always specify initialData so you have a fall back when things go down, are slow, or otherwise experience issues.
const boolean = makeBoolean(`api key here`, {
transport: {
type: `stream`,
timeout: 2000,
};
initialData,
});
Fetch Transport
The fetch transport simply fetches data over http without any retrying. If the request did not respond within the specified timeout, the promise is rejected. Catch the error if you want to continue evaluating but using the fallback.
If you are evaluating a feature before the request has been finished, Boolean falls back to your cache. If retrieving data from the cache has not been finished yet, it falls back to the initial data.
const boolean = makeBoolean(`api key here`, {
transport: {
type: `fetch`,
timeout: 2000,
};
initialData,
});
None Transport
You can disable the transport all together by specifying the none
transport. Boolean will fall back to the cache and the initial data.
This may be useful in serverless environments where you use the cache to retrieve fresh feature data. And you use webhooks to keep your cache up to date.
const boolean = makeBoolean(`api key here`, {
transport: {
type: `none`,
};
initialData,
});
Cache
You can provide your own cache which Boolean will use to fetch initial feature data.
Get
const cache = {
get(apiKey) {
// Just retrieve the blob by apiKey you cached using the cache#put operation
},
};
Put
const cache = {
put(apiKey, data) {
// Store data (which is a string) by apiKey.
},
};
📨 If you have any feedback or just want to say hi you can send us a ping. Over & out!