Javascript Client Side SDK
Install plain Javascript
You can find the latest version on our github Repository:
https://github.com/switchover-io/js-sdk
Just include Switchover as script tag
<html>
<head>
...
<script src="https://cdn.jsdelivr.net/npm/switchover-js-sdk@latest/dist/switchover.min.js"></script>
</head>
After that, just use our SDK by
const client = Switchover.createClient('<SKD_KEY>');
/* fetch toggles from server/cache */
client.fetch( () => {
/* evaluate the toggle value, provide a default value if evalutation fails */
const value = client.toggleValue('<YOUR_TOGGLE>', false));
//...
});
Using NPM
You can find the latest version on our github Repository:
https://github.com/switchover-io/js-sdk
Just install our sdk using:
npm i switchover-js-sdk
After that, import our sdk
import * as Switchover from 'switchover'
And initialize the client for using:
const client = Switchover.createClient('<SKD_KEY>');
/* fetch toggles from server/cache */
client.fetch( () => {
/* evaluate the toggle value, provide a default value if evalutation fails */
const value = client.toggleValue('<YOUR_TOGGLE>', false));
//...
});
Not sure what to do next? We have a step by step guide in our Learn how to use our SDKs section.
Advanced Options
The SDK supports some advanced options
createClient()
Parameters | Description |
Env-KEY | your Env Key, you will git it from the Environment Overview |
autoRefresh | true/false |
refreshInterval | Set refresh interval, for example 60 seconds, if not set and auto refresh is true, value will be set to 60 secondes |
onUpdated | updated will be called if some toggle keys are changed |
const client = Switchover.createClient('<SKD_KEY>', {
autoRefresh: true,
refreshInterval: 60,
onUpdated: ( keys ) => { }
});
Public methods
toggleValue()
Parameters | Description |
name | the name of the feature toggle configured in switchover |
defaultValue | It will return the given default value: * – When toggle is INACTIVE * – When evalution fails * – When client was not fully initialized |
context | the flag conditions as json input |
toggleValue(
name,
defaultValue,
context = {/** JSON */}
)
refresh()
Parameters | Description |
keys: string[] | Manually refreshes toggles. If toggles were updated, callback will hold the changed toggle keys. * If nothing has changed, keys are null. |
refresh(cb(keys: string[]))
refreshAsyc()
refreshAsync() : Promise<string[]>
initPolling()
Re-) Starts auto refresh when you enabled the option. If default interval is not set, interval will be 60s
initPolling()
stopPolling()
Stops auto-refresh. You can start againt with startPolling()
stopPolling()
getToggleKeys()
Get all toggle keys, which currently loaded
getToggleKeys()