JavaScript SSR SDK reference

8 minute readReference

The following information is for the latest SDK version (6.x). The CloudBees platform requires that your installed SDK version be at least 6.x. Please install the latest SDK by following the instructions in the platform UI or in the SDK installation documentation.

Any updates to version 6.x are noted in the platform changelog.

The Rox object is the main class to be used when registering your application’s feature flags in the platform. 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 platform UI and configured options, and your feature flags are ready to use.

To use the Rox object, you must 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, call the setup with the correct API key and options set.

Register a container

import {RoxNumber, Flag, RoxString, Rox} from 'rox-ssr'; // Creates a flag and a configuration container. CloudBees recommends that this be located 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 alerts the platform about the flags so they can be configured in the UI. Rox.register('DemoApp', DemoContainer);

Class Flag

Creates a new Flag (default: false).

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 is none.

name

Returns the name of this flag (only works after registering).

isEnabled()

Returns Boolean type 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 custom properties).

Returns Boolean type true when the flag is enabled.

Class RoxString

RoxString is a feature flag object that accepts a list of possible string values, and a default value. These values are used to select new values for the feature, and they can be overridden via the UI.

The following code constructs some simple RoxString objects, for 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.

The return value is none.

name

Returns the current String name of this flag (set only after registered).

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 custom properties).

Returns the current String value of this RoxString object.

Class RoxNumber

RoxNumber is a feature flag object that accepts a list of possible number values, and a default value. Use this list to select new values for the feature, and you can override them via the UI.

The following code constructs some simple RoxNumber objects for 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.

The return value is none.

name

Returns the name of this flag (set only after registered).

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 custom properties).

Rox.overrides

Rox.overrides is used to override a flag value on localhost. Use this only in dev mode, not in production builds.

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

If you override an existing flag value using the Rox.overrides.setOverride method, the SDK disregards the existing configuration coming from the UI and serializes the override on disk. This value is loaded and overrides the flag after you call Rox.setup. Call the Rox.overrides.clearOverride method to clear the override from the cache.

Rox.overrides.setOverride

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

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

Parameter Type Description

flagName

String

Full flag name including the namespace, for example, myNamespace.flagName.

value

String

The value of a flag. If this is a Boolean flag, the value must be "true" or "false".

Rox.overrides.clearOverride

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

Parameter Type Description

flagName

String

Full flag name including the namespace, for example, myNamespace.flagName.

Rox.overrides.hasOverride

Returns true if the flag has an override in place.

Parameter Type Description

flagName

String

Full flag name including the namespace, for example, myNamespace.flagName.

Rox methods

A list of Rox methods follows.

setup

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

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

Use as in the following example:

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

appKey

String

environment-specific SDK key (from UI)

options

Object

configuration object (optional)

options

Parameter Type Description

version

String

JavaScript client version

configurationFetchedHandler

Function

Function that is called after data is synced from the platform.

debugLevel

String

SDK verbosity level, accept verbose level.

impressionHandler

Function

Function that is called every time a flag value is computed and evaluated on the client. If a flag has an override, or is already frozen, then no impression handler is triggered.

platform

String

Custom platform, used to override the default platform (Browser, for platforms such as Tizen, Chromecast, etc).

fetchIntervalInSec

Number

Configures the interval of the fetch configuration call, in seconds. The default value is 60 seconds and the minimum is 30 seconds.

The return value is the promise that is executed once the configuration is retrieved from the platform.

register

Register a container of Rox entities by specifying a namespace.

(static) register(namespace, container)

Register a container of Rox entities and set the namespace to an empty string.

(static) register(container)
Parameter Type Description

namespace

String

Container namespace. Default is ''.

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 includes static identifiers that do not change in the lifetime of the application.

Parameter Type Description

context

Object

The context object

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

setCustomBooleanProperty

Sets a custom property value for use when creating target groups.

(static) setCustomBooleanProperty(key, value)
Parameter Type Description

key

String

The name of the custom property.

value

Boolean

The value of the custom property, or a function that accepts context and retrieves a value (refer to the example below).

// Check if the user has the tester role via a function and context Rox.setCustomBooleanProperty("tester", function(context){ return context.user.isTester(); });

setCustomNumberProperty

Sets a custom property value for use when creating target groups.

(static) setCustomNumberProperty(key, value)
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 retrieves a value (refer to the example below).

// Calculate the user's age via a function and context Rox.setCustomNumberProperty("age", function(context){ return context.user.getAge(); });

setCustomStringProperty

Sets a custom property value for use when creating target groups.

(static) setCustomStringProperty(key, value)
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 retrieves a value (refer to the example below).

// Retrieve user email via a function and context Rox.setCustomStringProperty("role", function(context){ return context.role; });

fetch

Fetch() pulls the latest configuration and flag values down from the platform.

(static) fetch()

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', and '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) {}
  • reporting is an object describing:

    • Flag name

    • Flag value

    • Flag targeting, and whether evaluation is result of configuration in the UI or is default.

  • context is the context passed to the flag evaluation function. Undefined if no context used.

configurationFetchedHandler

configurationFetchedHandler function is called with one argument from type FetcherResults.

function configurationFetchedHandler(fetcherResult){}

The FetcherResults instance has the following properties:

Name Type Description

fetcherStatus

String

Indicates the source of the applied data, and is 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 is not received correctly.

creationDate

Date

The time this data was created and signed in the CloudBees platform.

hasChanges

Boolean

True if this data is not already available on the client.

errorDetails

String

Error string in the case of an error.

dynamicPropertyRuleHandler

The dynamic custom property generator is called when an explicit custom property definition does not exist on the client side.

If you do not set the dynamicPropertyRuleHandler, it activates the default function, which tries to extract the property value from the context by its name.

function dynamicPropertyRuleHandler(propName, context){}
(propName, context) => context ? context[propName] : undefined
Parameter Type Description

handler

(propName, context) ⇒ value

The function to be called when the SDK searches for a property not explicitly set by the setCustom`Type`Property functions.

Rox.dynamicApi

An alternative to using a container and registering. This allows you to evaluate flags without having a static container. DynamicApi creates flags as if they were registered, including sending them to the UI.

isEnabled

Evaluate a Boolean flag.

(static) isEnabled(name, defaultValue, context)
Name Type Description

name

String

The flag name. Returns an error if not a string.

defaultValue

Boolean

The default value to use if there are no UI configurations, or if the value is set to default in the UI. Returns an error if not a Boolean.

context

Object

A context to evaluate the flag with. It is merged with the global context.

value

Evaluate a string flag.

(static) value(name, defaultValue, variations, context)
Name Type Description

name

String

The flag name. Returns an error if not a string.

defaultValue

String

The default value to use if there are no UI configurations, or if the value is set to default in the UI. Returns an error if not a string.

variations

Array<string>

All alternative values, can be used in and updated in the UI.

context

Object

A context to evaluate the flag with. It is merged with the global context.

getNumber

Evaluate a number flag.

(static) getNumber(name, defaultValue, variations, context)
Name Type Description

name

String

The flag name. Returns an error if not a string.

defaultValue

Number

The default value to use if there are no UI configurations, or if the value is set to default in the UI. Returns an error if not a number.

variations

Array<number>

All alternative values, can be used in and updated in the UI.

context

Object

A context to evaluate the flag with. It is merged with the global context.