From ce4d8c97239322da8c7723ca495c5a9b32866b5e Mon Sep 17 00:00:00 2001 From: Ron Cohen <744+roncohen@users.noreply.github.com> Date: Sun, 29 Nov 2026 18:37:40 +0000 Subject: [PATCH 1/3] feat: return setOptIn from useOptInFlags and drop feature-named aliases --- .changeset/opt-in-set-from-hook.md | 8 +++++ packages/browser-sdk/README.md | 5 +-- packages/browser-sdk/src/client.ts | 9 +----- packages/browser-sdk/src/flag/flags.ts | 4 +-- packages/browser-sdk/test/client.test.ts | 8 ++--- packages/node-sdk/src/edgeClient.ts | 2 +- packages/react-native-sdk/src/index.tsx | 1 - packages/react-native-sdk/test/usage.test.tsx | 4 +-- .../react-native-sdk/type-tests/defaults.tsx | 6 +--- .../type-tests/registered.tsx | 5 ++- packages/react-sdk/README.md | 8 +++-- packages/react-sdk/src/index.tsx | 32 +++++++++++++++---- packages/react-sdk/test/opt-in-types.tsx | 10 ++---- packages/react-sdk/test/usage.test.tsx | 7 ++-- packages/vue-sdk/README.md | 7 ++-- packages/vue-sdk/src/hooks.ts | 14 ++++++-- packages/vue-sdk/src/types.ts | 15 ++++++++- packages/vue-sdk/test/opt-in-types.ts | 5 ++- packages/vue-sdk/test/usage.test.ts | 9 ++++-- 19 files changed, 97 insertions(+), 62 deletions(-) create mode 100644 .changeset/opt-in-set-from-hook.md diff --git a/.changeset/opt-in-set-from-hook.md b/.changeset/opt-in-set-from-hook.md new file mode 100644 index 000000000..95b88ef5f --- /dev/null +++ b/.changeset/opt-in-set-from-hook.md @@ -0,0 +1,8 @@ +--- +"@reflag/browser-sdk": major +"@reflag/react-sdk": major +"@reflag/vue-sdk": major +"@reflag/react-native-sdk": major +--- + +`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()` and `getFeatures()` aliases are removed; use `useFlag()` and `getFlags()`. diff --git a/packages/browser-sdk/README.md b/packages/browser-sdk/README.md index 8474499a6..be5331751 100644 --- a/packages/browser-sdk/README.md +++ b/packages/browser-sdk/README.md @@ -122,7 +122,7 @@ try { } ``` -Each entry includes the flag's `key`, `name`, `description` 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. +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. ## Tracking, feedback and events @@ -136,7 +136,8 @@ Use `track(eventName, attributes?)`, `feedback(payload)` and `requestFeedback({ - Move remote config to a multivariate flag and read it with a default, such as `getFlag("model", "quick")`. Existing remote config isn't converted automatically. - Use the client's `track()` and `requestFeedback()` instead of the flag's. - `setFlagOverride(key, value)` takes the value directly; pass `undefined` to remove an override. -- `getFlags()` returns all flag values and records a check for each. +- `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. +- Opt-in entries from `getOptInFlags()` have the flag's `value` instead of `isEnabled`. - Remove `fallbackFlags` and pass a default to each read instead, such as `getFlag("huddles", true)`. - If you bootstrap from the server, upgrade the Node SDK that produces the bootstrap data to v2 at the same time. - If you generate type declarations, regenerate them with the v2 CLI. diff --git a/packages/browser-sdk/src/client.ts b/packages/browser-sdk/src/client.ts index d5cb1699f..ef66f4816 100644 --- a/packages/browser-sdk/src/client.ts +++ b/packages/browser-sdk/src/client.ts @@ -1151,13 +1151,6 @@ export class ReflagClient { }, 1); } - /** - * @deprecated Use `getFlags` instead. - */ - getFeatures() { - return this.getFlags(); - } - /** * Return direct values and register checks for these returned roots. * Use getFlagResults() for diagnostic/serialization access without checks. @@ -1217,7 +1210,7 @@ export class ReflagClient { { key, ...metadata, - isEnabled: flag.value === true, + value: flag.value, } satisfies OptInFlag, ] : []; diff --git a/packages/browser-sdk/src/flag/flags.ts b/packages/browser-sdk/src/flag/flags.ts index 3d3603cfa..48abdfc86 100644 --- a/packages/browser-sdk/src/flag/flags.ts +++ b/packages/browser-sdk/src/flag/flags.ts @@ -56,9 +56,9 @@ export type OptInFlag = RawFlagOptIn & { key: string; /** - * Result of flag evaluation. + * The flag's value for the current context, or `undefined` if it has none. */ - isEnabled: boolean; + value: FlagValue | undefined; }; export type EvaluationError = { diff --git a/packages/browser-sdk/test/client.test.ts b/packages/browser-sdk/test/client.test.ts index 0311538f8..7cabaed00 100644 --- a/packages/browser-sdk/test/client.test.ts +++ b/packages/browser-sdk/test/client.test.ts @@ -514,7 +514,7 @@ describe("bootstrap and opt-in boundaries", () => { key: "flagA", name: "Try it", description: null, - isEnabled: true, + value: true, userOptedIn: true, companyOptedIn: false, isOptedIn: true, @@ -573,7 +573,7 @@ describe("bootstrap and opt-in boundaries", () => { expect(client.getFlag("flagA")).toBe(false); expect(client.getFlagsForBootstrap().flagStateVersion).toBe(2); expect(client.getOptInFlags()).toEqual([ - expect.objectContaining({ userOptedIn: true, isEnabled: false }), + expect.objectContaining({ userOptedIn: true, value: false }), ]); expect(client.getIsLoadingOptInFlags()).toBe(false); expect(request).toHaveBeenCalledOnce(); @@ -665,7 +665,7 @@ describe("bootstrap and opt-in boundaries", () => { key: "flagA", name: "Try", description: null, - isEnabled: false, + value: false, userOptedIn: false, companyOptedIn: true, isOptedIn: true, @@ -774,7 +774,7 @@ describe("bootstrap and opt-in boundaries", () => { expect(updated).toHaveReturnedWith([ expect.objectContaining({ key: "flagA", - isEnabled: override, + value: override, userOptedIn: true, }), ]); diff --git a/packages/node-sdk/src/edgeClient.ts b/packages/node-sdk/src/edgeClient.ts index 8d9baea1a..e3211d57a 100644 --- a/packages/node-sdk/src/edgeClient.ts +++ b/packages/node-sdk/src/edgeClient.ts @@ -26,7 +26,7 @@ export type EdgeClientOptions = Omit< * user: { id: "user-id" }, * company: { id: "company-id" }, * } - * const { isEnabled } = client.getFlag(context, "flag-key"); + * const enabled = client.getFlag(context, "flag-key"); * * ``` */ diff --git a/packages/react-native-sdk/src/index.tsx b/packages/react-native-sdk/src/index.tsx index a19c00e0b..269b5a94d 100644 --- a/packages/react-native-sdk/src/index.tsx +++ b/packages/react-native-sdk/src/index.tsx @@ -25,7 +25,6 @@ const defaultEventSourceFactory: NonNullable< export { ReflagClientProvider, useClient, - useFeature, useFlag, useFlagSuspense, useIsLoading, diff --git a/packages/react-native-sdk/test/usage.test.tsx b/packages/react-native-sdk/test/usage.test.tsx index 89bba34dc..10ed0c412 100644 --- a/packages/react-native-sdk/test/usage.test.tsx +++ b/packages/react-native-sdk/test/usage.test.tsx @@ -559,7 +559,7 @@ describe("React Native v2", () => { test("loads opt-in metadata without replacing bootstrap values, then confirms v2 opt-in mutations", async () => { let result: ReturnType; - let setOptIn: ReturnType; + let setOptIn: ReturnType["setOptIn"]; let enabled: boolean; let changed = false; fetchMock.mockImplementation(async (input, options) => { @@ -592,7 +592,7 @@ describe("React Native v2", () => { }); function Read() { result = useOptInFlags(); - setOptIn = useSetOptIn(); + setOptIn = result.setOptIn; enabled = useFlag("release"); return null; } diff --git a/packages/react-native-sdk/type-tests/defaults.tsx b/packages/react-native-sdk/type-tests/defaults.tsx index 1dc5c2560..b1a096fc9 100644 --- a/packages/react-native-sdk/type-tests/defaults.tsx +++ b/packages/react-native-sdk/type-tests/defaults.tsx @@ -1,11 +1,9 @@ import { ReflagBootstrappedProvider, ReflagProvider, - useFeature, useFlag, useFlagSuspense, useOptInFlags, - useSetOptIn, useTrack, type BootstrappedFlags, type FlagDefaultValue, @@ -30,7 +28,6 @@ const state: BootstrappedFlags = { export function NativeContracts() { const enabled: boolean = useFlag("release"); const _undefinedDefault: boolean = useFlag("release", undefined); - const _legacyAlias: boolean = useFeature("release"); const _suspended: boolean = useFlagSuspense("release"); const _direct: Settings = useFlag("settings", settings); const _ready: Settings = useFlagSuspense("settings", settings); @@ -49,9 +46,8 @@ export function NativeContracts() { mutable.limits.push(10); const options: UseOptInFlagsOptions = { suspense: false }; const optIn: UseOptInFlagsResult = useOptInFlags(options); - const setOptIn = useSetOptIn(); for (const flag of optIn.flags) - void setOptIn(flag.key, { optedIn: !flag.userOptedIn }); + void optIn.setOptIn(flag.key, { optedIn: !flag.userOptedIn }); const track = useTrack(); void track("Used native flag", { screen: "home" }); // @ts-expect-error Direct-value reads do not expose v1 wrappers. diff --git a/packages/react-native-sdk/type-tests/registered.tsx b/packages/react-native-sdk/type-tests/registered.tsx index f3987648e..ae698d7fe 100644 --- a/packages/react-native-sdk/type-tests/registered.tsx +++ b/packages/react-native-sdk/type-tests/registered.tsx @@ -2,7 +2,6 @@ import { useFlag, useFlagSuspense, useOptInFlags, - useSetOptIn, type FlagKey, type Flags as NativeFlags, type FlagValue, @@ -59,8 +58,8 @@ export function RegisteredNativeContracts() { mutable.model = "quality"; mutable.prompt.tone = "detailed"; mutable.methods.push("express"); - const setOptIn = useSetOptIn(); - for (const flag of useOptInFlags().flags) + const { flags, setOptIn } = useOptInFlags(); + for (const flag of flags) void setOptIn(flag.key, { optedIn: !flag.userOptedIn }); // @ts-expect-error Registered keys constrain native reads. useFlag("unknown"); diff --git a/packages/react-sdk/README.md b/packages/react-sdk/README.md index b716cfa9e..b1f636ea7 100644 --- a/packages/react-sdk/README.md +++ b/packages/react-sdk/README.md @@ -137,8 +137,7 @@ To use a client you create yourself, pass it to `ReflagClientProvider`. Your app ## Opt-in hooks ```tsx -const { flags, isLoading } = useOptInFlags(); -const setOptIn = useSetOptIn(); +const { flags, isLoading, setOptIn } = useOptInFlags(); try { const response = await setOptIn("huddles", { optedIn: true }); if (!response?.ok) console.error("Could not update opt-in"); @@ -147,7 +146,7 @@ try { } ``` -Each entry includes the flag's `key`, `name`, `description` 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. +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. Pass `{ suspense: true }` to `useOptInFlags()` to suspend until the list loads. It defaults to the provider's `suspense` prop. @@ -157,6 +156,9 @@ Pass `{ suspense: true }` to `useOptInFlags()` to suspend until the list loads. - Move remote config to a multivariate flag and read it with a default, such as `useFlag("model", "quick")`. Existing remote config isn't converted automatically. - Use `useTrack()` and `useRequestFeedback()` instead of the flag's `track()` and `requestFeedback()`. - Replace `useFlag(key, { suspense: true })` with `useFlagSuspense(key)`, and the flag's loading property with `useIsLoading()`. +- `useFeature()` is removed. Use `useFlag()` instead. +- Use the `setOptIn` function returned by `useOptInFlags()`. `useSetOptIn()` still works but is deprecated. +- Opt-in entries have the flag's `value` instead of `isEnabled`. - Remove `fallbackFlags` and pass a default to each read instead, such as `useFlag("huddles", true)`. - If you bootstrap from the server, upgrade the Node SDK that produces the bootstrap data to v2 at the same time. - If you generate type declarations, regenerate them with the v2 CLI. diff --git a/packages/react-sdk/src/index.tsx b/packages/react-sdk/src/index.tsx index 98729ade3..abd073cf1 100644 --- a/packages/react-sdk/src/index.tsx +++ b/packages/react-sdk/src/index.tsx @@ -4,6 +4,7 @@ import { deepEqual } from "fast-equals"; import React, { createContext, ReactNode, + useCallback, useContext, useEffect, useLayoutEffect, @@ -65,7 +66,7 @@ export type TypedFlags = keyof Flags extends never * An opt-in-enabled flag for the generated React SDK flag definitions. * * Includes all fields from {@link @reflag/browser-sdk!OptInFlag}: `name`, `description`, - * `isEnabled`, `userOptedIn`, `companyOptedIn`, and `isOptedIn`. + * `value`, `userOptedIn`, `companyOptedIn`, and `isOptedIn`. * Only `key` is narrowed to the generated {@link FlagKey} type. */ export type OptInFlag = Omit & { @@ -612,6 +613,19 @@ export type UseOptInFlagsResult = { * Whether a bootstrapped provider is fetching opt-in metadata. */ isLoading: boolean; + + /** + * Sets whether the current user or company has opted into a flag. + * + * Check the returned Response's `ok` property and catch promise rejections. + * HTTP failures return a non-OK Response; offline mode, invalid arguments, or + * missing scoped context return undefined. Confirmation failures can reject + * after the membership changed remotely. See {@link @reflag/browser-sdk!ReflagClient.setOptIn}. + */ + setOptIn: ( + key: FlagKey, + options: SetOptInOptions, + ) => Promise; }; /** Boolean-first, non-suspending reads. Supply a positional default for JSON values. */ @@ -651,9 +665,6 @@ export function useFlagSuspense( return useFlagValue(key, defaultValue, true); } -/** @deprecated Use useFlag. */ -export const useFeature = useFlag; - function useFlagValue( key: FlagKey, defaultValue: FlagDefaultValue, @@ -698,7 +709,8 @@ function useFlagValue( } /** - * Returns opt-in-enabled flags and their loading state for the current context. + * Returns opt-in-enabled flags, their loading state, and a `setOptIn` function + * for the current context. * * The loading state is only used with `ReflagBootstrappedProvider` while * opt-in metadata is fetched on demand. Regular providers load opt-in metadata @@ -734,6 +746,12 @@ export function useOptInFlags( setIsOptInFlagsLoading(client.getIsLoadingOptInFlags()); }, [client]); + const setOptIn = useCallback( + (key: FlagKey, optInOptions: SetOptInOptions) => + client.setOptIn(String(key), optInOptions), + [client], + ); + const suspense = options.suspense ?? context.suspense; if (suspense && context.isLoading && isClientLoading(client)) { throw context.getLoadingPromise(); @@ -744,12 +762,14 @@ export function useOptInFlags( throw getOptInFlagsLoadingPromise(client); } - return { flags, isLoading }; + return { flags, isLoading, setOptIn }; } /** * Returns a function to set whether the current user or company has opted into a flag. * + * @deprecated Use the `setOptIn` function returned by {@link useOptInFlags}. + * * Check the returned Response's `ok` property and catch promise rejections. * HTTP failures return a non-OK Response; offline mode, invalid arguments, or * missing scoped context return undefined. Confirmation failures can reject diff --git a/packages/react-sdk/test/opt-in-types.tsx b/packages/react-sdk/test/opt-in-types.tsx index d8137db74..6b83340fb 100644 --- a/packages/react-sdk/test/opt-in-types.tsx +++ b/packages/react-sdk/test/opt-in-types.tsx @@ -1,9 +1,4 @@ -import { - useFlag, - useFlagSuspense, - useOptInFlags, - useSetOptIn, -} from "@reflag/react-sdk"; +import { useFlag, useFlagSuspense, useOptInFlags } from "@reflag/react-sdk"; declare module "@reflag/react-sdk" { interface Flags { @@ -13,8 +8,7 @@ declare module "@reflag/react-sdk" { } export function GeneratedOptInTypes() { - const { flags } = useOptInFlags(); - const setOptIn = useSetOptIn(); + const { flags, setOptIn } = useOptInFlags(); for (const flag of flags) { void setOptIn(flag.key, { optedIn: !flag.userOptedIn }); diff --git a/packages/react-sdk/test/usage.test.tsx b/packages/react-sdk/test/usage.test.tsx index d43198a3c..016198db8 100644 --- a/packages/react-sdk/test/usage.test.tsx +++ b/packages/react-sdk/test/usage.test.tsx @@ -694,7 +694,7 @@ describe("opt-in hooks", () => { await waitFor(() => expect(result.current.isLoading).toBe(false)); expect(result.current.flags[0]).toMatchObject({ key: "release", - isEnabled: true, + value: true, userOptedIn: true, }); }); @@ -735,7 +735,6 @@ describe("opt-in hooks", () => { () => ({ value: useFlag("release"), optIn: useOptInFlags(), - setOptIn: useSetOptIn(), }), { wrapper: ({ children }) => ( @@ -748,14 +747,14 @@ describe("opt-in hooks", () => { expect(result.current.optIn.flags[0].userOptedIn).toBe(false); await act(async () => { expect( - (await result.current.setOptIn("release", { optedIn: true }))?.ok, + (await result.current.optIn.setOptIn("release", { optedIn: true }))?.ok, ).toBe(true); }); await waitFor(() => expect(result.current.optIn.flags[0].userOptedIn).toBe(true), ); expect(result.current.value).toBe(false); - expect(result.current.optIn.flags[0].isEnabled).toBe(false); + expect(result.current.optIn.flags[0].value).toBe(false); }); test("preserves opt-in Suspense and releases it after metadata failure", async () => { diff --git a/packages/vue-sdk/README.md b/packages/vue-sdk/README.md index e429d260f..d7f0178d2 100644 --- a/packages/vue-sdk/README.md +++ b/packages/vue-sdk/README.md @@ -93,8 +93,7 @@ To use a client you create yourself, pass it to `ReflagClientProvider`. Your app ## Opt-in, tracking and feedback ```ts -const { flags, isLoading } = useOptInFlags(); // refs to metadata/list loading -const setOptIn = useSetOptIn(); +const { flags, isLoading, setOptIn } = useOptInFlags(); // flags and isLoading are refs try { const response = await setOptIn("huddles", { optedIn: true }); if (!response?.ok) console.error("Could not update opt-in"); @@ -110,7 +109,7 @@ const sendFeedback = useSendFeedback(); await sendFeedback({ flagKey: "huddles", score: 5 }); ``` -Each entry includes the flag's `key`, `name`, `description` 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. +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. Set `enableTracking: false` to stop sending checks and events, or `offline` to skip all requests. See [Browser feedback documentation](../browser-sdk/FEEDBACK.md) for prompting/UI options. @@ -133,6 +132,8 @@ Registered flags get autocompletion for their keys and type checks for the defau - `useFlag()` returns the flag's value: replace `const { isEnabled } = useFlag(key)` with `const enabled = useFlag(key)`. Use `.value` in script. - Move remote config to a multivariate flag and read it with a default, such as `useFlag("model", "quick")`. Existing remote config isn't converted automatically. - Use `useTrack()` and `useRequestFeedback()` instead of the flag's `track()` and `requestFeedback()`. +- Use the `setOptIn` function returned by `useOptInFlags()`. `useSetOptIn()` still works but is deprecated. +- Opt-in entries have the flag's `value` instead of `isEnabled`. - Remove `fallbackFlags` and pass a default to each read instead, such as `useFlag("huddles", true)`. - If you bootstrap from the server, upgrade the Node SDK that produces the bootstrap data to v2 at the same time. - If you generate type declarations, regenerate them with the v2 CLI. diff --git a/packages/vue-sdk/src/hooks.ts b/packages/vue-sdk/src/hooks.ts index 8fcc75cff..c3d59a348 100644 --- a/packages/vue-sdk/src/hooks.ts +++ b/packages/vue-sdk/src/hooks.ts @@ -101,8 +101,8 @@ export function useFlag( } /** - * Vue composable for getting opt-in-enabled flags and their loading state for - * the current context. + * Vue composable for getting opt-in-enabled flags, their loading state, and a + * `setOptIn` function for the current context. * * The loading state is only used with `ReflagBootstrappedProvider` while * opt-in metadata is fetched on demand. Regular providers load opt-in metadata @@ -114,7 +114,9 @@ export function useFlag( export function useOptInFlags(): UseOptInFlagsResult { const client = useClient(); const isLoading = useIsLoading(); - const optInFlags = ref(client.getOptInFlags() as OptInFlag[]); + const optInFlags = shallowRef( + client.getOptInFlags() as OptInFlag[], + ); const isOptInFlagsLoading = ref(client.getIsLoadingOptInFlags()); const updateOptInFlags = () => { @@ -132,15 +134,21 @@ export function useOptInFlags(): UseOptInFlagsResult { updateIsLoading(); }); + const setOptIn: UseOptInFlagsResult["setOptIn"] = (key, options) => + client.setOptIn(String(key), options); + return { flags: computed(() => optInFlags.value), isLoading: computed(() => isLoading.value || isOptInFlagsLoading.value), + setOptIn, }; } /** * Vue composable for setting whether the current user or company has opted into a flag. * + * @deprecated Use the `setOptIn` function returned by {@link useOptInFlags}. + * * Check the returned Response's `ok` property and catch promise rejections. * HTTP failures return a non-OK Response; offline mode, invalid arguments, or * missing scoped context return undefined. Confirmation failures can reject diff --git a/packages/vue-sdk/src/types.ts b/packages/vue-sdk/src/types.ts index ade540ff1..67f2fa4b0 100644 --- a/packages/vue-sdk/src/types.ts +++ b/packages/vue-sdk/src/types.ts @@ -6,6 +6,7 @@ import type { InitOptions, Logger, OptInFlag as BrowserOptInFlag, + SetOptInOptions, FlagValue, FlagDefaultFor, FlagDefaultValue, @@ -36,7 +37,7 @@ export type FlagKey = keyof Flags extends never * An opt-in-enabled flag for the generated Vue SDK flag definitions. * * Includes all fields from {@link BrowserOptInFlag}: `name`, `description`, - * `isEnabled`, `userOptedIn`, `companyOptedIn`, and `isOptedIn`. + * `value`, `userOptedIn`, `companyOptedIn`, and `isOptedIn`. * Only `key` is narrowed to the generated {@link FlagKey} type. */ export type OptInFlag = Omit & { @@ -46,6 +47,18 @@ export type OptInFlag = Omit & { export type UseOptInFlagsResult = { flags: ComputedRef; isLoading: ComputedRef; + /** + * Sets whether the current user or company has opted into a flag. + * + * Check the returned Response's `ok` property and catch promise rejections. + * HTTP failures return a non-OK Response; offline mode, invalid arguments, or + * missing scoped context return undefined. Confirmation failures can reject + * after the membership changed remotely. + */ + setOptIn: ( + key: FlagKey, + options: SetOptInOptions, + ) => Promise; }; export interface ProviderContextType { diff --git a/packages/vue-sdk/test/opt-in-types.ts b/packages/vue-sdk/test/opt-in-types.ts index 690b95e02..bc7d3edee 100644 --- a/packages/vue-sdk/test/opt-in-types.ts +++ b/packages/vue-sdk/test/opt-in-types.ts @@ -1,4 +1,4 @@ -import { useFlag, useOptInFlags, useSetOptIn } from "@reflag/vue-sdk"; +import { useFlag, useOptInFlags } from "@reflag/vue-sdk"; declare module "@reflag/vue-sdk" { interface Flags { @@ -8,8 +8,7 @@ declare module "@reflag/vue-sdk" { } export function GeneratedOptInTypes() { - const { flags, isLoading } = useOptInFlags(); - const setOptIn = useSetOptIn(); + const { flags, isLoading, setOptIn } = useOptInFlags(); for (const flag of flags.value) { void setOptIn(flag.key, { optedIn: !flag.userOptedIn }); diff --git a/packages/vue-sdk/test/usage.test.ts b/packages/vue-sdk/test/usage.test.ts index 5e14dd063..f3c607a2d 100644 --- a/packages/vue-sdk/test/usage.test.ts +++ b/packages/vue-sdk/test/usage.test.ts @@ -181,7 +181,7 @@ describe("ReflagProvider", () => { key: "optInFlag", name: "Opt-in flag", description: "Try it early", - isEnabled: false, + value: false, userOptedIn: false, companyOptedIn: false, isOptedIn: false, @@ -274,10 +274,13 @@ describe("ReflagProvider", () => { expect(wrapper.findComponent(Child).vm.isLoading).toBe(false); }); - test("useSetOptIn delegates to the browser client", async () => { + test.each([ + ["useOptInFlags().setOptIn", () => useOptInFlags().setOptIn], + ["useSetOptIn", () => useSetOptIn()], + ])("%s delegates to the browser client", async (_name, getSetOptIn) => { const Child = defineComponent({ setup() { - const setOptIn = useSetOptIn(); + const setOptIn = getSetOptIn(); return { setOptIn }; }, template: "
", From db431c3b6bc3fdeee283e0eac301d0b98dfde8c5 Mon Sep 17 00:00:00 2001 From: Ron Cohen <744+roncohen@users.noreply.github.com> Date: Sun, 29 Nov 2026 18:37:53 +0000 Subject: [PATCH 2/3] docs: use setOptIn from useOptInFlags in the React Native README --- packages/react-native-sdk/README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/react-native-sdk/README.md b/packages/react-native-sdk/README.md index f9e610d57..42579e48b 100644 --- a/packages/react-native-sdk/README.md +++ b/packages/react-native-sdk/README.md @@ -178,8 +178,7 @@ Render this component inside `ReflagProvider`. ## Opt-in hooks ```tsx -const { flags, isLoading } = useOptInFlags(); -const setOptIn = useSetOptIn(); +const { flags, isLoading, setOptIn } = useOptInFlags(); try { const response = await setOptIn("huddles", { optedIn: true }); if (!response?.ok) console.error("Could not update opt-in"); @@ -188,7 +187,7 @@ try { } ``` -Each entry includes the flag's `key`, `name`, `description` 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. +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. Pass `{ suspense: true }` to `useOptInFlags()` to suspend until the list loads. It defaults to the provider's `suspense` prop. @@ -202,6 +201,9 @@ See the [Expo](https://github.com/reflagcom/javascript/tree/main/packages/react- - Move remote config to a multivariate flag and read it with a default, such as `useFlag("model", "quick")`. Existing remote config isn't converted automatically. - Use `useTrack()` instead of the flag's `track()`, and send feedback with `useSendFeedback()`. - Replace `useFlag(key, { suspense: true })` with `useFlagSuspense(key)`, and the flag's loading property with `useIsLoading()`. +- `useFeature()` is removed. Use `useFlag()` instead. +- Use the `setOptIn` function returned by `useOptInFlags()`. `useSetOptIn()` still works but is deprecated. +- Opt-in entries have the flag's `value` instead of `isEnabled`. - Remove `fallbackFlags` and pass a default to each read instead, such as `useFlag("huddles", true)`. - If you bootstrap from the server, upgrade the Node SDK that produces the bootstrap data to v2 at the same time. - If you generate type declarations, regenerate them with the v2 CLI. From 239ad5231b232678286d848f956719a49bdd9353 Mon Sep 17 00:00:00 2001 From: Ron Cohen <744+roncohen@users.noreply.github.com> Date: Sun, 29 Nov 2026 18:41:47 +0000 Subject: [PATCH 3/3] chore: split opt-in changeset so the Browser changelog doesn't mention hooks --- .changeset/browser-opt-in-value.md | 5 +++++ .changeset/opt-in-set-from-hook.md | 3 +-- 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 .changeset/browser-opt-in-value.md diff --git a/.changeset/browser-opt-in-value.md b/.changeset/browser-opt-in-value.md new file mode 100644 index 000000000..85aa495b5 --- /dev/null +++ b/.changeset/browser-opt-in-value.md @@ -0,0 +1,5 @@ +--- +"@reflag/browser-sdk": major +--- + +Opt-in entries from `getOptInFlags()` now include the flag's `value` instead of `isEnabled`. The deprecated `getFeatures()` alias is removed; use `getFlags()`. diff --git a/.changeset/opt-in-set-from-hook.md b/.changeset/opt-in-set-from-hook.md index 95b88ef5f..12818bb4f 100644 --- a/.changeset/opt-in-set-from-hook.md +++ b/.changeset/opt-in-set-from-hook.md @@ -1,8 +1,7 @@ --- -"@reflag/browser-sdk": major "@reflag/react-sdk": major "@reflag/vue-sdk": major "@reflag/react-native-sdk": major --- -`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()` and `getFeatures()` aliases are removed; use `useFlag()` and `getFlags()`. +`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()`.