@karhoo/lib-feature-flags
v20.1.4
Published
Feature flags π© library for Karhoo web apps. It provides possibility to enable/disable certain feature per environment without redeployment π
Downloads
34
Readme
This library was created in order to have a possibility to enable/disable certain feature per environment without redeployment.
Installation
npm i @karhoo/lib-feature-flags
Warnings
This library uses Promise
, async/await
and other modern JS features so currently it works only in modern browsers by default. For old browsers you must run babel on it.
Usage
In order to get Feature flags config you need to make request for remoteConfig:
import { FeatureFlags, RemoteConfig } from '@karhoo/lib-feature-flags'
import { getApi } from '@karhoo/demand-api'
const defaultOptions = { defaultValueForMissingKeys: false }
let flagsInstance
const initFlagsInstance = config => {
const api = getApi({
url: 'api',
defaultRequestOptionsGetter: () => ({
headers: {
identifier: 'XXXX',
referrer: 'https://example-referer.com/',
},
}),
})
const { session, flags } = config
const organisationId = 'organisationId'
const remoteConfigFetcher = () =>
api.flagsService.getLatestVersion({ organisationId, platform: 'web' }).then(data => {
return data.body?flags || {}
})
flagsInstance = new FeatureFlags(new RemoteConfig(remoteConfigFetcher), flags || defaultOptions)
}
const getFlagsInstance = () => {
return flagsInstance
}
After creating getFlagsInstances funtion you can use it, for example:
if (someCondition && getFlagsInstance().isEnabled(MY_FEATURE))) {
// do thomething...
}
FeatureFlags
It takes config and has such functionality as enable/disable feature and isEnabled method that returns boolean for feature flag that is passed to it. By default it returns false if key is missing. This behavior can be changed by passing options to it.
FeatureProvider
FeatureProvider is a provider of FeatureFlags. Use it in your App component so that every component can use this functionality.
Feature component could be used for every feature that you want to have a possibility to enable/disable. Example of usage:
<Feature name="pageLogo"> <PageLogo /> </Feature>
ObjectConfig
It can be used if you want to use simple object as a config for feature toggling. It takes config and environment so different configs could be used per different environments.
Issues
Looking to contribute?
π Bugs
Please file an issue for bugs, missing documentation, or unexpected behavior with a label FEATURE_FLAGS
π‘ Feature Requests
Please file an issue to suggest new features with a label FEATURE_FLAGS
. Vote on feature requests by adding
a π. This helps maintainers prioritize what to work on.
β Questions
For questions related to using the library, please re-visit a documentation first. If there are no answer, please create an issue with a label help needed
and FEATURE_FLAGS
.