Node JS

Install node JS SDK

You can find the latest version on our github Repository:

https://github.com/switchover-io/node-sdk

Install node JS SDK via npm

npm i switchover-node-sdk

and import it into your project:

const Switchover = require('switchover');

Initialize client

You will find your SDK Key on the environment page. Copy it and use it to initialize the client:

Basic usage:

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));

//...
});

Of course it’s also possible to enable auto-refresh on toggle updates:

const client = Switchover.createClient('<SKD_KEY>', {
    /* Set auto refresh to true, for fetching periodically the toggle status */
    autoRefresh: true,

    /* Set refresh interval, for example 60 seconds */
    refreshInterval: 60,

    onUpdated: ( keys ) => {
        /* updated will be called if some toggle keys are changed */
    }
});

/* Now you can do a initial fetch. It would be also possible to wait for the first update cycle */
client.fetch( () => {
    //...
});

Advanced Options

The SDK supports some advanced options

createClient()

ParametersDescription
Env-KEYyour Env Key, you will git it from the Environment Overview
autoRefreshtrue/false
refreshInterval Set refresh interval, for example 60 seconds, if not set and auto refresh is true, value will be set to 60 secondes
onUpdatedupdated will be called if some toggle keys are changed
const client = Switchover.createClient('<SKD_KEY>', {
       autoRefresh: true,
       refreshInterval: 60,
       onUpdated: ( keys ) => { }
});

Public methods

toggleValue()

ParametersDescription
namethe name of the feature toggle configured in switchover
defaultValueIt will return the given default value:
* – When toggle is INACTIVE
* – When evalution fails
* – When client was not fully initialized
contextthe flag conditions as json input
toggleValue(
	name, 
	defaultValue,
	context = {/** JSON */}
)

refresh()

ParametersDescription
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()