osff-js-sdk
v1.0.7
Published
This is a client-side api to compliment a feature-flagging backend service that I have made as part of a personal project. This package is not recommended for production-level/enterprise environments.
Downloads
9
Readme
OSFT Client API
Javascript SDK for opensourcefeaturetoggles.com
Other Repositories that are a part of this project
React SDK
Admin Website
Node Backend
How to Use
1. Installations
npm install osff-js-sdk
# or
yarn add osff-js-sdk
2. Initialize the SDK
Somewhere in your application, instantiate the FeaureToggleClient with your apiKey and preferred refreshRate (in seconds). Then call start to send the inital request for flags and start pinging the server for subsequent updates.
import FeatureToggleClient from "osff-js-sdk"
const client = new FeatureToggleClient({
apiKey : 'YOUR-API-KEY',
refreshRate : '5s',
})
// Send initial request and continue pinging server for updates
client.start()
3. Let the client synchronize
You should wait for the client's ready
event to be fired before attempting to check for active features on the client. Attempting to check for flags before the client is initialized could lead to the consumer reading invalid flag data.
client.addEventListener('ready', () => {
if (client.getFlag('feature.variable')) {
console.log('feature.variable is enabled!')
}
else {
console.log('feature.variable is disabled!')
}
})
Listening for events
The client emits 4 events.
ready
- The client has loaded the initial flags from the server and is ready to serve up to date flagsupdate
- The server has notified the client that there has been an update to one or more of the flagserror
- The client has encountered an error while attmepting to retrieve data from the serverend
- The client has been turned off and will no longer attempt to fetch data from the server. Whatever data was received in its final request before the end event is emitted is the data that the client will use until it is reconnected to the server.
The FeatureToggleClient extends the EventTarget class, which means that you can listen to events by using client.addEventListener('event-name', callBack).