@farmart-tech/brave-admin-sdk
    Preparing search index...

    Class FeatureFlagWriter

    Client for managing feature flags, segments, and conditions.

    Provides full write access to the Brave Ship feature flag system, including creating, updating, deleting, archiving, and moving feature flags between workspaces.

    All feature flag names must follow snake_case convention (lowercase with underscores).

    import { FeatureFlagWriter, FeatureBuilder } from '@farmart-tech/brave-admin-sdk';
    import { getAuth } from 'firebase/auth';

    const writer = new FeatureFlagWriter({
    api: 'https://api.farmart.com/api/v1',
    getIdToken: async () => await getAuth().currentUser.getIdToken(),
    workspace: 'production'
    });

    // Create a simple feature flag
    await writer.setFeatureFlag('dark_mode', {
    enabled: true,
    description: 'Enable dark mode UI'
    });

    // Use FeatureBuilder for complex configurations
    const feature = new FeatureBuilder()
    .setFeatureName('premium_features')
    .setEnabled(true)
    .setRolloutStrategy({
    type: 'percentage',
    config: { percentage: 50 }
    })
    .build();

    await writer.setFeatureFlag('premium_features', feature);
    Index

    Constructors

    Properties

    FeatureBuilder: typeof FeatureBuilder = FeatureBuilder

    Static reference to FeatureBuilder for convenient access.

    const builder = new FeatureWriter.FeatureBuilder();
    

    Methods

    • Archives a feature flag (moves it to archived state while preserving data).

      Parameters

      • flagName: string

        Name of the feature flag to archive

      • featureData: FeatureData | null | undefined

        Current feature flag data

      Returns Promise<boolean>

      Promise resolving to true if archived successfully, false if featureData is null/undefined

      Archived flags can be restored using unarchiveFeatureFlag. This is preferred over deleteFeatureFlag when you might need the configuration later.

      const featureData = await reader.getFeatureFlag('old_experiment');
      await writer.archiveFeatureFlag('old_experiment', featureData);
    • Copies a feature flag to another workspace without removing it from current workspace.

      Parameters

      • flagName: string

        Name of the feature flag to copy

      • featureData: FeatureData | null | undefined

        Feature flag data to copy

      • workspace: string

        Target workspace ID

      Returns Promise<false | undefined>

      Promise resolving to false if featureData or workspace is null/undefined

      const stagingFeature = await reader.getFeatureFlag('new_feature');
      await writer.copyFeatureToWorkspace('new_feature', stagingFeature, 'production');
    • Permanently deletes an archived feature flag.

      Parameters

      • flagName: string

        Name of the archived feature flag to delete

      Returns Promise<void>

      This operation is irreversible and applies only to archived flags.

      await writer.deleteArchivedFeatureFlag('very_old_experiment');
      
    • Deletes a condition configuration.

      Parameters

      • conditionName: string

        Name of the condition to delete

      Returns Promise<void>

      await writer.deleteFeatureCondition('old_condition');
      
    • Permanently deletes a feature flag.

      Parameters

      • flagName: string

        Name of the feature flag to delete

      Returns Promise<void>

      This operation is irreversible. Consider using archiveFeatureFlag instead if you might need to restore the flag later.

      await writer.deleteFeatureFlag('old_experiment');
      
    • Deletes a segment group.

      Parameters

      • segmentName: string

        Name of the segment to delete

      Returns Promise<void>

      await writer.deleteSegmentGroup('old_segment');
      
    • Moves a feature flag from current workspace to another workspace.

      Parameters

      • flagName: string

        Name of the feature flag to move

      • featureData: FeatureData | null | undefined

        Current feature flag data

      • workspace: string

        Target workspace ID

      Returns Promise<false | undefined>

      Promise resolving to false if featureData or workspace is null/undefined

      This operation copies the flag to the target workspace and then deletes it from the current workspace. If you want to keep the flag in both workspaces, use copyFeatureToWorkspace instead.

      const feature = await reader.getFeatureFlag('new_checkout');
      await writer.moveFeatureToWorkspace('new_checkout', feature, 'production');
    • Registers a new condition configuration.

      Parameters

      • conditionName: string

        Name for the condition (must be snake_case)

      • conditions: FeatureCondition

        Condition logic definition

      Returns Promise<void>

      If conditionName is not in snake_case format

      await writer.registerFeatureCondition('premium_users', {
      field: 'userTier',
      operator: 'eq',
      value: 'premium'
      });
      await writer.registerFeatureCondition('eligible_beta_users', {
      allOf: [
      { field: 'appVersion', operator: 'gte', value: '2.0.0' },
      { field: 'betaOptIn', operator: 'eq', value: true }
      ]
      });
    • Registers a new user segment group.

      Parameters

      • segmentName: string

        Name for the segment (must be snake_case)

      • segmentType: "user.id" | "device.platform" | "geo.state" | "device.environment"

        Type of segment ('user' or 'device')

      • segmentCriteria: { list: string[] }

        Criteria for segment membership

      Returns Promise<void>

      If segmentName is not in snake_case format

      await writer.registerSegmentGroup(
      'premium_users',
      'user',
      {
      field: 'tier',
      operator: 'in',
      value: ['premium', 'enterprise']
      }
      );
    • Creates a new feature flag.

      Parameters

      • flagName: string

        Name of the feature flag (must be snake_case)

      • options: Partial<FeatureData> = {}

        Feature flag configuration data

      • workspace: string = ...

        Target workspace (defaults to instance workspace)

      Returns Promise<void>

      If flagName is not in snake_case format

      Flag names must be lowercase alphanumeric with underscores only.

      await writer.setFeatureFlag('dark_mode', {
      enabled: true,
      description: 'Enable dark mode'
      });
      await writer.setFeatureFlag('premium_checkout', {
      enabled: true,
      condition: {
      field: 'userTier',
      operator: 'eq',
      value: 'premium'
      }
      });
    • Restores an archived feature flag to active state.

      Parameters

      • flagName: string

        Name of the archived feature flag

      • featureData: FeatureData | null | undefined

        Archived feature flag data to restore

      Returns Promise<boolean>

      Promise resolving to true if unarchived successfully, false if featureData is null/undefined

      const archivedData = await reader.getArchivedFeatureFlag('old_experiment');
      await writer.unarchiveFeatureFlag('old_experiment', archivedData);
    • Updates an existing condition configuration.

      Parameters

      • conditionName: string

        Name of the condition to update

      • conditions: FeatureCondition

        New condition logic

      Returns Promise<void>

      await writer.updateFeatureCondition('premium_users', {
      anyOf: [
      { field: 'userTier', operator: 'eq', value: 'premium' },
      { field: 'userTier', operator: 'eq', value: 'enterprise' }
      ]
      });
    • Updates an existing feature flag.

      Parameters

      • flagName: string

        Name of the feature flag to update

      • options: Partial<FeatureData> = {}

        Partial feature flag data to update

      Returns Promise<void>

      Only provided fields will be updated; other fields remain unchanged.

      // Update only the enabled status
      await writer.updateFeatureFlag('dark_mode', {
      enabled: false
      });
    • Updates an existing segment group.

      Parameters

      • segmentName: string

        Name of the segment to update

      • segmentType: "user.id" | "device.platform" | "geo.state" | "device.environment"

        Updated segment type

      • segmentCriteria: { list: string[] }

        Updated segment criteria

      Returns Promise<void>

      await writer.updateSegmentGroup(
      'premium_users',
      'user',
      {
      field: 'tier',
      operator: 'eq',
      value: 'premium_plus'
      }
      );
    • Internal

      Merges updates while preserving creation timestamp from existing data.

      Type Parameters

      • T extends CommonFirebaseDataConfigs

      Parameters

      • current: Partial<T>

        Current data containing metadata

      • updates: Partial<T>

        New data to merge

      Returns Partial<T>

      Merged data with preserved createdAt timestamp