0.1.0 • Published 1 year ago

@tryrolljs/feature-flag v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@tryrolljs/feature-flag

Install

yarn add @tryrolljs/feature-flag
npx install-peerdeps @tryrolljs/feature-flag

Usage

Wrap the root of your application with the provider.

import { FeatureFlagProvider } from '@tryrolljs/feature-flag'

const flags = [
  {
    type: 'static',
    name: 'useNavigationV2',
    value: true,
  },
]

const App = ({ children }) => {
  return (
    <FeatureFlagProvider flags={flags}>
      {children}
    </FeatureFlagProvider>
  )
}

Use hook to retreive a feature flag value.

import { useFeatureFlag } from '@tryrolljs/feature-flag'

import Navigation from './navigation'
import NavigationV2 from './navigationV2'

const Layout = () => {
  const shouldUseNavigationV2 = useFeatureFlag('useNavigationV2')

  return shouldUseNavigationV2 ? <NavigationV2 /> : <Navigation />
}

Advanced Usage

This package allows you to inject any feature flag provider. You can make static, async & computed flags. Moreover, you can add an async group from an external provider (ex. LaunchDarkly).

import * as LDClient from 'launchdarkly-js-client-sdk'
import { FeatureFlagProvider } from '@tryrolljs/feature-flag'

const flags = [
  {
    type: 'static',
    name: 'useNavigationV2',
    value: true,
  },
  {
    type: 'async',
    name: 'useExternalService',
    value: async () => {
      try {
        const response = await fetch('https://external.service/api/healthcheck')
        return response.status === 200
      } catch (e) {
        return false
      }
    },
    defaultValue: false,
  },
  {
    type: 'asyncGroup',
    value: async () => {
      const client = LDClient.initialize('LAUNCH_DARKLY_CLIENT_ID', { key: 'test' })
      await client.waitUntilReady()
      return client.allFlags();
    },
    defaultValue: {
      ldFeatureFlag1: true,
      ldFeatureFlag2: 123,
      ldFeatureFlag3: 'here',
    },
  },
  {
    type: 'computed',
    name: 'useExternalServiceLinkInNavigation',
    value: flags => flags.useNavigationV2 && flags.useExternalService,
  }
]

const App = ({ children }) => {
  return (
    <FeatureFlagProvider flags={flags}>
      {children}
    </FeatureFlagProvider>
  )
}
0.1.0

1 year ago

0.0.8

1 year ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago