The flipt-client-browser
library contains the JavaScript/TypeScript source code for the Flipt client-side evaluation client for the browser.
npm install @flipt-io/flipt-client-browser
In your JavaScript/Typescript code you can import this client and use it as so:
import { FliptEvaluationClient } from '@flipt-io/flipt-client-browser';
// namespace is the first positional argument and is optional here and will have a value of "default" if not specified.
// options is the second positional argument and is also optional, the structure is:
// {
// "url": "http://localhost:8080",
// "authentication": {
// "clientToken": "secret"
// }
// }
//
const fliptEvaluationClient = await FliptEvaluationClient.init('default', {
url: 'http://localhost:8080',
authentication: {
clientToken
}
});
const variant = fliptEvaluationClient.evaluateVariant('flag1', 'someentity', {
fizz: 'buzz'
});
console.log(variant);
The FliptEvaluationClient
constructor accepts two optional arguments:
namespace
: The namespace to fetch flag state from. If not provided, the client will default to thedefault
namespace.options
: An instance of theClientOptions
type that supports several options for the client. The structure is:url
: The URL of the upstream Flipt instance. If not provided, the client will default tohttp://localhost:8080
.authentication
: The authentication strategy to use when communicating with the upstream Flipt instance. If not provided, the client will default to no authentication. See the Authentication section for more information.reference
: The reference to use when fetching flag state. If not provided, reference will not be used.fetcher
: An implementation of a fetcher interface to use when requesting flag state. If not provided, a default fetcher using the browser'sfetch
API will be used.
The FliptEvaluationClient
supports the following authentication strategies:
- No Authentication (default)
- Client Token Authentication
- JWT Authentication
The FliptEvaluationClient
supports custom fetchers. This allows you to fetch flag state from a custom source or override HTTP headers.
The fetcher can be passed in as an argument to the FliptEvaluationClient
initializer function.
const fliptEvaluationClient = await FliptEvaluationClient.init('default', {
url: 'http://localhost:8080',
authentication: {
clientToken
},
fetcher: customFetcher
});
The fetcher is a function that takes an optional IFetcherOpts
argument and returns a Promise
that resolves to a Response
object.
The FliptEvaluationClient
class pulls flag state from the Flipt instance at the url
provided in the options
object on instantiation.
To update the flag state, you can call the refresh
method on the FliptEvaluationClient
class.
Note
The refresh
method returns a boolean indicating whether the flag state changed.
// Refresh the flag state
let changed = await fliptEvaluationClient.refresh();
if (changed) {
// Do something
}
The default fetcher uses ETag HTTP headers to reduce the number of requests to the Flipt instance. This is useful in scenarios where the flag state is not frequently updated and you want to reduce the load on the server.
To disable ETag support, you can implement a custom fetcher that does not use ETags.
This library uses a WebAssembly (WASM) layer to interact with the Flipt server. It is written in Rust and exposes a JavaScript API using the wasm-bindgen
and wasm-pack
tools. We wrap the built WASM layer in a JavaScript API to make it easier to use in a browser environment.
npm run build
npm install
npm test
Contributions are welcome! Please feel free to open an issue or submit a Pull Request.
This project is licensed under the MIT License.