@karhoo/lib-feature-flags
TypeScript icon, indicating that this package has built-in type declarations

20.1.4Β β€’Β PublicΒ β€’Β Published
Karhoo logo

Karhoo Lib Feature Flags

This library was created in order to have a possibility to enable/disable certain feature per environment without redeployment.

License

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.

Contributing

License

BSD-2-Clause

Package Sidebar

Install

npm i @karhoo/lib-feature-flags

Weekly Downloads

8

Version

20.1.4

License

BSD-2-Clause

Unpacked Size

132 kB

Total Files

25

Last publish

Collaborators

  • karhoo_js
  • karhoo_ops_tooling
  • karhoo_connect
  • karhoo_demand_web
  • karhoo_partner_central