Skip to content

Commit c29b292

Browse files
committed
refactor: update README and code to reflect flag terminology changes
- Replaced instances of `getFeature` with `getFlag` in the README and example files to align with the new flag-based approach. - Updated the `Feedback` component to reference `ExampleFlag` instead of `ExampleFeature`. - Adjusted the `ReflagClient` class to ensure consistent usage of `flagKey` in feedback requests and related methods. - Enhanced documentation to clarify the transition from features to flags, including migration instructions for users moving from the Bucket SDK to the Reflag SDK.
1 parent b5eafed commit c29b292

9 files changed

Lines changed: 151 additions & 331 deletions

File tree

packages/browser-sdk/README.md

Lines changed: 92 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,40 @@ const reflagClient = new ReflagClient({ publishableKey, user, company });
2929

3030
await reflagClient.initialize();
3131

32-
const {
33-
isEnabled,
34-
config: { payload: question },
35-
track,
36-
requestFeedback,
37-
} = reflagClient.getFeature("huddle");
38-
39-
if (isEnabled) {
40-
// Show feature. When retrieving `isEnabled` the client automatically
32+
const huddleFlag = reflagClient.getFlag("huddle");
33+
34+
if (typeof huddleFlag === "boolean") {
35+
// Simple toggle flag
36+
if (huddleFlag) {
37+
// Show flag. When retrieving the flag value the client automatically
38+
// sends a "check" event for the "huddle" flag which is shown in the
39+
// Reflag UI.
40+
41+
// On usage, call `track` to let Reflag know that a user interacted with the flag
42+
reflagClient.track("huddle");
43+
44+
// Use `requestFeedback` to create "Send feedback" buttons easily for specific
45+
// flags. This is not related to `track` and you can call them individually.
46+
reflagClient.requestFeedback({ flagKey: "huddle" });
47+
}
48+
} else {
49+
// Multi-variate flag with config
50+
const { key, payload } = huddleFlag;
51+
52+
// Show flag. When retrieving the flag value the client automatically
4153
// sends a "check" event for the "huddle" flag which is shown in the
4254
// Reflag UI.
4355

4456
// On usage, call `track` to let Reflag know that a user interacted with the flag
45-
track();
57+
reflagClient.track("huddle");
4658

4759
// The `payload` is a user-supplied JSON in Reflag that is dynamically picked
4860
// out depending on the user/company.
4961
const question = payload?.question ?? "Tell us what you think of Huddles";
5062

5163
// Use `requestFeedback` to create "Send feedback" buttons easily for specific
5264
// flags. This is not related to `track` and you can call them individually.
53-
requestFeedback({ title: question });
65+
reflagClient.requestFeedback({ flagKey: "huddle", title: question });
5466
}
5567

5668
// `track` just calls `reflagClient.track(<flagKey>)` to send an event using the same flag key
@@ -102,7 +114,7 @@ type Configuration = {
102114
sseBaseUrl?: "https://livemessaging.bucket.co";
103115
feedback?: undefined; // See FEEDBACK.md
104116
enableTracking?: true; // set to `false` to stop sending track events and user/company updates to Reflag servers. Useful when you're impersonating a user
105-
fallbackFeatures?:
117+
fallbackFlags?:
106118
| string[]
107119
| Record<string, { key: string; payload: any } | true>; // Enable these flags if unable to contact reflag.com. Can be a list of flag keys or a record with configuration values
108120
timeoutMs?: number; // Timeout for fetching flags (default: 5000ms)
@@ -113,7 +125,7 @@ type Configuration = {
113125
};
114126
```
115127

116-
## Feature toggles
128+
## Flag targeting
117129

118130
Reflag determines which flags are active for a given user/company. The user/company is given in the `ReflagClient` constructor.
119131

@@ -144,64 +156,28 @@ const reflagClient = new ReflagClient({
144156
});
145157
```
146158

147-
To retrieve flags along with their targeting information, use `getFeature(flagKey: string)`:
159+
To retrieve the value of a flag use `getFlag(flagKey: string)`:
148160

149161
```ts
150-
const huddle = reflagClient.getFeature("huddle");
151-
// {
152-
// isEnabled: true,
153-
// config: { key: "zoom", payload: { ... } },
154-
// track: () => Promise<Response>
155-
// requestFeedback: (options: RequestFeedbackData) => void
156-
// }
162+
const huddle = reflagClient.getFlag("huddle");
163+
// Returns either:
164+
// - boolean (for simple toggle flags)
165+
// - { key: string, payload: any } (for multi-variate flags)
157166
```
158167

159-
You can use `getFeatures()` to retrieve all enabled flags currently.
168+
You can use `getFlags()` to retrieve all flags:
160169

161170
```ts
162-
const flags = reflagClient.getFeatures();
171+
const flags = reflagClient.getFlags();
163172
// {
164173
// huddle: {
165-
// isEnabled: true,
166-
// targetingVersion: 42,
167-
// config: ...
174+
// - boolean (for simple toggle flags)
175+
// - { key: string, payload: any } (for multi-variate flags)
168176
// }
177+
// ...
169178
// }
170179
```
171180

172-
`getFeatures()` is meant to be more low-level than `getFeature()` and it typically used
173-
by down-stream clients, like the React SDK.
174-
175-
Note that accessing `isEnabled` on the object returned by `getFeatures` does not automatically
176-
generate a `check` event, contrary to the `isEnabled` property on the object returned by `getFeature`.
177-
178-
## Remote config
179-
180-
Remote config is a dynamic and flexible approach to configuring flag behavior outside of your app – without needing to re-deploy it.
181-
182-
Similar to `isEnabled`, each flag has a `config` property. This configuration is managed from within Reflag.
183-
It is managed similar to the way access to flags is managed, but instead of the binary `isEnabled` you can have
184-
multiple configuration values which are given to different user/companies.
185-
186-
```ts
187-
const flags = reflagClient.getFeatures();
188-
// {
189-
// huddle: {
190-
// isEnabled: true,
191-
// targetingVersion: 42,
192-
// config: {
193-
// key: "gpt-3.5",
194-
// payload: { maxTokens: 10000, model: "gpt-3.5-beta1" }
195-
// }
196-
// }
197-
// }
198-
```
199-
200-
`key` is mandatory for a config, but if a flag has no config or no config value was matched against the context, the `key` will be `undefined`. Make sure to check against this case when trying to use the configuration in your application. `payload` is an optional JSON value for arbitrary configuration needs.
201-
202-
Just as `isEnabled`, accessing `config` on the object returned by `getFeatures` does not automatically
203-
generate a `check` event, contrary to the `config` property on the object returned by `getFeature`.
204-
205181
## Updating user/company/other context
206182

207183
Attributes given for the user/company/other context in the `ReflagClient` constructor can be updated for use in flag targeting evaluation with the `updateUser()`, `updateCompany()` and `updateOtherContext()` methods.
@@ -211,7 +187,8 @@ The following shows how to let users self-opt-in for a new flag. The flag must h
211187

212188
```ts
213189
// toggle opt-in for the voiceHuddle flag:
214-
const { isEnabled } = reflagClient.getFeature("voiceHuddle");
190+
const isEnabled = reflagClient.getFlag("voiceHuddle");
191+
215192
// this toggles the flag on/off. The promise returns once flag targeting has been
216193
// re-evaluated.
217194
await reflagClient.updateUser({ voiceHuddleOptIn: (!isEnabled).toString() });
@@ -310,15 +287,15 @@ reflagClient.track("huddle", { voiceHuddle: true });
310287
Event listeners allow for capturing various events occurring in the `ReflagClient`. This is useful to build integrations with other system or for various debugging purposes. There are 5 kinds of events:
311288

312289
- `check`: Your code used `isEnabled` or `config` for a flag
313-
- `flagsUpdated`: Features were updated. Either because they were loaded as part of initialization or because the user/company updated
290+
- `flagsUpdated`: Flags were updated. Either because they were loaded as part of initialization or because the user/company updated
314291
- `user`: User information updated (similar to the `identify` call used in tracking terminology)
315292
- `company`: Company information updated (sometimes to the `group` call used in tracking terminology)
316293
- `track`: Track event occurred.
317294

318295
Use the `on()` method to add an event listener to respond to certain events. See the API reference for details on each hook.
319296

320297
```ts
321-
import { ReflagClient, CheckEvent, RawFeatures } from "@reflag/browser-sdk";
298+
import { ReflagClient, CheckEvent, RawFlags } from "@reflag/browser-sdk";
322299

323300
const client = new ReflagClient({
324301
// options
@@ -355,13 +332,6 @@ The two cookies are:
355332
- `reflag-prompt-${userId}`: store the last automated feedback prompt message ID received to avoid repeating surveys
356333
- `reflag-token-${userId}`: caching a token used to connect to Reflag's live messaging infrastructure that is used to deliver automated feedback surveys in real time.
357334

358-
## Upgrading to 3.0 from 2.x
359-
360-
Breaking changes:
361-
362-
- `client.onFeaturesUpdated()` is now replaced by [event listeners](#event-listeners)
363-
- Arguments to the `ReflagClient` constructor which were previously under `flagOptions` are now supplied directly in the root.
364-
365335
## TypeScript
366336

367337
Types are bundled together with the library and exposed automatically when importing through a package manager.
@@ -382,6 +352,59 @@ If you are including the Reflag tracking SDK with a `<script>`-tag from `jsdeliv
382352
| --------------- | ---------------------------------------------------- | ------------------------------- |
383353
| script-src-elem | [https://cdn.jsdelivr.net](https://cdn.jsdelivr.net) | Loads the Reflag SDK from a CDN |
384354

355+
## Migration from Bucket SDK to Reflag SDK
356+
357+
If you're migrating from the legacy Bucket SDK to the new Reflag SDK, here are the key changes you need to make:
358+
359+
### General
360+
361+
- **`BucketClient`****`ReflagClient`**
362+
- **`BucketContext`****`ReflagContext`**
363+
- **`featureKey`****`flagKey`**
364+
- **`featureId`** was dropped
365+
366+
### Feature to Flag conversion
367+
368+
- **`getFeature()`****`getFlag()`** (`ReflagClient`)
369+
- **`getFeatures()`****`getFlags()`** (`ReflagClient`)
370+
371+
**Important**: The new methods return the flag values directly (boolean or object), not an object with methods.
372+
The methods that were previously returned by `getFeature()` or `getFeatures()` are now available as separate methods:
373+
374+
- **`Feature.isEnabled`****`getFlag()`** (returns boolean for "toggle" flags)
375+
- **`Feature.config`****`getFlag()`** (returns object for "multi-variate" flags)
376+
- **`Feature.track`****`track()`** (separate method)
377+
- **`Feature.requestFeedback`****`requestFeedback()`** (separate method)
378+
- **`Feature.isEnabledOverride`****`getFlagOverride()`** (separate method)
379+
- **`Feature.setIsEnabledOverride`****`setFlagOverride()`** (separate method)
380+
381+
### Configuration changes
382+
383+
- **`fallbackFeatures`****`fallbackFlags`**
384+
385+
```typescript
386+
// Old
387+
const client = new ReflagClient({
388+
publishableKey,
389+
fallbackFeatures: ["flag1", "flag2"],
390+
});
391+
392+
// New
393+
const client = new ReflagClient({
394+
publishableKey,
395+
fallbackFlags={{
396+
"flag1": true,
397+
"flag2": { key: "variant-a", payload: { limit: 100 } }
398+
}}
399+
});
400+
```
401+
402+
### Event hook changes
403+
404+
- **`featuresUpdated`****`flagsUpdated`**
405+
- **`enabledCheck`****`check`** (use the unified `check` event instead)
406+
- **`configCheck`****`check`** (use the unified `check` event instead)
407+
385408
## License
386409

387410
> MIT License

packages/browser-sdk/example/feedback/Feedback.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const FeedbackForm = () => {
3232

3333
return (
3434
<form action="#" onSubmit={handleSubmit}>
35-
<h2>How satisfied are you with our ExampleFeature?</h2>
35+
<h2>How satisfied are you with our ExampleFlag?</h2>
3636

3737
<fieldset>
3838
<legend>Satisfaction</legend>

0 commit comments

Comments
 (0)