Node.js

Installation

Add the OpenFeature SDK and the Tggl Provider to your project:

npm i @openfeature/server-sdk openfeature-server-tggl-provider

Quick start

Register the TgglServerProvider with the OpenFeature SDK and start evaluating flags locally:

import { OpenFeature } from '@openfeature/server-sdk';
import { TgglServerProvider } from 'openfeature-server-tggl-provider'
 
await OpenFeature.setProviderAndWait(new TgglServerProvider('API_KEY'))
 
const client = OpenFeature.getClient();
 
const value = await client.getBooleanValue(
  'my-feature',
  false,
  { userId: 'foo' }
)
 
if (value) {
  console.log('Feature is enabled')
}

Getting a flag's value

There are four methods to get a flag's value: getBooleanValue, getNumberValue, getStringValue, and getObjectValue. Those four methods return a Promise, but evaluation is done locally and no API call is performed.

Each method takes the flag's key as the first argument, a default value as the second argument, and an optional context object as the third argument.

await client.getBooleanValue('feat-a', false, { userId: 'foo' })
await client.getNumberValue('feat-b', 42, { userId: 'foo' })
await client.getStringValue('feat-c', 'foo', { userId: 'foo' })
await client.getObjectValue('feat-d', { custom: 'object' }, { userId: 'foo' })

The default value must be specified and will be returned if the flag is not found or if an error occurs.

Info

When calling either getBooleanValue, getNumberValue, or getStringValue, the value will be cast to the appropriate type. If you are unsure of the type of the flag, use getObjectValue to get the raw value which may be of any valid JSON value, including boolean, string, and number.

Fetching the initial configuration

Calling setProviderAndWait will fetch the configuration from the Tggl API and cache it locally. The promise will resolve when the configuration has been successfully fetched, and will fail if an error occurred.

Alternatively, you can use the setProvider method to set the provider without waiting for the configuration to be fetched:

import { OpenFeature, ServerProviderEvents } from '@openfeature/server-sdk';
import { TgglServerProvider } from 'openfeature-server-tggl-provider'
 
OpenFeature.setProvider(new TgglServerProvider('API_KEY'))
 
OpenFeature.addHandler(ServerProviderEvents.Error, ({ message }) => {
  console.log(message)
})
 
OpenFeature.addHandler(ServerProviderEvents.Ready, () => {
  console.log('Provider ready')
})

You can also get the status on the provider by reading the providerStatus property on the client:

const client = OpenFeature.getClient();
 
console.log(client.providerStatus) // NOT_READY, READY, ERROR...

Polling for "live" updates

If you need your client to be up-to-date with the latest flag configuration, you can enable polling:

OpenFeature.setProvider(new TgglServerProvider('API_KEY', {
  pollingInterval: 5000
}))

Here, the provider will poll the Tggl API every 5 seconds for updates. You can be notified when the configuration changes by listening to the ConfigurationChanged event:

OpenFeature.addHandler(ServerProviderEvents.ConfigurationChanged, () => {
  // Configuration has changed
})

This is a good time to recompute flags where you need a "live" value.

Using the Proxy

By default, the Provider talks directly to the Tggl API. If you are using the Tggl Proxy, you can specify the proxy URL when instantiating the provider:

new TgglServerProvider('API_KEY', {
  baseUrl: 'http://your-proxy-domain.com'
})

The /config and /report path will be appended to the baseUrl and both flags evaluation and reporting will go through the proxy. If your proxy is configured with custom paths, you can specify them:

new TgglServerProvider('API_KEY', {
  url: 'http://your-proxy-domain.com/custom-config',
  reporting: {
    url: 'http://your-proxy-domain.com/custom-report'
  }
})

Going further

Read the official OpenFeature SDK documentation for more information on how to use the SDK.

You can also have a look at Tggl's Node.js SDK documentation that is used under the hood by the TgglServerProvider.