@@ -89,7 +89,8 @@ rules: ({ featureFlags }) => [
8989],
9090```
9191
92- Evaluation operations and HTTP endpoints are public: they expose boolean results
92+ Evaluation operations and HTTP endpoints are public: they expose boolean results,
93+ assigned variant keys, and assigned payloads
9394and do not consult authorization rules. Bulk evaluation also exposes active flag
9495keys. Their permission facts distinguish ` scope: "single" ` with a ` key ` from
9596` scope: "all" ` , but those descriptors do not restrict public access. Browser
@@ -128,6 +129,52 @@ Disabled, archived, and missing flags evaluate to `false`. Keys remain reserved
128129when archived. Segment flags require at least one rule. Organizing tags on the
129130flag definition are separate from targeting tags supplied in evaluation context.
130131
132+ ## Weighted variants
133+
134+ Any flag type can define optional weighted alternatives. Existing boolean
135+ evaluations are unchanged: variants are assigned only after the flag evaluates to
136+ ` true ` . Use the dashboard's ** Variants** controls to add keys, weights, and optional
137+ JSON payloads, then ** Preview saved variants** to try a user or session and see
138+ the assignment in the evaluation log.
139+
140+ ``` ts
141+ await myStack .trusted .featureFlags .createFlag ({
142+ key: " checkout-layout" ,
143+ name: " Checkout layout" ,
144+ enabled: true ,
145+ variants: [
146+ { key: " control" , weight: 30 , payload: " classic" },
147+ { key: " compact" , weight: 70 , payload: { columns: 1 } },
148+ ],
149+ })
150+
151+ const { result, variant, payload } = await myStack .forRequest (request )
152+ .operations .featureFlags .evaluateVariant ({
153+ key: " checkout-layout" ,
154+ context: { userId: session .userId },
155+ })
156+ ```
157+
158+ Variant keys must be unique, non-empty strings of up to 100 characters.
159+ Weights are finite numbers from 0 to 100 and must sum to 100, including decimal
160+ weights. Zero-weight alternatives receive no assignments. An empty ` variants `
161+ array removes variants. Payloads may be strings, numbers, booleans, or JSON
162+ objects (including nested arrays and nulls); top-level arrays and null are not
163+ accepted. Payloads are public to evaluation clients; never store secrets in them.
164+
165+ Assignment uses the flag key and ` userId ` , falling back to ` sessionId ` , then
166+ ` anonymous ` , in a separate bucket from percentage rollout. The same definition
167+ and identity produce the same assignment on server, browser, and edge. Editing
168+ weights, keys, or ordering can reassign subjects. Disabled, archived, missing,
169+ untargeted flags and flags without variants return ` variant: null ` without a
170+ payload. ` result ` still reports boolean eligibility.
171+
172+ The pure ` evaluateFlagVariant(flag, context) ` export from
173+ ` @btst/plugin-feature-flags/evaluate ` returns the same assignment without logs or
174+ hooks. Database users must apply their adapter's additive schema migration for
175+ the new serialized ` flag.variants ` string field (default ` "[]" ` ). Existing records
176+ without variants are read as empty lists; existing rollout buckets are preserved.
177+
131178## Server-side evaluation
132179
133180Use request-scoped operations in Server Components and API routes. Evaluation is
@@ -183,14 +230,29 @@ flag read permission. The same hooks entry exports create, update, toggle, archi
183230and evaluation-log hooks. Management mutations invalidate cached definitions,
184231values, and logs in the current query client.
185232
233+ ` useFeatureFlagVariant ` returns a React Query result with the boolean result,
234+ assigned key, and optional payload. It uses the same identity and transport cache
235+ partitioning and visible-tab refresh interval as ` useFlag ` . Definition edits
236+ invalidate both hooks immediately in the current query client.
237+
238+ ``` tsx
239+ import { useFeatureFlagVariant } from " @btst/plugin-feature-flags/client/hooks"
240+
241+ const { data, isPending, error } = useFeatureFlagVariant (" checkout-layout" , { userId })
242+ if (isPending ) return <p >Loading layout…</p >
243+ if (error ) return <p >Layout could not be loaded.</p >
244+ return data ?.variant === " compact" ? <CompactCheckout /> : <ClassicCheckout />
245+ ```
246+
186247## HTTP evaluation
187248
188249| Method | Endpoint | Result |
189250| --- | --- | --- |
190251| GET | ` /api/data/feature-flags/evaluate/:key ` | Boolean |
191252| GET | ` /api/data/feature-flags/evaluate ` | Object mapping active flag keys to booleans |
253+ | GET | ` /api/data/feature-flags/variant/:key ` | ` { result: boolean, variant: string \| null, payload?: ... } ` |
192254
193- Both endpoints accept an optional ` context ` query parameter containing base64
255+ All evaluation endpoints accept an optional ` context ` query parameter containing base64
194256encoded UTF-8 JSON, such as ` { "userId": "user-123", "tags": ["beta"] } ` . Invalid
195257context is rejected. Definitions and evaluation logs use separate protected
196258management endpoints under ` /api/data/feature-flags/flags ` .
@@ -210,6 +272,16 @@ have separate histories. Use `hooks.onEvaluate` to send exposures to your analyt
210272audit system when persistent storage is required. Hooks run after authorization;
211273` onBeforeToggle ` can throw to prevent a toggle.
212274
275+ Every evaluation path (including boolean and bulk evaluation) records the assigned
276+ ` variant ` key or ` null ` in ` FlagEvaluation ` . The existing ` onEvaluate ` callback
277+ keeps its argument order; its fourth argument now includes ` variant ` :
278+
279+ ``` ts
280+ onEvaluate : (flag , result , context , { request , variant }) => {
281+ analytics .capture ({ flag: flag .key , result , variant , userId: context .userId })
282+ }
283+ ```
284+
213285## SSG and ISR
214286
215287Administration is dynamic. A static consumer can evaluate trusted flag values at
0 commit comments