JavaScript SSR API

8 minute readReference

The following information is for the latest version of the JavaScript SSR SDK. If you are running an older version, please check the CloudBees Feature Management - JavaScript changelog for any differences.

The Rox object is the main class to be used when registering your application’s feature flags with CloudBees Feature Management. Define a set of Rox.Flag(), Rox.RoxNumber(), or Rox.RoxString() objects, and supply these to Rox.register() to connect them with your application. Then provide your app key (from the CloudBees Feature Management UI) and configured options, and your feature flags are ready to use.

Example

To use the Rox object, you’ll first need to specify a feature flag container. This defines a set of Flag, RoxNumber, and RoxString objects that can be used to control the features of your application. Once the flags have been defined, simply call the setup with the correct API key and options set.

Registering a container

import {RoxNumber, Flag, RoxString, Rox} from 'rox-ssr'; // create a flags and configuration container should usualy be in a different module|file const DemoContainer = { sayHello: new Flag(), flagA: new Flag(), textColor: new RoxString('red', ['green', 'blue']), textSize: new RoxNumber(15, [20, 40]) }; // register the container, this lets {PRODUCT} knows about the flags and they can be configured in the dashboard Rox.register('DemoApp', DemoContainer);

Classes

Flag

Creates a new Flag (default: false).

Example

To use the Flag object, declare it within a flag container:

displayWelcome: new Flag(); // requireCaptcha flag default value is set to true requireCaptcha: new Flag(true);

Constructor

new Flag(defaultValue)
Parameter Type Description

defaultValue

boolean:= false

Initial state of the flag

Return Value

none

name

Return Value

Type :string - returns the name of this flag (will only work after register)

isEnabled()

Return Value

Type :boolean - returns true when the flag is enabled

isEnabled(context):boolean

Parameter Type Description

context

Object

object to be merged with the global context, the context is used when calculating the custom properties

Return Value

Type :boolean - Returns true when the flag is enabled

RoxString

RoxString is used to create and manage feature flags that determine different predefined string values.

Example

The following code constructs some simple RoxString objects, which can be used to have a flag with string values.

colorsVariant: new RoxString('red', ['green', 'blue', 'red']); languageVariant: new RoxString('english', ['english', 'spanish', 'german']);

Constructor

new RoxString(defaultValue, variations)

Sets the default value, and variations for this RoxString instance.

Parameter Type Description

defaultValue

String

The default value for this feature flag

options

Array<String>

The domain of possible values for this flag

Return Value

none

name

Return Value

Type :string - returns the name of this flag (will be set only after register)

getValue(context):string

Returns the current value of the RoxString, accounting for value overrides.

Parameter Type Description

context

Object

object to be merged with the global context, the context is used when calculating the custom

Return Value

Type: string - the current value of this RoxString object

RoxNumber

RoxNumber is used to create and manage feature flags that determine different predefined number values.

Example

The following code constructs some simple RoxNumber objects, which can be used to have a flag with string values.

textSize: new RoxNumber(12, [18, 26, 42]); numberOfInstances: new RoxNumber(1, [2, 3, 8]);

Constructor

new RoxNumber(defaultValue, variations)

Sets the default value, and variations for this RoxNumber instance.

Parameter Type Description

defaultValue

number

The default value for this feature flag

variations

Array<number>

The domain of possible values for this flag

Return Value

none

name

Return Value

Type :string - returns the name of this flag (will be set only after register)

getValue(context):string

Returns the current value of the RoxNumber, accounting for value overrides.

Parameter Type Description

context

Object

object to be merged with the global context, the context is used when calculating the custom

Return Value

Type: number - the current value of this RoxNumber object

Rox.overrides

Rox.overrides is used to override a flag value on localhost, it is used by developers working on a feature in dev mode and shouldn’t be used in production builds.

It is the base API that is used to show the Flag update flow.

When you override an existing flag value using the Rox.overrides.setOverride method, the SDK will disregard existing configuration coming from the dashboard and will serialize the override on disk. This value will be loaded and override the flag right after you call Rox.setup. To clear the override from the cache you need to call the Rox.overrides.clearOverride method.

Rox.overrides.setOverride

Sets an override value on a specific flag, this function accepts two parameters flag name (flag name including namespace) and desired value (from type String).

This function also saves the override value on the local device disk, so it is "remembered" for when the next SDK is loaded to production.

Parameter Type Description

flagName

String

full flag name including namespace, eg. default.flagName

value

String

The value of a flag, if this is a boolean flag the value should be "true"

Rox.overrides.clearOverride

Clears the override value from the flag (and the disk). After calling setOverride function the override value is serialized on the disk, this function is used to clear this override value.

Parameter Type Description

flagName

String

full flag name including namespace, eg. default.flagName

Rox.overrides.hasOverride

Returns true if a flag has an override

Parameter Type Description

flagName

String

full flag name including namespace, eg. default.flagName

Return Value Type : boolean - true if flag has an override in place

ROX Methods

setup

(static) setup(appKey, options [optional])

Initiate connection with Rox servers for the application identified by the application key. The registered containers will be synced and Rox entities will get the appropriate values.

Example

import {Rox} from 'rox-ssr'; Rox.register('', container); const options = { version: '2.0.0' }; Rox.setup('76aea5dd656a254d125c2a19',options);
Parameter Type Description

appKey

String

application key as appears in Rox dashboard

options

Object

configuration object (optional)

Options

Parameter Type Description

version

String

javascript client version

configurationFetchedHandler

Function

function that is called after data was synced from the CloudBees Feature Management backend

debugLevel

String

SDK verbosity level, accept 'verbose' level

impressionHandler

Function

function that is called every time a flag value gets computed and evaluated on the client

platform

String

custom platform; for cases where you want to override the default platform (Browser), for platforms like tizen, chromecast, etc.

fetchIntervalInSec

Number

configure the interval of fetch configuration call. The default value is 60s and the minimum is 30s

Return Value

Promise that gets executed once the configuration was retrieved from the CloudBees Feature Management backend servers.

register

(static) register(namespace, container)

Register a container of Rox entities by specifying a namespace.

(static) register(container)

Register a container of Rox entities under a string empty namespace.

Parameter Type Description

namespace

String

Container namespace

container

Object

Object literal whose properties are Rox entities

setContext

(static) setContext(context)

Sets the global context of the SDK. The context is used when evaluating custom properties A typical global context will include static identifiers that do not change in the lifetime of the app.

Parameter Type Description

context

Object

The context object

import {Rox} from 'rox-ssr'; Rox.setContext({ role: "api", hostname: "Hello Kiti" })

setCustomStringProperty

(static) setCustomStringProperty(key, value)

Sets a custom property value that can be used when creating target groups.

Parameter Type Description

key

String

The name of the custom property

value

String or Function

The value of the custom property, or a function that accepts context and retrieve a value (see example)

import {Rox} from 'rox-ssr'; // retriving user email via a function and context Rox.setCustomStringProperty("role", function(context){ return context.role; });

setCustomBooleanProperty

(static) setCustomBooleanProperty(key, value)

Sets a custom property value that can be used when creating target groups.

Parameter Type Description

key

String

The name of the custom property

value

Boolean or Function

The value of the custom property, or a function that accepts context and retrieve a value (see example)

// Checking if the user is a tester via a function and ontext Rox.setCustomBooleanProperty("tester", function(context){ return context.user.isTester(); });

setCustomNumberProperty

(static) setCustomNumberProperty(key, value)

Sets a custom property value that can be used when creating target groups.

Parameter Type Description

key

String

The name of the custom property

value

Number or Function

The value of the custom property, or a function that accepts context and retrieve a value (see example)

import {Rox} from 'rox-ssr'; // Calulating the age of the user via a function and context Rox.setCustomNumberProperty("age", function(context){ return context.user.getAge(); });

fetch

(static) fetch()

Fetch() pulls the latest configuration and flag values from CloudBees Feature Management servers.

showOverrides

(static) showOverrides(position [optional])

Rox.showOverrides opens the flag override view, providing a debug UI for the application’s set of feature flags.

Parameter Type Description

position

String

position for the debug menu. The possible values are 'top left', 'top right', 'bottom left', 'bottom right'

Using the impressionHandler option

The impressionHandler function has two useful parameters which can help you decide on further actions.

function impressionHandler(reporting, context) {}

See also Impression handler.

  • First parameter is an object with:

    • name - flag’s name

    • value - flag’s value

    • targeting - flag’s targeting: true/false

  • The second parameter is the context which the call used (global context merged with the calls' context, might be undefined if no context was used)

configurationFetchedHandler

function configurationFetchedHandler(fetcherResult){}

configurationFetchedHandler function is called with one argument from type FetcherResults.

The FetcherResults instance has the following properties:

name

type

description

fetcherStatus

string

Indicates the source of that data that was applied One of the following values:

  • "APPLIED_FROM_EMBEDDED" - data source is embedded inside the html

  • "APPLIED_FROM_CACHE"- data source is from local storage cache

  • "APPLIED_FROM_NETWORK" - data source is network

  • "ERROR_FETCH_FAILED" - data wasn’t received correctly

creationDate

Date

the time this data was created and signed on CloudBees Feature Management servers

hasChanges

Boolean

True if this data is no already available on the client

errorDetails

String

Error string in case of an error

dynamicPropertyRuleHandler

function dynamicPropertyRuleHandler(propName, context){}

The Dynamic Custom Property Generator is called when an explicit custom property definition does not exists on the client side.

If you do not set the setDynamicCustomPropertyRule it will then activate the default function which tries to extract the property value from the context by its name.

(propName, context) => context ? context[propName] : undefined
Parameter Type Description

handler

(propName, context) ⇒ value

The function to be called when the SDK is looking for a property that was not explicitly set by the setCustom`Type`Property functions

Rox.dynamicApi

An alternative of using container with register. This allows to evaluate flags without having a static container. DynamicApi will create flags as if they were registered, including sending them to the dashboard.

See also Dynamic api.

isEnabled

Evaludate a boolean flag

(static) isEnabled(name, defaultValue, context)

name

type

description

name

string

the flag’s name - throws an error if not a string

defaultValue

boolean

the default value to use if no dashboard configurations, or value was set to default on dashboard; throws an error if not a boolean

context

Object

a context to evalute the flag with, will be merged with the global context

value

Evaluate a string flag

(static) value(name, defaultValue, variations, context)

name

type

description

name

string

the flag’s name - throws an error if not a string

defaultValue

number

the default value to use if no dashboard configurations, or value was set to default on dashboard; throws an error if not a string

variations

Array<string>

all alternative values to use on the dashboard (can be changed on the dashboard side)

context

Object

a context to evalute the flag with, will be merged with the global context

getNumber

Evaluate a number flag

(static) getNumber(name, defaultValue, variations, context)
name type description

name

string

the flag’s name - throws an error if not a string

defaultValue

number

the default value to use if no dashboard configurations, or value was set to default on dashboard; throws an error if not a number

variations

Array<number>

all alternative values to use on the dashboard (can be changed on the dashboard side)

context

Object

a context to evalute the flag with, will be merged with the global context