|
| 1 | +# Flags SDK - Flagsmith Provider |
| 2 | + |
| 3 | +A provider adapter for the Flags SDK that integrates with [Flagsmith](https://flagsmith.com/), allowing you to use Flagsmith's feature flags and remote configuration in your application. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install @flags-sdk/flagsmith |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +An Environment ID must be provided using the `FLAGSMITH_ENVIRONMENT_ID` environment variable. |
| 14 | + |
| 15 | +```typescript |
| 16 | +import { flagsmithAdapter } from '@flags-sdk/flagsmith'; |
| 17 | +import { flag } from 'flags'; |
| 18 | + |
| 19 | +// Boolean flags - returns flagState.enabled |
| 20 | +const myBooleanFlag = flag({ |
| 21 | + key: 'my-boolean-feature', |
| 22 | + adapter: flagsmithAdapter.booleanValue(), |
| 23 | +}); |
| 24 | + |
| 25 | +// String flags - returns flagState.value when enabled |
| 26 | +const myStringFlag = flag({ |
| 27 | + key: 'my-string-feature', |
| 28 | + adapter: flagsmithAdapter.stringValue(), |
| 29 | +}); |
| 30 | + |
| 31 | +// Number flags - returns flagState.value when enabled |
| 32 | +const myNumberFlag = flag({ |
| 33 | + key: 'my-number-feature', |
| 34 | + adapter: flagsmithAdapter.numberValue(), |
| 35 | +}); |
| 36 | +``` |
| 37 | + |
| 38 | +## API |
| 39 | + |
| 40 | +The adapter provides three methods for different flag types: |
| 41 | + |
| 42 | +### `booleanValue()` |
| 43 | + |
| 44 | +Returns an adapter for boolean flags. Uses `flagState.enabled` directly. |
| 45 | + |
| 46 | +```typescript |
| 47 | +const booleanAdapter = flagsmithAdapter.booleanValue(); |
| 48 | +const value = await booleanAdapter.decide({ |
| 49 | + key: 'my-flag', |
| 50 | + defaultValue: false, |
| 51 | + entities: { identifier: 'user-123' }, |
| 52 | +}); |
| 53 | +``` |
| 54 | + |
| 55 | +### `stringValue()` |
| 56 | + |
| 57 | +Returns an adapter for string flags. Returns `flagState.value` when the flag is enabled, otherwise returns the default value. |
| 58 | + |
| 59 | +```typescript |
| 60 | +const stringAdapter = flagsmithAdapter.stringValue(); |
| 61 | +const value = await stringAdapter.decide({ |
| 62 | + key: 'my-flag', |
| 63 | + defaultValue: 'default', |
| 64 | + entities: { identifier: 'user-123' }, |
| 65 | +}); |
| 66 | +``` |
| 67 | + |
| 68 | +### `numberValue()` |
| 69 | + |
| 70 | +Returns an adapter for number flags. Returns `flagState.value` when the flag is enabled, otherwise returns the default value. |
| 71 | + |
| 72 | +```typescript |
| 73 | +const numberAdapter = flagsmithAdapter.numberValue(); |
| 74 | +const value = await numberAdapter.decide({ |
| 75 | + key: 'my-flag', |
| 76 | + defaultValue: 0, |
| 77 | + entities: { identifier: 'user-123' }, |
| 78 | +}); |
| 79 | +``` |
| 80 | + |
| 81 | +## Identity Handling |
| 82 | + |
| 83 | +The adapter supports Flagsmith identity management. Pass an `IIdentity` object to the `entities` parameter: |
| 84 | + |
| 85 | +```typescript |
| 86 | +const identity: IIdentity = { |
| 87 | + identifier: 'user-123', |
| 88 | + traits: { |
| 89 | + email: 'user@example.com', |
| 90 | + plan: 'premium', |
| 91 | + }, |
| 92 | +}; |
| 93 | + |
| 94 | +const value = await adapter.decide({ |
| 95 | + key: 'my-flag', |
| 96 | + defaultValue: false, |
| 97 | + entities: identity, |
| 98 | +}); |
| 99 | +``` |
| 100 | + |
| 101 | +## Configuration |
| 102 | + |
| 103 | +The adapter automatically initializes Flagsmith with the following configuration: |
| 104 | + |
| 105 | +- `environmentId`: From `FLAGSMITH_ENVIRONMENT_ID` environment variable |
| 106 | + |
| 107 | +## Features |
| 108 | + |
| 109 | +- **Type-safe flag definitions**: Each method returns a properly typed adapter |
| 110 | +- **Automatic initialization**: Flagsmith client can be lazily initialized |
| 111 | +- **Identity support**: Full support for Flagsmith identity and traits |
| 112 | +- **Default value handling**: Proper fallback to default values when flags are disabled or not found |
| 113 | +- **Boolean flag optimization**: Boolean flags use the `enabled` state directly for better performance |
| 114 | + |
| 115 | +## Environment Variables |
| 116 | + |
| 117 | +- `FLAGSMITH_ENVIRONMENT_ID` (required): Your Flagsmith environment ID |
| 118 | + |
| 119 | +## License |
| 120 | + |
| 121 | +MIT |
0 commit comments