Koople
Search…
JavaScript

Getting Started

If still not registered, sign up and create an account at https://koople.io.
1. Install and import package
1
npm i @pataflags/sdk-js
Copied!
Refer to the NPM release page to identify the latest version.
2. Initialize the SDK
1
const user = { id: 'aUserId' }
2
const pfclient = PFClient.initialize('YOUR_API_KEY', user);
Copied!
3. Get your values
1
pfclient.onReady(() => {
2
const isEnabled = pfclient.isEnabled('myAwesomeFeature');
3
if (isEnabled) {
4
// Show the feature
5
} else {
6
// Hide the feature
7
}
8
});
Copied!

Customize the SDK

You can set custom parameters to the client by creating a custom configuration object:
1
const options = { timeout: 500 };
2
const pfclient = PFClient.initialize('YOUR_API_KEY', user, options);
Copied!
Parameter
Description
Default
storage
Use storage to keep your evaluation object response in the cache. See storage.
none
bootRun
Use bootRun to provide an initial evaluation object for the SDK. See bootRun.
network
fallback
Use fallback to provide a default evaluation object to the SDK. See fallback.
null
timeout
The number of milliseconds the SDK waits for a response from the Koople servers before returning the evaluation object from the storage or fallback. See timeout.
2000
pollingInterval
Interval in seconds with which the SDK makes requests to the server to update the settings. See pollingInterval.
0

Storage

Use storage to keep your evaluation object response in the cache. When the request fails, the SDK try automatically to get the latest valid evaluation object from storage.
Value
Behaviour
none
Default value. The SDK will not store the evaluation object.
session
The SDK will store the latest evaluation object between page loads.
persistent
The SDK will store the latest evaluation object between sessions.
1
const pfclient = PFClient.initialize('YOUR_API_KEY', user, {
2
storage: 'persistent'
3
});
Copied!

Boot run

UsebootRun to provide an initial evaluation object for the SDK.
Value
Behaviour
network
The default value. The SDK will boot from the server response.
storage
The SDK will boot with the lastest stored response. Meanwhile, the request is loading in the background, and the evaluation object will be updated with the new server response. This mode allows loading the page without any delay.
1
const pfclient = PFClient.initialize('YOUR_API_KEY', user, {
2
bootRun: 'storage'
3
});
Copied!

Fallback

Use fallback to provide a default evaluation object for the SDK. When the request fails, and the storage is empty, the fallback will be returned. If the SDK has already initialized successfully, the fallback will not take effect.
1
const pfclient = PFClient.initialize('YOUR_API_KEY', user, {
2
fallback: YOUR_EVALUATION_OBJECT
3
});
Copied!
Alternatively, you can provide your fallback from storage. This means that in case of failure, the last valid server configuration will be returned.

Timeout

The number of milliseconds the SDK waits for a response from the Koople servers before returning the evaluation object from the storage or fallback.
Timeout is more useful when combines with fallback. The minimum value timeout is 50 milliseconds. If the value is less than 50 it will be ignored and automatically set to 50. If you want less than 50 milliseconds, then see bootRun.
1
const pfclient = PFClient.initialize('YOUR_API_KEY', user, {
2
timeout: 50
3
});
Copied!

Polling interval

You can configure your SDK to update the evaluation object every pollingInterval seconds automatically. You will receive the changes in the subscription to onChanges.
The minimum value is 1 second. If the value is less than 1 it will be ignored and automatically set to 1.
1
const pfclient = PFClient.initialize('YOUR_API_KEY', user, {
2
pollingInterval: 60
3
});
4
pfclient.onChanges(() => {
5
// ...
6
});
Copied!

Events

onReady

The SDK will raise the ready event when the SDK is initialized.
The initialization will depend on the SDK configuration. If you have configured the bootRun parameter as storage, the initialization will be immediate. Else it will take until the server responds.
You can subscribe to the ready event before or after the SDK initialization, in both cases, the callback will be executed.
1
pfclient.onReady(() => {
2
const isEnabled = pfclient.releaseToggle('myAwesomeFeature');
3
if (isEnabled) {
4
// Show the feature
5
} else {
6
// Hide the feature
7
}
8
});
Copied!

onIdentified

The SDK will raise the identified event when you call to identify method.
1
pfclient.onIdentified(() => {
2
// ...
3
});
Copied!

onChanges

While the ready and identified events are fired only once, changes will be fired every time the evaluation object is updated, including the times ready and identified events are fired.
Changes event can cause that, if the evaluation object changes, the page will be repainted or cause UI flickering.
In some cases, you can use changes event as a mechanism to ask the user if wants to apply changes or reload the page.
1
pfclient.onChanges(() => {
2
if (confirm('There is a more updated version of the page.' +
3
'Do you want to reload the page?')) {
4
// apply changes or reload page
5
}
6
});
Copied!

Methods

releaseToggles

The releaseToggles method will return a map of release toggles with its settings.
1
pfclient.releaseToggles();
Copied!

isEnabled

The isEnabled method will return a boolean indicating whether a release toggle is enabled or disabled.
Name
Type
releaseToggleKey
string
required
1
pfclient.isEnabled('myAwesomeFeature');
Copied!

remoteConfigs

The remoteConfigs method will return a map of remote configs with its settings.
1
pfclient.remoteConfigs();
Copied!

valueOf

The valueOf method will return a value for a remote config. You can pass an optional second parameter used as a fallback if there is no value.
Name
Type
remoteConfigKey
string
required
fallback
string
optional
1
pfclient.valueOf('theme', 'light');
Copied!

evaluation

The evaluation method will return an evaluation object.
1
pfclient.evaluation();
Copied!

identify

The identify method provides a mechanism to update your evaluation object when the user has changed. It's the typical login scenario when a user goes from being anonymous to an authenticated user.
Changes will be listened from onIdentified and onChanges.
Name
Type
user
required
1
client.onIdentified(() => { /* UPDATED SETTINGS */ });
2
client.onChanges(() => { /* UPDATED SETTINGS */ });
3
4
client.identify({ id: 'logged-user' });
Copied!

refresh

The refresh method provides a mechanism to update your evaluation object manually. It is manual polling.
Changes can be listened from onChanges.
1
client.onChanges(() => { /* UPDATED SETTINGS */ });
2
3
client.refresh();
Copied!

User object

// TODO ...

Evaluation object

Each request to the server will have an evaluation object as a response. Also, fallback parameter is an evaluation object. You can retrieve the current evaluation object using evaluation method.

Anatomy

A valid evaluation object should contain these fields:
Name
Description
releaseToggles
Is a map of key values.
  • The keys represent the key of your release toggle and should be a string.
  • The values indicate if your release toggle is enabled or disabled and should be a boolean.
remoteConfigs
Is a map of key values.
  • The keys represent the key of your remote config and should be a string.
  • The values indicate a value of your remote config and should be a boolean.

Example

1
const evaluationObject = {
2
releaseToggles: {
3
myAwesomeFeature: true,
4
mySuperFeature: true
5
},
6
remoteConfigs: {
7
theme: 'dark',
8
permissions: 'ADMIN'
9
}
10
};
Copied!
Last modified 11mo ago