@vllnt/convex-flags 0.1.0 → 0.1.1-canary.4efa5b8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +113 -0
- package/README.md +512 -114
- package/dist/client/index.d.ts +2 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +2 -0
- package/dist/client/index.js.map +1 -1
- package/dist/component/convex.config.d.ts +1 -0
- package/dist/component/convex.config.d.ts.map +1 -1
- package/dist/component/convex.config.js +1 -0
- package/dist/component/convex.config.js.map +1 -1
- package/dist/component/mutations.d.ts.map +1 -1
- package/dist/component/mutations.js +77 -23
- package/dist/component/mutations.js.map +1 -1
- package/dist/shared.d.ts +10 -0
- package/dist/shared.d.ts.map +1 -1
- package/dist/shared.js +17 -1
- package/dist/shared.js.map +1 -1
- package/docs/API.md +593 -0
- package/llms.txt +74 -0
- package/package.json +53 -28
- package/src/client/index.ts +2 -0
- package/src/component/convex.config.ts +1 -0
- package/src/component/mutations.ts +107 -28
- package/src/shared.ts +19 -1
- package/src/react/index.test.ts +0 -52
- package/src/status-backward-compat.test.ts +0 -51
package/docs/API.md
ADDED
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
# API Reference — @vllnt/convex-flags
|
|
2
|
+
|
|
3
|
+
**Compatibility:** `convex@^1.45.0`; `react@^18.0.0 || ^19.0.0` optional (only
|
|
4
|
+
for `./react`). This reference is bundled in the package and describes the
|
|
5
|
+
installed version.
|
|
6
|
+
|
|
7
|
+
## Entry points
|
|
8
|
+
|
|
9
|
+
| Import | Exports |
|
|
10
|
+
| ----------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
11
|
+
| `@vllnt/convex-flags` | `Flags`; types `FlagsComponent`, `EvalContext`, `EvalOptions`, `FlagDefinition`, `FlagDoc`, `FlagEvaluation`, `FlagUpdate`, `Rollout`, `Rule`, `Variant`, `VariantValue` |
|
|
12
|
+
| `@vllnt/convex-flags/convex.config` (or `.js`) | default component config for `app.use(...)` |
|
|
13
|
+
| `@vllnt/convex-flags/react` | `useFlag`, `useFlags`; types `UseFlagOptions`, `EvaluateRef`, `AllRef`, `EvalContext`, `FlagEvaluation`, `VariantValue` |
|
|
14
|
+
| `@vllnt/convex-flags/test` | `register(t, name = "flags")` for `convex-test` (TypeScript source; needs Vite `import.meta.glob`) |
|
|
15
|
+
| `@vllnt/convex-flags/_generated/component` (or `.js`) | type `ComponentApi` |
|
|
16
|
+
|
|
17
|
+
`./package.json` is exported as-is. A flag value is a typed primitive,
|
|
18
|
+
`boolean | string | number` ([`VariantValue`](#variantvalue)): a boolean flag is
|
|
19
|
+
the two-variant case, string and number flags are multivariate. Flag keys and
|
|
20
|
+
subject refs are opaque host strings. On writes the component rejects only empty
|
|
21
|
+
ones; it does not check a key's characters or length.
|
|
22
|
+
|
|
23
|
+
Keys that [`all`](#all) cannot return: `all` returns a record keyed by flag key,
|
|
24
|
+
and Convex rejects record keys that start with `$` or `_`, contain a character
|
|
25
|
+
outside printable ASCII, or are longer than 1024 characters. `define` stores
|
|
26
|
+
such a key and `get` / `evaluate` serve it, but `all` then fails for every flag
|
|
27
|
+
while it exists, for example with
|
|
28
|
+
`Field name café has invalid character 'é': Field names can only contain non-control ASCII characters`
|
|
29
|
+
or `Field name $beta starts with a '$', which is reserved.` The `$`, non-ASCII,
|
|
30
|
+
control-character and length cases were reproduced in `convex-test`; the `_`
|
|
31
|
+
rule is Convex's documented record-key rule (`v.record`: "Keys must be ASCII
|
|
32
|
+
characters only, non-empty, and not start with `$` or `_`") and was not
|
|
33
|
+
reproduced there. Use printable ASCII keys of at most 1024 characters that do
|
|
34
|
+
not start with `$` or `_`.
|
|
35
|
+
|
|
36
|
+
## Client
|
|
37
|
+
|
|
38
|
+
### `Flags`
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
new Flags(component: FlagsComponent)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
`component` is the mounted component, `components.flags` (or the name passed to
|
|
45
|
+
`app.use`). There are no constructor options. Every method takes the host `ctx`
|
|
46
|
+
first. The context types are structural: the mutation methods (`define`,
|
|
47
|
+
`update`, `enable`, `disable`, `archive`, `restore`, `remove`, `setOverride`,
|
|
48
|
+
`clearOverride`) need a context with `runMutation` (a mutation or action
|
|
49
|
+
context); the query methods (`get`, `list`, `evaluate`, `variant`, `isEnabled`,
|
|
50
|
+
`all`) need a context with `runQuery` (a query, mutation or action context).
|
|
51
|
+
From an action, each call runs in its own transaction.
|
|
52
|
+
|
|
53
|
+
The host owns auth. The component's functions are internal; gate the mutation
|
|
54
|
+
methods behind your own authorized mutations.
|
|
55
|
+
|
|
56
|
+
### `define`
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
define(ctx, definition: FlagDefinition): Promise<string>
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Creates a flag, or **fully replaces** the definition of an existing key (value,
|
|
63
|
+
description, variants, rules, rollout), and returns the flag id. Fields left out
|
|
64
|
+
of `definition` are cleared. The status (active or archived) is kept on replace;
|
|
65
|
+
a new flag starts active. A definition identical to the stored one writes
|
|
66
|
+
nothing, so `updatedAt` is kept.
|
|
67
|
+
|
|
68
|
+
- `definition.key` must be non-empty (`INVALID_ARGUMENT`).
|
|
69
|
+
- Every split weight, flag-level or in a rule, must be finite and `>= 0`
|
|
70
|
+
(`INVALID_ARGUMENT`).
|
|
71
|
+
|
|
72
|
+
### `update`
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
update(ctx, key: string, patch: FlagUpdate): Promise<null>
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
**Partially** updates a flag: only the fields present in `patch` change (the
|
|
79
|
+
opposite of `define`). A field that is omitted or `undefined` is left as is, so
|
|
80
|
+
`update` cannot clear a field; pass an empty value (`rules: []`) or use
|
|
81
|
+
`define`. Throws `FLAG_NOT_FOUND` for an unknown key, and `INVALID_ARGUMENT` for
|
|
82
|
+
an empty key or an invalid weight. A patch that changes nothing writes nothing.
|
|
83
|
+
|
|
84
|
+
### `enable`
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
enable(ctx, key: string): Promise<null>
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Sets the flag's value to `true`. It does this on any flag, including a string or
|
|
91
|
+
number flag; use `define` or `update` for multivariate values. Throws
|
|
92
|
+
`FLAG_NOT_FOUND`.
|
|
93
|
+
|
|
94
|
+
### `disable`
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
disable(ctx, key: string): Promise<null>
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Sets the flag's value to `false`, on any flag. Throws `FLAG_NOT_FOUND`.
|
|
101
|
+
|
|
102
|
+
### `archive`
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
archive(ctx, key: string): Promise<null>
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Reversibly retires a flag. Evaluation then skips rules and rollout and serves
|
|
109
|
+
the base value with reason `disabled`; per-subject overrides still apply. The
|
|
110
|
+
flag stays in `list` and `all`. Throws `FLAG_NOT_FOUND`.
|
|
111
|
+
|
|
112
|
+
### `restore`
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
restore(ctx, key: string): Promise<null>
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Returns an archived flag to active. Throws `FLAG_NOT_FOUND`.
|
|
119
|
+
|
|
120
|
+
### `remove`
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
remove(ctx, key: string): Promise<null>
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Permanently deletes a flag and all of its per-subject overrides. Throws
|
|
127
|
+
`FLAG_NOT_FOUND`.
|
|
128
|
+
|
|
129
|
+
### `setOverride`
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
setOverride(ctx, key: string, subjectRef: string, value: VariantValue): Promise<null>
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Forces the flag's value for one subject. The flag must exist (`FLAG_NOT_FOUND`);
|
|
136
|
+
`key` and `subjectRef` must be non-empty (`INVALID_ARGUMENT`). Setting it again
|
|
137
|
+
replaces the value. The value is not checked against the flag's `variants`.
|
|
138
|
+
|
|
139
|
+
### `clearOverride`
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
clearOverride(ctx, key: string, subjectRef: string): Promise<null>
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Removes a subject's override. Does nothing when there is none, including for an
|
|
146
|
+
unknown flag. `key` and `subjectRef` must be non-empty (`INVALID_ARGUMENT`).
|
|
147
|
+
|
|
148
|
+
### `get`
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
get(ctx, key: string): Promise<FlagDoc | null>
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
One flag definition, or `null` for an unknown key.
|
|
155
|
+
|
|
156
|
+
### `list`
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
list(ctx): Promise<FlagDoc[]>
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Every flag definition, active and archived, read in one query with no
|
|
163
|
+
pagination.
|
|
164
|
+
|
|
165
|
+
### `evaluate`
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
evaluate(ctx, key: string, options?: EvalOptions): Promise<FlagEvaluation>
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Resolves a flag against `options.context` and returns `{ value, reason }`. See
|
|
172
|
+
[Evaluation order](#evaluation-order). An unknown key does not throw: it returns
|
|
173
|
+
`options.default` with reason `default`, or `false` with reason `unknown`.
|
|
174
|
+
|
|
175
|
+
### `variant`
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
variant(ctx, key: string, options?: EvalOptions): Promise<VariantValue>
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Like `evaluate`, returning only the value.
|
|
182
|
+
|
|
183
|
+
### `isEnabled`
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
isEnabled(ctx, key: string, context?: EvalContext): Promise<boolean>
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
`true` when the evaluated value is exactly the boolean `true`; `false`
|
|
190
|
+
otherwise, including for unknown keys. It takes the context directly, not an
|
|
191
|
+
options object, and has no `default`.
|
|
192
|
+
|
|
193
|
+
### `all`
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
all(ctx, context?: EvalContext): Promise<Record<string, FlagEvaluation>>
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
Evaluates every defined flag against the same context, keyed by flag key: the
|
|
200
|
+
bootstrap payload for a reactive client. Overrides for `context.subjectRef` are
|
|
201
|
+
applied. Unknown keys are simply absent. It throws while any stored key is not a
|
|
202
|
+
valid Convex record key (see [Entry points](#entry-points) for the key rules).
|
|
203
|
+
|
|
204
|
+
`all` returns one field per flag, and Convex limits an object to 1024 fields
|
|
205
|
+
(including return values), so with more than 1024 flags `all` and `useFlags`
|
|
206
|
+
fail. This is Convex's documented limit and is not tested here. Use `evaluate`
|
|
207
|
+
per key for larger sets. `all` reads every flag in one query, with no
|
|
208
|
+
pagination.
|
|
209
|
+
|
|
210
|
+
## Evaluation order
|
|
211
|
+
|
|
212
|
+
`evaluate`, `variant`, `isEnabled` and `all` resolve a flag in this order; the
|
|
213
|
+
first that applies wins:
|
|
214
|
+
|
|
215
|
+
1. **Override** — a per-subject override for `context.subjectRef` (reason
|
|
216
|
+
`override`).
|
|
217
|
+
2. **Archived** — an archived flag serves its base value (reason `disabled`);
|
|
218
|
+
rules and rollout are skipped. A flag with no `status` (a row written before
|
|
219
|
+
`status` existed) is treated as active.
|
|
220
|
+
3. **Rules** — rules in order; the first whose conditions all match serves its
|
|
221
|
+
`value` (reason `rule`), or else its `rollout` (reason `rollout`).
|
|
222
|
+
4. **Flag-level rollout** — when no rule matched (reason `rollout`).
|
|
223
|
+
5. **Flag value** — the base value (reason `flag`).
|
|
224
|
+
|
|
225
|
+
A rollout that cannot bucket (no `subjectRef` / `by` attribute in the context,
|
|
226
|
+
or no positive-weight split) yields no value, so the flag value is served with
|
|
227
|
+
reason `flag`. When that happens inside a matched rule, or the matched rule has
|
|
228
|
+
neither `value` nor `rollout`, evaluation stops there: later rules and the
|
|
229
|
+
flag-level rollout are not tried.
|
|
230
|
+
|
|
231
|
+
## Targeting operators
|
|
232
|
+
|
|
233
|
+
A condition is `{ attribute, op, values }`. A missing attribute never matches,
|
|
234
|
+
including `neq` and `nin`. Equality is strict (no string/number coercion);
|
|
235
|
+
`contains` is case-sensitive. There are no regex or prefix/suffix operators.
|
|
236
|
+
|
|
237
|
+
| Op | Holds when |
|
|
238
|
+
| --------------------------- | --------------------------------------------------------- |
|
|
239
|
+
| `eq` / `neq` | attribute equals / does not equal `values[0]` |
|
|
240
|
+
| `in` / `nin` | attribute is / is not in `values` |
|
|
241
|
+
| `contains` | string attribute contains the string `values[0]` |
|
|
242
|
+
| `gt` / `gte` / `lt` / `lte` | numeric attribute compares against the number `values[0]` |
|
|
243
|
+
|
|
244
|
+
## Rollouts
|
|
245
|
+
|
|
246
|
+
A rollout distributes across weighted splits, bucketed deterministically by a
|
|
247
|
+
stable subject so a given subject always resolves to the same value:
|
|
248
|
+
|
|
249
|
+
```ts
|
|
250
|
+
const rollout = {
|
|
251
|
+
splits: [
|
|
252
|
+
{ value: "A", weight: 50 },
|
|
253
|
+
{ value: "B", weight: 50 },
|
|
254
|
+
],
|
|
255
|
+
by: "userId",
|
|
256
|
+
};
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
`by` names the context attribute to bucket by; when omitted,
|
|
260
|
+
`context.subjectRef` is used. The subject is hashed with 32-bit FNV-1a as
|
|
261
|
+
`"<flag key>:<subject>"`. Weights are relative to the sum of the positive
|
|
262
|
+
weights, and may be whole numbers (for example percentages) or decimals (`0.5` /
|
|
263
|
+
`0.5`); a `0` weight is never served. When every weight is a whole number, a
|
|
264
|
+
subject's bucket is its hash modulo that sum; when any weight is not a whole
|
|
265
|
+
number and the sum is finite, the hash modulo 1,000,000 is scaled into
|
|
266
|
+
`[0, sum)` instead. Splits are filled in order, so reordering them moves
|
|
267
|
+
subjects.
|
|
268
|
+
|
|
269
|
+
Upgrading from 0.1.0: rollouts whose weights are all whole numbers, and rows
|
|
270
|
+
stored with an `Infinity` weight before weights were validated, bucket exactly
|
|
271
|
+
as before. Other rollouts with a non-whole-number weight are re-bucketed once,
|
|
272
|
+
so some of their subjects change value. In 0.1.0 some of these weights bucketed
|
|
273
|
+
unevenly: weights summing to 1 (such as `0.5` / `0.5`) sent every subject to the
|
|
274
|
+
first split, other sets were skewed (`1.5` / `1.5` served about 67% / 33%), and
|
|
275
|
+
a split could get no subjects at all (`0.25` / `0.75` / `1` never served its
|
|
276
|
+
middle split).
|
|
277
|
+
|
|
278
|
+
## Types
|
|
279
|
+
|
|
280
|
+
`AttributeValue`, `EvalReason`, `Condition` and `Split` appear below for
|
|
281
|
+
reference and are not exported by name: derive them with
|
|
282
|
+
`FlagEvaluation["reason"]`, `Rule["conditions"][number]` and
|
|
283
|
+
`Rollout["splits"][number]`; `AttributeValue` is the same union as
|
|
284
|
+
`VariantValue`.
|
|
285
|
+
|
|
286
|
+
### `FlagsComponent`
|
|
287
|
+
|
|
288
|
+
```ts
|
|
289
|
+
interface FlagsComponent {
|
|
290
|
+
mutations: {
|
|
291
|
+
define;
|
|
292
|
+
update;
|
|
293
|
+
enable;
|
|
294
|
+
disable;
|
|
295
|
+
remove;
|
|
296
|
+
archive;
|
|
297
|
+
restore;
|
|
298
|
+
setOverride;
|
|
299
|
+
clearOverride;
|
|
300
|
+
};
|
|
301
|
+
queries: { get; list; evaluate; all };
|
|
302
|
+
}
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
The internal function references `Flags` calls. Pass `components.flags`; you do
|
|
306
|
+
not build this yourself.
|
|
307
|
+
|
|
308
|
+
### `FlagDefinition`
|
|
309
|
+
|
|
310
|
+
```ts
|
|
311
|
+
interface FlagDefinition {
|
|
312
|
+
key: string;
|
|
313
|
+
value: VariantValue;
|
|
314
|
+
description?: string;
|
|
315
|
+
variants?: Variant[];
|
|
316
|
+
rules?: Rule[];
|
|
317
|
+
rollout?: Rollout;
|
|
318
|
+
}
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
Argument of [`define`](#define).
|
|
322
|
+
|
|
323
|
+
### `FlagUpdate`
|
|
324
|
+
|
|
325
|
+
```ts
|
|
326
|
+
interface FlagUpdate {
|
|
327
|
+
value?: VariantValue;
|
|
328
|
+
description?: string;
|
|
329
|
+
variants?: Variant[];
|
|
330
|
+
rules?: Rule[];
|
|
331
|
+
rollout?: Rollout;
|
|
332
|
+
}
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
Argument of [`update`](#update); the key is passed separately.
|
|
336
|
+
|
|
337
|
+
### `FlagDoc`
|
|
338
|
+
|
|
339
|
+
```ts
|
|
340
|
+
interface FlagDoc {
|
|
341
|
+
_id: string;
|
|
342
|
+
_creationTime: number;
|
|
343
|
+
key: string;
|
|
344
|
+
description?: string;
|
|
345
|
+
value: VariantValue;
|
|
346
|
+
variants?: Variant[];
|
|
347
|
+
rules?: Rule[];
|
|
348
|
+
rollout?: Rollout;
|
|
349
|
+
status: "active" | "archived";
|
|
350
|
+
createdAt: number;
|
|
351
|
+
updatedAt: number;
|
|
352
|
+
}
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
Returned by [`get`](#get) and [`list`](#list). A legacy row stored without a
|
|
356
|
+
status reads as `"active"`.
|
|
357
|
+
|
|
358
|
+
### `FlagEvaluation`
|
|
359
|
+
|
|
360
|
+
```ts
|
|
361
|
+
type EvalReason =
|
|
362
|
+
"flag" | "rule" | "rollout" | "override" | "disabled" | "default" | "unknown";
|
|
363
|
+
|
|
364
|
+
interface FlagEvaluation {
|
|
365
|
+
value: VariantValue;
|
|
366
|
+
reason: EvalReason;
|
|
367
|
+
}
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
| Reason | Served |
|
|
371
|
+
| ---------- | --------------------------------------------------- |
|
|
372
|
+
| `flag` | the flag's base value (no rule or rollout applied) |
|
|
373
|
+
| `rule` | a matched rule's `value` |
|
|
374
|
+
| `rollout` | a split chosen by a rule's or the flag's rollout |
|
|
375
|
+
| `override` | the subject's override |
|
|
376
|
+
| `disabled` | the base value of an archived flag |
|
|
377
|
+
| `default` | the caller's `default` for an unknown key |
|
|
378
|
+
| `unknown` | `false` for an unknown key with no caller `default` |
|
|
379
|
+
|
|
380
|
+
Also exported from `@vllnt/convex-flags/react`.
|
|
381
|
+
|
|
382
|
+
### `EvalContext`
|
|
383
|
+
|
|
384
|
+
```ts
|
|
385
|
+
interface EvalContext {
|
|
386
|
+
subjectRef?: string; // used for overrides and default rollout bucketing
|
|
387
|
+
attributes?: Record<string, boolean | string | number>; // used by rule conditions and `by`
|
|
388
|
+
}
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
Supplied by the host on every call. Also exported from
|
|
392
|
+
`@vllnt/convex-flags/react`.
|
|
393
|
+
|
|
394
|
+
### `EvalOptions`
|
|
395
|
+
|
|
396
|
+
```ts
|
|
397
|
+
interface EvalOptions {
|
|
398
|
+
context?: EvalContext;
|
|
399
|
+
default?: VariantValue; // served (reason `default`) when the key is unknown
|
|
400
|
+
}
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
Options for [`evaluate`](#evaluate) and [`variant`](#variant). Only `context`
|
|
404
|
+
and `default` exist.
|
|
405
|
+
|
|
406
|
+
### `VariantValue`
|
|
407
|
+
|
|
408
|
+
```ts
|
|
409
|
+
type VariantValue = boolean | string | number;
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
Also exported from `@vllnt/convex-flags/react`.
|
|
413
|
+
|
|
414
|
+
### `Variant`
|
|
415
|
+
|
|
416
|
+
```ts
|
|
417
|
+
interface Variant {
|
|
418
|
+
value: VariantValue;
|
|
419
|
+
label?: string;
|
|
420
|
+
}
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
A declared variant. Metadata only: evaluation does not check served values
|
|
424
|
+
against `variants`.
|
|
425
|
+
|
|
426
|
+
### `Rule`
|
|
427
|
+
|
|
428
|
+
```ts
|
|
429
|
+
interface Condition {
|
|
430
|
+
attribute: string;
|
|
431
|
+
op: "eq" | "neq" | "in" | "nin" | "contains" | "gt" | "gte" | "lt" | "lte";
|
|
432
|
+
values: Array<boolean | string | number>;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
interface Rule {
|
|
436
|
+
conditions: Condition[]; // AND; an empty list matches everyone
|
|
437
|
+
value?: VariantValue; // wins over `rollout` when both are set
|
|
438
|
+
rollout?: Rollout;
|
|
439
|
+
}
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
See [Targeting operators](#targeting-operators).
|
|
443
|
+
|
|
444
|
+
### `Rollout`
|
|
445
|
+
|
|
446
|
+
```ts
|
|
447
|
+
interface Split {
|
|
448
|
+
value: VariantValue;
|
|
449
|
+
weight: number; // finite and >= 0
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
interface Rollout {
|
|
453
|
+
splits: Split[];
|
|
454
|
+
by?: string; // context attribute to bucket by; default `subjectRef`
|
|
455
|
+
}
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
See [Rollouts](#rollouts).
|
|
459
|
+
|
|
460
|
+
## Error codes
|
|
461
|
+
|
|
462
|
+
| Code | Thrown by | Condition | Message |
|
|
463
|
+
| ------------------ | ----------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------- |
|
|
464
|
+
| `FLAG_NOT_FOUND` | `update`, `enable`, `disable`, `archive`, `restore`, `remove`, `setOverride` | The flag key does not exist | contains `FLAG_NOT_FOUND` |
|
|
465
|
+
| `INVALID_ARGUMENT` | every mutation (`key`); `setOverride`, `clearOverride` (`subjectRef`); `define`, `update` | `key` or `subjectRef` is empty, or a rollout weight (flag-level or in a rule) is `NaN`, infinite or negative | `key must be non-empty`, `subjectRef must be non-empty`, `rollout weights must be finite and non-negative` |
|
|
466
|
+
|
|
467
|
+
`FLAG_NOT_FOUND` is a plain JavaScript `Error("FLAG_NOT_FOUND")`, not a
|
|
468
|
+
structured `ConvexError` payload. `INVALID_ARGUMENT` is a `ConvexError` whose
|
|
469
|
+
`data` is `{ code: "INVALID_ARGUMENT", message }`. Convex hides plain error
|
|
470
|
+
messages from clients in production; do not depend on client-visible error text.
|
|
471
|
+
|
|
472
|
+
## React
|
|
473
|
+
|
|
474
|
+
Optional entry `@vllnt/convex-flags/react`. The hooks wrap `convex/react`'s
|
|
475
|
+
`useQuery` and take your own public query references; the component never
|
|
476
|
+
imports your `api`. A backend-only consumer never imports this entry.
|
|
477
|
+
|
|
478
|
+
### `useFlag`
|
|
479
|
+
|
|
480
|
+
```ts
|
|
481
|
+
import type {
|
|
482
|
+
EvaluateRef,
|
|
483
|
+
FlagEvaluation,
|
|
484
|
+
UseFlagOptions,
|
|
485
|
+
} from "@vllnt/convex-flags/react";
|
|
486
|
+
|
|
487
|
+
useFlag(
|
|
488
|
+
query: EvaluateRef,
|
|
489
|
+
key: string,
|
|
490
|
+
options?: UseFlagOptions,
|
|
491
|
+
): FlagEvaluation | undefined
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
Returns `undefined` while loading, then `{ value, reason }`; re-renders when the
|
|
495
|
+
result changes.
|
|
496
|
+
|
|
497
|
+
### `useFlags`
|
|
498
|
+
|
|
499
|
+
```ts
|
|
500
|
+
import type {
|
|
501
|
+
AllRef,
|
|
502
|
+
EvalContext,
|
|
503
|
+
FlagEvaluation,
|
|
504
|
+
} from "@vllnt/convex-flags/react";
|
|
505
|
+
|
|
506
|
+
useFlags(
|
|
507
|
+
query: AllRef,
|
|
508
|
+
context?: EvalContext,
|
|
509
|
+
): Record<string, FlagEvaluation> | undefined
|
|
510
|
+
```
|
|
511
|
+
|
|
512
|
+
Returns `undefined` while loading, then a map of flag key to
|
|
513
|
+
`{ value, reason }`.
|
|
514
|
+
|
|
515
|
+
### `UseFlagOptions`
|
|
516
|
+
|
|
517
|
+
```ts
|
|
518
|
+
interface UseFlagOptions {
|
|
519
|
+
context?: EvalContext;
|
|
520
|
+
default?: VariantValue; // served (reason `default`) when the key is unknown
|
|
521
|
+
}
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
### `EvaluateRef`
|
|
525
|
+
|
|
526
|
+
```ts
|
|
527
|
+
type EvaluateRef = FunctionReference<
|
|
528
|
+
"query",
|
|
529
|
+
"public",
|
|
530
|
+
{ key: string; context?: EvalContext; default?: VariantValue },
|
|
531
|
+
FlagEvaluation
|
|
532
|
+
>;
|
|
533
|
+
```
|
|
534
|
+
|
|
535
|
+
A public query with the args and result of [`evaluate`](#evaluate), typically a
|
|
536
|
+
wrapper that calls it.
|
|
537
|
+
|
|
538
|
+
### `AllRef`
|
|
539
|
+
|
|
540
|
+
```ts
|
|
541
|
+
type AllRef = FunctionReference<
|
|
542
|
+
"query",
|
|
543
|
+
"public",
|
|
544
|
+
{ context?: EvalContext },
|
|
545
|
+
Record<string, FlagEvaluation>
|
|
546
|
+
>;
|
|
547
|
+
```
|
|
548
|
+
|
|
549
|
+
A public query with the args and result of [`all`](#all), typically a wrapper
|
|
550
|
+
that calls it.
|
|
551
|
+
|
|
552
|
+
## Component config
|
|
553
|
+
|
|
554
|
+
### `convex.config`
|
|
555
|
+
|
|
556
|
+
```ts
|
|
557
|
+
import flags from "@vllnt/convex-flags/convex.config";
|
|
558
|
+
```
|
|
559
|
+
|
|
560
|
+
The default export is the component definition (`defineComponent("flags")`).
|
|
561
|
+
Mount it with `app.use(flags)`, or `app.use(flags, { name })` once per named
|
|
562
|
+
mount. Each mount has its own `flags` and `overrides` tables. The component has
|
|
563
|
+
no crons, actions or child components.
|
|
564
|
+
|
|
565
|
+
### `ComponentApi`
|
|
566
|
+
|
|
567
|
+
```ts
|
|
568
|
+
import type { ComponentApi } from "@vllnt/convex-flags/_generated/component";
|
|
569
|
+
```
|
|
570
|
+
|
|
571
|
+
Generated type of the mounted component's internal functions (`mutations.*`,
|
|
572
|
+
`queries.*`). `components.flags` in the host has this type, and it is assignable
|
|
573
|
+
to [`FlagsComponent`](#flagscomponent).
|
|
574
|
+
|
|
575
|
+
## Testing
|
|
576
|
+
|
|
577
|
+
### `register`
|
|
578
|
+
|
|
579
|
+
```ts
|
|
580
|
+
import type { TestConvex } from "convex-test";
|
|
581
|
+
import type { GenericSchema, SchemaDefinition } from "convex/server";
|
|
582
|
+
|
|
583
|
+
register(
|
|
584
|
+
t: TestConvex<SchemaDefinition<GenericSchema, boolean>>,
|
|
585
|
+
name = "flags",
|
|
586
|
+
): void
|
|
587
|
+
```
|
|
588
|
+
|
|
589
|
+
Registers the component with a `convex-test` instance under `name` (pass the
|
|
590
|
+
mount name if you used `app.use(flags, { name })`). `t` can be built from a host
|
|
591
|
+
schema with its own tables. The helper ships as TypeScript source and uses
|
|
592
|
+
Vite's `import.meta.glob`, so it needs Vitest (or another Vite-based runner)
|
|
593
|
+
plus `convex-test`.
|
package/llms.txt
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# @vllnt/convex-flags
|
|
2
|
+
|
|
3
|
+
> Convex component for feature flags in TypeScript: kill switches, variants, attribute targeting, percentage rollouts, per-subject overrides and React hooks.
|
|
4
|
+
|
|
5
|
+
Install `@vllnt/convex-flags` (unreleased builds from `main` use the `canary`
|
|
6
|
+
dist-tag). Peer dependency `convex@^1.45.0`; `react@^18.0.0 || ^19.0.0` is
|
|
7
|
+
optional and only needed for `@vllnt/convex-flags/react`. Mount it with
|
|
8
|
+
`app.use(flags)` from `@vllnt/convex-flags/convex.config` and call it through
|
|
9
|
+
`new Flags(components.flags)` from host queries, mutations and actions.
|
|
10
|
+
Management methods: `define` (full replace), `update` (partial), `enable`,
|
|
11
|
+
`disable`, `archive`, `restore`, `remove`, `setOverride`, `clearOverride`.
|
|
12
|
+
Read methods: `get`, `list`, `evaluate`, `variant`, `isEnabled`, `all`. A flag
|
|
13
|
+
is keyed by an opaque host string and its value is `boolean | string | number`.
|
|
14
|
+
Evaluation order: per-subject override, archived (serves the base value with
|
|
15
|
+
reason `disabled`), targeting rules in order, flag-level rollout, base value.
|
|
16
|
+
Rollouts bucket by the 32-bit FNV-1a hash of `<flag key>:<subject>`, where the
|
|
17
|
+
subject is `subjectRef` or the `by` attribute. An unknown key returns the
|
|
18
|
+
caller `default` (reason `default`) or `false` (reason `unknown`) and never
|
|
19
|
+
throws. `update`, `enable`, `disable`, `archive`, `restore`, `remove` and
|
|
20
|
+
`setOverride` throw a plain `Error` whose message contains `FLAG_NOT_FOUND` for
|
|
21
|
+
a key that does not exist (`define` creates it, `clearOverride` does nothing);
|
|
22
|
+
empty keys or subject refs and `NaN`, infinite or negative weights throw
|
|
23
|
+
`ConvexError` with `data.code` `INVALID_ARGUMENT`. `all` returns a record keyed
|
|
24
|
+
by flag key, so it fails while any key starts with `$` or `_`, has a character
|
|
25
|
+
outside printable ASCII or exceeds 1024 characters, and (Convex's documented
|
|
26
|
+
1024-fields-per-object limit) when there are more than 1024 flags; use printable
|
|
27
|
+
ASCII keys of at most 1024 characters not starting with `$` or `_`, and
|
|
28
|
+
`evaluate` per key for larger sets. React hooks `useFlag` and
|
|
29
|
+
`useFlags` take the host's own public queries. The host owns auth and the
|
|
30
|
+
meaning of keys, subjects and attributes.
|
|
31
|
+
|
|
32
|
+
## When to use
|
|
33
|
+
|
|
34
|
+
- Use it when a Convex app needs on/off switches it can flip without a deploy.
|
|
35
|
+
- Use it when a feature goes to a percentage of users, or to users whose
|
|
36
|
+
attributes match rules, and each user must keep the same value.
|
|
37
|
+
- Use it when clients must see a flag change live through a Convex query.
|
|
38
|
+
- Do not use it when you run an A/B experiment and need sticky assignments and
|
|
39
|
+
exposure counts per variant: use `@vllnt/convex-experiments`.
|
|
40
|
+
- Do not use it when you need to measure what a flagged feature did (events,
|
|
41
|
+
funnels, retention): use `@vllnt/convex-analytics`.
|
|
42
|
+
|
|
43
|
+
## For coding agents
|
|
44
|
+
|
|
45
|
+
The docs are bundled in the package at `node_modules/@vllnt/convex-flags/docs/`
|
|
46
|
+
and match the installed version. Read them, and the shipped `src/`, before
|
|
47
|
+
writing code. The links below to `./` paths are files inside the installed package.
|
|
48
|
+
|
|
49
|
+
## Docs
|
|
50
|
+
|
|
51
|
+
- [README](./README.md): Install, quick start, common tasks, limits
|
|
52
|
+
- [API Reference](./docs/API.md): Every export, signature, return shape and validation rule
|
|
53
|
+
- [Errors and fixes](./README.md#errors-and-fixes): Every error code, its cause and the fix
|
|
54
|
+
- [Changelog](./CHANGELOG.md): Version history and release notes
|
|
55
|
+
- [License](./LICENSE): MIT license
|
|
56
|
+
|
|
57
|
+
## Optional
|
|
58
|
+
|
|
59
|
+
Repository files, not bundled in the package; they track `main`.
|
|
60
|
+
|
|
61
|
+
- [Example component wrapper](https://github.com/vllnt/convex-flags/blob/main/example/convex/example.ts): Host-app wrapper functions
|
|
62
|
+
- [Example tests](https://github.com/vllnt/convex-flags/blob/main/example/convex/example.test.ts): Simulated usage coverage with `convex-test`
|
|
63
|
+
- [Contributing](https://github.com/vllnt/convex-flags/blob/main/CONTRIBUTING.md): Development setup, validation commands, release workflow
|
|
64
|
+
- [Security Policy](https://github.com/vllnt/convex-flags/blob/main/SECURITY.md): Vulnerability reporting policy
|
|
65
|
+
- [Code of Conduct](https://github.com/vllnt/convex-flags/blob/main/CODE_OF_CONDUCT.md): Community participation guidelines
|
|
66
|
+
|
|
67
|
+
## Ecosystem
|
|
68
|
+
|
|
69
|
+
- [Package page](https://oss.vllnt.com/packages/convex-flags)
|
|
70
|
+
- [Release feed](https://oss.vllnt.com/packages/convex-flags/rss.xml)
|
|
71
|
+
- [All @vllnt packages](https://oss.vllnt.com/llms.txt)
|
|
72
|
+
- [Search vllnt packages and docs](https://index.vllnt.com)
|
|
73
|
+
- [vllnt](https://vllnt.com/llms.txt)
|
|
74
|
+
- [Author](https://bntvllnt.com)
|