Universal JavaScript SDK for both Node.js and the browser.
Visit https://featurevisor.com/docs/sdks/ for more information.
$ npm install --save @featurevisor/sdk
Initialize the SDK:
import { createInstance } from "@featurevisor/sdk";
const sdk = createInstance(options);
Options you can pass when creating Featurevisor SDK instance:
- Type:
string
- Required: no
- Defaults to:
.
- Type:
function
- Required: no
Use it to take over bucketing key generation process.
const sdk = createInstance({
configureBucketKey: (feature, context, bucketKey) => {
return bucketKey;
}
});
- Type:
function
- Required: no
Use it to take over bucketing process.
const sdk = createInstance({
configureBucketValue: (feature, context, bucketValue) => {
return bucketValue; // 0 to 100,000
}
});
- Type:
object
- Required: either
datafile
ordatafileUrl
is required
Use it to pass the datafile object directly.
- Type:
string
- Required: either
datafile
ordatafileUrl
is required
Use it to pass the URL to fetch the datafile from.
- Type:
function
- Required: no
Pass this function if you need to take over the datafile fetching and parsing process:
const sdk = createInstance({
handleDatafileFetch: async (datafileUrl) => {
const response = await fetch(datafileUrl);
const datafile = await response.json();
return datafile;
}
});
- Type:
object
- Required: no
Pass set of initial features with their variation and (optional) variables that you want the SDK to return until the datafile is fetched and parsed:
const sdk = createInstance({
initialFeatures: {
myFeatureKey: {
enabled: true,
// optional
variation: "treatment",
variables: {
myVariableKey: "my-variable-value"
}
}
}
});
- Type:
function
- Required: no
Intercept given context before they are used to bucket the user:
const defaultContext = {
platform: "web",
locale: "en-US",
country: "US",
timezone: "America/New_York",
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko)",
};
const sdk = createInstance({
interceptContext: (context) => {
return {
...defaultContext,
...context,
};
}
});
- Type:
object
- Required: no
Pass a custom logger with custom levels, otherwise it the SDK will print logs to the console for error
and warn
levels.
import { createInstance, createLogger } from "@featurevisor/sdk";
const sdk = createInstance({
logger: createLogger({
levels: ["debug", "info", "warn", "error"],
handler: (level, message, details) => {
// do something with the log
},
}),
});
- Type:
function
- Required: no
Capture activated features along with their evaluated variation:
const sdk = createInstance({
onActivation: (featureKey, variation, context, captureContext) => {
// do something with the activated feature
}
});
captureContext
will only contain attributes that are marked as capture: true
in the Attributes' YAML files.
- Type:
function
- Required: no
Triggered maximum once when the SDK is ready to be used.
const sdk = createInstance({
onReady: () => {
// do something when the SDK is ready
}
});
- Type:
function
- Required: no
Triggered every time the datafile is refreshed.
Works only when datafileUrl
and refreshInterval
are set.
const sdk = createInstance({
onRefresh: () => {
// do something when the datafile is refreshed
}
});
- Type:
function
- Required: no
Triggered every time the datafile is refreshed, and the newly fetched datafile is detected to have different content than last fetched one.
Works only when datafileUrl
and refreshInterval
are set.
const sdk = createInstance({
onUpdate: () => {
// do something when the datafile is updated
}
});
- Type:
number
(in seconds) - Required: no
Set the interval to refresh the datafile.
const sdk = createInstance({
refreshInterval: 60 * 5, // every 5 minutes
});
- Type:
object
- Required: no
If set, the SDK will skip evaluating the datafile and return variation and variable results from this object instead.
If a feature key is not present in this object, the SDK will continue to evaluate the datafile.
const sdk = createInstance({
stickyFeatures: {
myFeatureKey: {
enabled: true,
// optional
variation: "treatment",
variables: {
myVariableKey: "my-variable-value"
}
}
}
});
These methods are available once the SDK instance is created:
isEnabled(featureKey: string, context: Context): boolean
getVariation(featureKey: string, context: Context): VariationValue
getVariable(featureKey: string, variableKey: string, context: Context): VariableValue
Also supports additional type specific methods:
getVariableBoolean
getVariableString
getVariableInteger
getVariableDouble
getVariableArray
getVariableObject
getVariableJSON
activate(featureKey: string, context: Context): VariationValue
Same as getVariation
, but also calls the onActivation
callback.
This is a convenience method meant to be called when you know the User has been exposed to your Feature, and you also want to track the activation.
isReady(): boolean
Synchonously check if the SDK is ready to be used.
refresh(): void
Manually refresh datafile.
setLogLevels(levels: LogLevel[]): void
Accepted values for levels
: ["debug", "info", "warn", "error"]
.
on(event: string, callback: function): void
Listen to SDK events, like:
ready
activation
refresh
update
Alias for on
method.
off(event: string, callback: function): void
Alias for off
method.
removeAllListeners(event?: string): void
Resolves the SDK instance with a promise for convenience when it's ready:
onReady(): Promise
MIT © Fahad Heylaal