1.0.0 • Published 3 months ago

toggle-provider v1.0.0

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
3 months ago

@dataverse/toggle-provider

This package provides a common interface for feature toggle provider functionality as well as default feature toggle provider implementation leveraging Unleash server client.

Config

NameDescription
urlURL to Unleash server (not proxy but the actual instance)
appNameapp name for browser and metric reasons
instanceIdidentity the instance
apiTokentoken to authorize API requests (taken from Unleash)
loggerA config for the logger
refreshIntervalHow often does unleash client refreshes the toggles status
disableMetricsShould the client register with the server and send usage metrics
environmentWhich environment in Unleash to connect to

For logger configuration properties see the logger documentation

Usage

import { UnleashToggleProvider } from '@dataverse/toggle-provider';

const config = {
  url: 'https://URL to your unleash instance/api',
  appName: 'my application',
  instanceId: 'my application instance id',
  apiToken: '*.development.tokenfromunleash',
  logger: { ...loggerConfig },
  refreshInterval: 15000,
  disableMetrics: false,
  environment: 'development',
};

const provider = new UnleashToggleProvider(config);

// Init the instance (optional bootstrap can be provided to load feature config before first api call is done)
await provider.init();

const isEnabled = provider.isEnabled('feature-name-from-unleash');
// console.log(isEnabled) -> false

// Optionally make sure your openapi definition is feature toggled.
// Take a look into ./src/__tests__/fixtures for possible usage.
// Currently object and array object segments can be feature toggled
const openapiSpec = {
  info: {},
  paths: {
    '/users': {
      post: {
        'x-toggle-feature': 'feature-name-from-unleash',
      },
    },
  },
};

const featureToggleUpdatedSpec = this.provider.syncOpenApiSpec(openapiSpec);
// console.log(featureToggleUpdatedSpec) ->
// {
//   info: {},
//   paths: {
//     '/users': {
//       // The whole post object parrent to 'x-toggle-feature' is removed when the feature toggle is disabled
//     },
//   },
// }
1.0.0

3 months ago