Skip to content

Commit dac7f7f

Browse files
authored
feat: type opt-in value as Boolean across client SDKs (#846)
Follow-up to #844. This commit was pushed after #844 merged, so it didn't land. Opt-in only applies to Boolean flags, so opt-in entries now have `value: boolean` (computed as `value === true`) instead of any JSON value. This covers the Browser, React, Vue and React Native SDKs, which share the Browser SDK's `OptInFlag` type. - Type tests in all four SDKs check that `flag.value` is a `boolean` and that `flag.isEnabled` is gone. The Browser type test also checks that `getFeatures()` is removed. - READMEs and changesets say "a Boolean `value`". Browser, React, Vue and React Native tests pass, including type tests. Co-authored-by: Ron Cohen <744+roncohen@users.noreply.github.com>
1 parent b29594d commit dac7f7f

12 files changed

Lines changed: 34 additions & 14 deletions

File tree

‎.changeset/browser-opt-in-value.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"@reflag/browser-sdk": major
33
---
44

5-
Opt-in entries from `getOptInFlags()` now include the flag's `value` instead of `isEnabled`. The deprecated `getFeatures()` alias is removed; use `getFlags()`.
5+
Opt-in entries from `getOptInFlags()` now have a Boolean `value` instead of `isEnabled`. The deprecated `getFeatures()` alias is removed; use `getFlags()`.

‎.changeset/opt-in-set-from-hook.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
"@reflag/react-native-sdk": major
55
---
66

7-
`useOptInFlags()` now returns `setOptIn` along with `flags` and `isLoading`. `useSetOptIn()` still works but is deprecated. Opt-in entries now include the flag's `value` instead of `isEnabled`. The deprecated `useFeature()` alias is removed from React and React Native; use `useFlag()`.
7+
`useOptInFlags()` now returns `setOptIn` along with `flags` and `isLoading`. `useSetOptIn()` still works but is deprecated. Opt-in entries now have a Boolean `value` instead of `isEnabled`. The deprecated `useFeature()` alias is removed from React and React Native; use `useFlag()`.

‎packages/browser-sdk/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ try {
122122
}
123123
```
124124

125-
Each entry includes the flag's `key`, `name`, `description` and `value`, and whether the user or company has opted in. User and company opt-ins are independent, and the user or company you opt in must have an `id`. `setOptIn()` resolves once the new flag values have loaded. Check `response.ok` and catch errors, as in the example. If the list fails to load, it stays empty; call `reflag.refresh()` to retry.
125+
Each entry includes the flag's `key`, `name`, `description`, a Boolean `value`, and whether the user or company has opted in. User and company opt-ins are independent, and the user or company you opt in must have an `id`. `setOptIn()` resolves once the new flag values have loaded. Check `response.ok` and catch errors, as in the example. If the list fails to load, it stays empty; call `reflag.refresh()` to retry.
126126

127127
## Tracking, feedback and events
128128

@@ -137,7 +137,7 @@ Use `track(eventName, attributes?)`, `feedback(payload)` and `requestFeedback({
137137
- Use the client's `track()` and `requestFeedback()` instead of the flag's.
138138
- `setFlagOverride(key, value)` takes the value directly; pass `undefined` to remove an override.
139139
- `getFlags()` returns all flag values and records a check for each. Replace `getFeatures()` and `getFeature(key)` with `getFlags()` and `getFlag(key)`; the feature-named methods are removed.
140-
- Opt-in entries from `getOptInFlags()` have the flag's `value` instead of `isEnabled`.
140+
- Opt-in entries from `getOptInFlags()` have a Boolean `value` instead of `isEnabled`.
141141
- Remove `fallbackFlags` and pass a default to each read instead, such as `getFlag("huddles", true)`.
142142
- If you bootstrap from the server, upgrade the Node SDK that produces the bootstrap data to v2 at the same time.
143143
- If you generate type declarations, regenerate them with the v2 CLI.

‎packages/browser-sdk/src/client.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,7 @@ export class ReflagClient {
12101210
{
12111211
key,
12121212
...metadata,
1213-
value: flag.value,
1213+
value: flag.value === true,
12141214
} satisfies OptInFlag,
12151215
]
12161216
: [];

‎packages/browser-sdk/src/flag/flags.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ export type OptInFlag = RawFlagOptIn & {
5656
key: string;
5757

5858
/**
59-
* The flag's value for the current context, or `undefined` if it has none.
59+
* Whether the flag is on for the current context. Opt-in flags are always Boolean.
6060
*/
61-
value: FlagValue | undefined;
61+
value: boolean;
6262
};
6363

6464
export type EvaluationError = {

‎packages/browser-sdk/type-tests/defaults.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ const fallback: Settings = { model: "quick", limits: [1] };
2222
const _interface: Settings = client.getFlag("settings", fallback);
2323
// @ts-expect-error literal defaults widen to string, not a specific authored variant
2424
const _literal: "quick" = client.getFlag("model", "quick");
25+
26+
for (const flag of client.getOptInFlags()) {
27+
const _on: boolean = flag.value;
28+
// @ts-expect-error opt-in entries use value instead of isEnabled
29+
void flag.isEnabled;
30+
}
31+
// @ts-expect-error getFeatures was removed; use getFlags
32+
client.getFeatures();

‎packages/react-native-sdk/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ try {
187187
}
188188
```
189189

190-
Each entry includes the flag's `key`, `name`, `description` and `value`, and whether the user or company has opted in. User and company opt-ins are independent, and the user or company you opt in must have an `id`. `setOptIn()` resolves once the new flag values have loaded. Check `response.ok` and catch errors, as in the example. If the list fails to load, it stays empty; call `useClient().refresh()` to retry.
190+
Each entry includes the flag's `key`, `name`, `description`, a Boolean `value`, and whether the user or company has opted in. User and company opt-ins are independent, and the user or company you opt in must have an `id`. `setOptIn()` resolves once the new flag values have loaded. Check `response.ok` and catch errors, as in the example. If the list fails to load, it stays empty; call `useClient().refresh()` to retry.
191191

192192
Pass `{ suspense: true }` to `useOptInFlags()` to suspend until the list loads. It defaults to the provider's `suspense` prop.
193193

@@ -203,7 +203,7 @@ See the [Expo](https://github.com/reflagcom/javascript/tree/main/packages/react-
203203
- Replace `useFlag(key, { suspense: true })` with `useFlagSuspense(key)`, and the flag's loading property with `useIsLoading()`.
204204
- `useFeature()` is removed. Use `useFlag()` instead.
205205
- Use the `setOptIn` function returned by `useOptInFlags()`. `useSetOptIn()` still works but is deprecated.
206-
- Opt-in entries have the flag's `value` instead of `isEnabled`.
206+
- Opt-in entries have a Boolean `value` instead of `isEnabled`.
207207
- Remove `fallbackFlags` and pass a default to each read instead, such as `useFlag("huddles", true)`.
208208
- If you bootstrap from the server, upgrade the Node SDK that produces the bootstrap data to v2 at the same time.
209209
- If you generate type declarations, regenerate them with the v2 CLI.

‎packages/react-native-sdk/type-tests/registered.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@ export function RegisteredNativeContracts() {
5959
mutable.prompt.tone = "detailed";
6060
mutable.methods.push("express");
6161
const { flags, setOptIn } = useOptInFlags();
62-
for (const flag of flags)
62+
for (const flag of flags) {
6363
void setOptIn(flag.key, { optedIn: !flag.userOptedIn });
64+
const _on: boolean = flag.value;
65+
// @ts-expect-error opt-in entries use value instead of isEnabled
66+
void flag.isEnabled;
67+
}
6468
// @ts-expect-error Registered keys constrain native reads.
6569
useFlag("unknown");
6670
// @ts-expect-error Another numeric flag must not permit a wrong-category default.

‎packages/react-sdk/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ try {
146146
}
147147
```
148148

149-
Each entry includes the flag's `key`, `name`, `description` and `value`, and whether the user or company has opted in. User and company opt-ins are independent, and the user or company you opt in must have an `id`. `setOptIn()` resolves once the new flag values have loaded. Check `response.ok` and catch errors, as in the example. If the list fails to load, it stays empty; call `useClient().refresh()` to retry.
149+
Each entry includes the flag's `key`, `name`, `description`, a Boolean `value`, and whether the user or company has opted in. User and company opt-ins are independent, and the user or company you opt in must have an `id`. `setOptIn()` resolves once the new flag values have loaded. Check `response.ok` and catch errors, as in the example. If the list fails to load, it stays empty; call `useClient().refresh()` to retry.
150150

151151
Pass `{ suspense: true }` to `useOptInFlags()` to suspend until the list loads. It defaults to the provider's `suspense` prop.
152152

@@ -158,7 +158,7 @@ Pass `{ suspense: true }` to `useOptInFlags()` to suspend until the list loads.
158158
- Replace `useFlag(key, { suspense: true })` with `useFlagSuspense(key)`, and the flag's loading property with `useIsLoading()`.
159159
- `useFeature()` is removed. Use `useFlag()` instead.
160160
- Use the `setOptIn` function returned by `useOptInFlags()`. `useSetOptIn()` still works but is deprecated.
161-
- Opt-in entries have the flag's `value` instead of `isEnabled`.
161+
- Opt-in entries have a Boolean `value` instead of `isEnabled`.
162162
- Remove `fallbackFlags` and pass a default to each read instead, such as `useFlag("huddles", true)`.
163163
- If you bootstrap from the server, upgrade the Node SDK that produces the bootstrap data to v2 at the same time.
164164
- If you generate type declarations, regenerate them with the v2 CLI.

‎packages/react-sdk/test/opt-in-types.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ export function GeneratedOptInTypes() {
1212

1313
for (const flag of flags) {
1414
void setOptIn(flag.key, { optedIn: !flag.userOptedIn });
15+
const on: boolean = flag.value;
16+
void on;
17+
// @ts-expect-error opt-in entries use value instead of isEnabled
18+
void flag.isEnabled;
1519
}
1620

1721
// @ts-expect-error generated flag types still reject unknown literal keys

0 commit comments

Comments
 (0)