@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/README.md
CHANGED
|
@@ -1,182 +1,580 @@
|
|
|
1
|
-
<!-- Badges -->
|
|
2
|
-
[](https://www.convex.dev/components)
|
|
3
|
-
[](https://www.npmjs.com/package/@vllnt/convex-flags)
|
|
4
|
-
[](https://github.com/vllnt/convex-flags/actions/workflows/ci.yml)
|
|
5
|
-
[](./LICENSE)
|
|
6
|
-
|
|
7
1
|
# @vllnt/convex-flags
|
|
8
2
|
|
|
9
|
-
|
|
3
|
+
Convex component for feature flags in TypeScript: kill switches, variants,
|
|
4
|
+
attribute targeting, percentage rollouts, per-subject overrides and React hooks.
|
|
10
5
|
|
|
11
|
-
|
|
12
|
-
const flags = new Flags(components.flags);
|
|
13
|
-
await flags.define(ctx, { key: "new-checkout", value: false });
|
|
14
|
-
await flags.enable(ctx, "new-checkout");
|
|
15
|
-
const on = await flags.isEnabled(ctx, "new-checkout", { subjectRef: userId }); // reactive in a query
|
|
16
|
-
```
|
|
6
|
+
## For coding agents
|
|
17
7
|
|
|
18
|
-
|
|
8
|
+
Docs for the installed version are in `node_modules/@vllnt/convex-flags/`:
|
|
9
|
+
`docs/API.md`, `llms.txt`, `CHANGELOG.md` and the TypeScript `src/`. Read them
|
|
10
|
+
before writing code against this package.
|
|
19
11
|
|
|
20
|
-
|
|
21
|
-
- **Attribute targeting** — ordered rules (`eq`, `in`, `contains`, `gt`, …) over a host-supplied typed context.
|
|
22
|
-
- **Per-subject overrides** — force any flag's value for one subject.
|
|
23
|
-
- **Lifecycle** — archive (reversible) distinct from permanent delete.
|
|
24
|
-
- **Real-time** — evaluated in Convex queries, so changes stream to clients over the same reactive subscriptions as your app data.
|
|
25
|
-
- **Data isolation** — definitions and overrides live in the component's own sandboxed tables; no blast radius into your schema.
|
|
26
|
-
- **Reactive React hooks** — optional `./react` entry (`useFlag`/`useFlags`); backend-only consumers pull in zero React.
|
|
27
|
-
- **Auth-agnostic & dual-target** — the host gates every management call; runs unchanged on Convex Cloud and self-hosted `convex-backend`.
|
|
12
|
+
## When to use
|
|
28
13
|
|
|
29
|
-
|
|
14
|
+
- Use it when a Convex app needs on/off switches it can flip without a deploy.
|
|
15
|
+
- Use it when a feature goes to a percentage of users, or to users whose
|
|
16
|
+
attributes match rules, and each user must keep the same value.
|
|
17
|
+
- Use it when clients must see a flag change live through a Convex query.
|
|
18
|
+
- Do not use it when you run an A/B experiment and need sticky assignments and
|
|
19
|
+
exposure counts per variant: use
|
|
20
|
+
[`@vllnt/convex-experiments`](https://oss.vllnt.com/packages/convex-experiments).
|
|
21
|
+
- Do not use it when you need to measure what a flagged feature did (events,
|
|
22
|
+
funnels, retention): use
|
|
23
|
+
[`@vllnt/convex-analytics`](https://oss.vllnt.com/packages/convex-analytics).
|
|
24
|
+
|
|
25
|
+
## Install
|
|
30
26
|
|
|
31
27
|
```bash
|
|
32
|
-
npm install @vllnt/convex-flags
|
|
28
|
+
npm install @vllnt/convex-flags convex
|
|
33
29
|
```
|
|
34
30
|
|
|
35
|
-
|
|
31
|
+
## Quick start
|
|
36
32
|
|
|
37
33
|
```ts
|
|
38
|
-
|
|
34
|
+
// convex/convex.config.ts
|
|
39
35
|
import flags from "@vllnt/convex-flags/convex.config";
|
|
36
|
+
import { defineApp } from "convex/server";
|
|
40
37
|
|
|
41
38
|
const app = defineApp();
|
|
42
39
|
app.use(flags);
|
|
43
40
|
export default app;
|
|
44
41
|
```
|
|
45
42
|
|
|
46
|
-
Peer dependency: `convex@^1.36.1`. `react` is an optional peer dependency, needed only for the `./react` entry.
|
|
47
|
-
|
|
48
|
-
## Usage
|
|
49
|
-
|
|
50
43
|
```ts
|
|
51
|
-
// convex/flags.ts
|
|
52
|
-
// rest of your app (and the React hooks) call. The host owns auth: gate the
|
|
53
|
-
// management mutations here before delegating to the component.
|
|
54
|
-
import { v } from "convex/values";
|
|
44
|
+
// convex/flags.ts
|
|
55
45
|
import { Flags } from "@vllnt/convex-flags";
|
|
46
|
+
import { v } from "convex/values";
|
|
47
|
+
|
|
56
48
|
import { components } from "./_generated/api";
|
|
57
|
-
import {
|
|
49
|
+
import { internalMutation, query } from "./_generated/server";
|
|
58
50
|
|
|
59
51
|
export const flags = new Flags(components.flags);
|
|
60
52
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
handler: (ctx,
|
|
65
|
-
flags.evaluate(ctx, args.key, { context: args.context, default: args.default }),
|
|
53
|
+
export const createNewCheckout = internalMutation({
|
|
54
|
+
args: {},
|
|
55
|
+
returns: v.string(),
|
|
56
|
+
handler: (ctx) => flags.define(ctx, { key: "new-checkout", value: false }),
|
|
66
57
|
});
|
|
67
58
|
|
|
68
|
-
export const
|
|
69
|
-
args: {
|
|
70
|
-
|
|
59
|
+
export const turnOnNewCheckout = internalMutation({
|
|
60
|
+
args: {},
|
|
61
|
+
returns: v.null(),
|
|
62
|
+
handler: (ctx) => flags.enable(ctx, "new-checkout"),
|
|
71
63
|
});
|
|
72
64
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
handler: (ctx
|
|
77
|
-
|
|
78
|
-
return flags.
|
|
65
|
+
export const newCheckoutOn = query({
|
|
66
|
+
args: {},
|
|
67
|
+
returns: v.boolean(),
|
|
68
|
+
handler: async (ctx) => {
|
|
69
|
+
const user = await ctx.auth.getUserIdentity();
|
|
70
|
+
return flags.isEnabled(ctx, "new-checkout", {
|
|
71
|
+
subjectRef: user?.tokenIdentifier,
|
|
72
|
+
});
|
|
79
73
|
},
|
|
80
74
|
});
|
|
81
|
-
|
|
82
|
-
// Wrap the rest the same way — `update`, `enable`, `disable`, `archive`,
|
|
83
|
-
// `restore`, `setOverride`, `clearOverride` — each gated then delegated.
|
|
84
75
|
```
|
|
85
76
|
|
|
77
|
+
Run `npx convex dev` once to generate `components.flags`, then
|
|
78
|
+
`npx convex run flags:createNewCheckout` and
|
|
79
|
+
`npx convex run flags:turnOnNewCheckout`. `newCheckoutOn` re-runs for every
|
|
80
|
+
subscribed client when the flag changes. The component never reads auth: the
|
|
81
|
+
management functions above are internal, and the host decides who may call them.
|
|
82
|
+
|
|
83
|
+
## API
|
|
84
|
+
|
|
85
|
+
Full reference: [docs/API.md](docs/API.md).
|
|
86
|
+
|
|
87
|
+
| Export | Import from | What it does |
|
|
88
|
+
| --------------------------------------------------------------------- | ------------------------------------------ | ------------------------------------------------------------------- |
|
|
89
|
+
| [`Flags`](docs/API.md#flags) | `@vllnt/convex-flags` | Host client: `new Flags(components.flags)` |
|
|
90
|
+
| [`define(ctx, definition)`](docs/API.md#define) | `Flags` method, mutation | Create a flag, or fully replace its definition; returns the flag id |
|
|
91
|
+
| [`update(ctx, key, patch)`](docs/API.md#update) | `Flags` method, mutation | Change only the supplied fields of a flag |
|
|
92
|
+
| [`enable(ctx, key)`](docs/API.md#enable) | `Flags` method, mutation | Set the flag's value to `true` |
|
|
93
|
+
| [`disable(ctx, key)`](docs/API.md#disable) | `Flags` method, mutation | Set the flag's value to `false` |
|
|
94
|
+
| [`archive(ctx, key)`](docs/API.md#archive) | `Flags` method, mutation | Retire a flag: serve its base value, skip rules and rollout |
|
|
95
|
+
| [`restore(ctx, key)`](docs/API.md#restore) | `Flags` method, mutation | Make an archived flag active again |
|
|
96
|
+
| [`remove(ctx, key)`](docs/API.md#remove) | `Flags` method, mutation | Delete a flag and its overrides |
|
|
97
|
+
| [`setOverride(ctx, key, subjectRef, value)`](docs/API.md#setoverride) | `Flags` method, mutation | Force one subject's value for a flag |
|
|
98
|
+
| [`clearOverride(ctx, key, subjectRef)`](docs/API.md#clearoverride) | `Flags` method, mutation | Remove one subject's override |
|
|
99
|
+
| [`get(ctx, key)`](docs/API.md#get) | `Flags` method, query | One flag definition, or `null` |
|
|
100
|
+
| [`list(ctx)`](docs/API.md#list) | `Flags` method, query | Every flag definition |
|
|
101
|
+
| [`evaluate(ctx, key, options?)`](docs/API.md#evaluate) | `Flags` method, query | `{ value, reason }` for one flag and context |
|
|
102
|
+
| [`variant(ctx, key, options?)`](docs/API.md#variant) | `Flags` method, query | The evaluated value only |
|
|
103
|
+
| [`isEnabled(ctx, key, context?)`](docs/API.md#isenabled) | `Flags` method, query | `true` only when the evaluated value is exactly `true` |
|
|
104
|
+
| [`all(ctx, context?)`](docs/API.md#all) | `Flags` method, query | `{ value, reason }` for every flag, keyed by flag key |
|
|
105
|
+
| [`FlagsComponent`](docs/API.md#flagscomponent) | `@vllnt/convex-flags` (type) | Type of `components.flags` the client accepts |
|
|
106
|
+
| [`FlagDefinition`](docs/API.md#flagdefinition) | `@vllnt/convex-flags` (type) | Argument of `define` |
|
|
107
|
+
| [`FlagUpdate`](docs/API.md#flagupdate) | `@vllnt/convex-flags` (type) | Argument of `update` |
|
|
108
|
+
| [`FlagDoc`](docs/API.md#flagdoc) | `@vllnt/convex-flags` (type) | A stored flag as returned by `get` and `list` |
|
|
109
|
+
| [`FlagEvaluation`](docs/API.md#flagevaluation) | `@vllnt/convex-flags`, `/react` (type) | `{ value, reason }` |
|
|
110
|
+
| [`EvalContext`](docs/API.md#evalcontext) | `@vllnt/convex-flags`, `/react` (type) | `{ subjectRef?, attributes? }` used for targeting and bucketing |
|
|
111
|
+
| [`EvalOptions`](docs/API.md#evaloptions) | `@vllnt/convex-flags` (type) | `{ context?, default? }` for `evaluate` and `variant` |
|
|
112
|
+
| [`VariantValue`](docs/API.md#variantvalue) | `@vllnt/convex-flags`, `/react` (type) | `boolean \| string \| number` |
|
|
113
|
+
| [`Variant`](docs/API.md#variant-1) | `@vllnt/convex-flags` (type) | Declared variant: `{ value, label? }` (metadata only) |
|
|
114
|
+
| [`Rule`](docs/API.md#rule) | `@vllnt/convex-flags` (type) | Targeting rule: conditions, then a `value` or a `rollout` |
|
|
115
|
+
| [`Rollout`](docs/API.md#rollout) | `@vllnt/convex-flags` (type) | Weighted splits, bucketed by `subjectRef` or a `by` attribute |
|
|
116
|
+
| [`useFlag`](docs/API.md#useflag) | `@vllnt/convex-flags/react` | Reactive `{ value, reason }` for one flag over your own query |
|
|
117
|
+
| [`useFlags`](docs/API.md#useflags) | `@vllnt/convex-flags/react` | Reactive map of every flag over your own query |
|
|
118
|
+
| [`UseFlagOptions`](docs/API.md#useflagoptions) | `@vllnt/convex-flags/react` (type) | `{ context?, default? }` for `useFlag` |
|
|
119
|
+
| [`EvaluateRef`](docs/API.md#evaluateref) | `@vllnt/convex-flags/react` (type) | Query reference shape `useFlag` accepts |
|
|
120
|
+
| [`AllRef`](docs/API.md#allref) | `@vllnt/convex-flags/react` (type) | Query reference shape `useFlags` accepts |
|
|
121
|
+
| [`default` (component config)](docs/API.md#convexconfig) | `@vllnt/convex-flags/convex.config` | Pass to `app.use(...)` |
|
|
122
|
+
| [`register`](docs/API.md#register) | `@vllnt/convex-flags/test` | Mount the component in `convex-test` |
|
|
123
|
+
| [`ComponentApi`](docs/API.md#componentapi) | `@vllnt/convex-flags/_generated/component` | Generated type of the mounted component |
|
|
124
|
+
|
|
125
|
+
## Common tasks
|
|
126
|
+
|
|
127
|
+
Read flags from React. The hooks take your own public queries. Here the server
|
|
128
|
+
sets `subjectRef` from auth and ignores any `subjectRef` the client sends;
|
|
129
|
+
`attributes` still come from the client, so do not target on them for access
|
|
130
|
+
decisions:
|
|
131
|
+
|
|
86
132
|
```ts
|
|
87
|
-
//
|
|
88
|
-
|
|
133
|
+
// convex/flagQueries.ts
|
|
134
|
+
import { v } from "convex/values";
|
|
135
|
+
|
|
89
136
|
import { query } from "./_generated/server";
|
|
90
137
|
import { flags } from "./flags";
|
|
91
138
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
139
|
+
const value = v.union(v.boolean(), v.string(), v.number());
|
|
140
|
+
const context = v.object({
|
|
141
|
+
subjectRef: v.optional(v.string()),
|
|
142
|
+
attributes: v.optional(v.record(v.string(), value)),
|
|
143
|
+
});
|
|
144
|
+
const evaluation = v.object({
|
|
145
|
+
value,
|
|
146
|
+
reason: v.union(
|
|
147
|
+
v.literal("flag"),
|
|
148
|
+
v.literal("rule"),
|
|
149
|
+
v.literal("rollout"),
|
|
150
|
+
v.literal("override"),
|
|
151
|
+
v.literal("disabled"),
|
|
152
|
+
v.literal("default"),
|
|
153
|
+
v.literal("unknown"),
|
|
154
|
+
),
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
export const evaluate = query({
|
|
158
|
+
args: {
|
|
159
|
+
key: v.string(),
|
|
160
|
+
context: v.optional(context),
|
|
161
|
+
default: v.optional(value),
|
|
162
|
+
},
|
|
163
|
+
returns: evaluation,
|
|
164
|
+
handler: async (ctx, args) => {
|
|
165
|
+
const user = await ctx.auth.getUserIdentity();
|
|
166
|
+
return flags.evaluate(ctx, args.key, {
|
|
167
|
+
context: {
|
|
168
|
+
subjectRef: user?.tokenIdentifier,
|
|
169
|
+
attributes: args.context?.attributes,
|
|
170
|
+
},
|
|
171
|
+
default: args.default,
|
|
172
|
+
});
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
export const all = query({
|
|
177
|
+
args: { context: v.optional(context) },
|
|
178
|
+
returns: v.record(v.string(), evaluation),
|
|
179
|
+
handler: async (ctx, args) => {
|
|
180
|
+
const user = await ctx.auth.getUserIdentity();
|
|
181
|
+
return flags.all(ctx, {
|
|
182
|
+
subjectRef: user?.tokenIdentifier,
|
|
183
|
+
attributes: args.context?.attributes,
|
|
184
|
+
});
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
```tsx
|
|
190
|
+
// src/Checkout.tsx
|
|
191
|
+
import { useFlag, useFlags } from "@vllnt/convex-flags/react";
|
|
192
|
+
|
|
193
|
+
import { api } from "../convex/_generated/api";
|
|
194
|
+
|
|
195
|
+
export function Checkout({ plan }: { plan: string }) {
|
|
196
|
+
const checkout = useFlag(api.flagQueries.evaluate, "new-checkout", {
|
|
197
|
+
context: { attributes: { plan } },
|
|
99
198
|
});
|
|
100
|
-
|
|
199
|
+
const everything = useFlags(api.flagQueries.all);
|
|
200
|
+
if (checkout === undefined || everything === undefined) return <p>Loading</p>;
|
|
201
|
+
const label = checkout.value === true ? "New checkout" : "Classic checkout";
|
|
202
|
+
const count = Object.keys(everything).length;
|
|
203
|
+
return (
|
|
204
|
+
<p>{`${label} (reason: ${checkout.reason}, ${String(count)} flags)`}</p>
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Target by attributes and roll out by percentage. Pro users always get the new
|
|
210
|
+
checkout; everyone else is split 10% / 90% by `subjectRef`:
|
|
211
|
+
|
|
212
|
+
```ts
|
|
213
|
+
// convex/rollout.ts
|
|
214
|
+
import { v } from "convex/values";
|
|
215
|
+
|
|
216
|
+
import { internalMutation, query } from "./_generated/server";
|
|
217
|
+
import { flags } from "./flags";
|
|
101
218
|
|
|
102
|
-
export const
|
|
219
|
+
export const setUpRollout = internalMutation({
|
|
103
220
|
args: {},
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
:
|
|
221
|
+
returns: v.string(),
|
|
222
|
+
handler: (ctx) =>
|
|
223
|
+
flags.define(ctx, {
|
|
224
|
+
key: "new-checkout",
|
|
225
|
+
value: false,
|
|
226
|
+
rules: [
|
|
227
|
+
{
|
|
228
|
+
conditions: [{ attribute: "plan", op: "eq", values: ["pro"] }],
|
|
229
|
+
value: true,
|
|
230
|
+
},
|
|
231
|
+
],
|
|
232
|
+
rollout: {
|
|
233
|
+
splits: [
|
|
234
|
+
{ value: true, weight: 10 },
|
|
235
|
+
{ value: false, weight: 90 },
|
|
236
|
+
],
|
|
237
|
+
},
|
|
238
|
+
}),
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
export const checkoutFor = query({
|
|
242
|
+
args: { plan: v.string() },
|
|
243
|
+
returns: v.boolean(),
|
|
244
|
+
handler: async (ctx, { plan }) => {
|
|
245
|
+
const user = await ctx.auth.getUserIdentity();
|
|
246
|
+
return flags.isEnabled(ctx, "new-checkout", {
|
|
247
|
+
subjectRef: user?.tokenIdentifier,
|
|
248
|
+
attributes: { plan },
|
|
249
|
+
});
|
|
250
|
+
},
|
|
108
251
|
});
|
|
109
252
|
```
|
|
110
253
|
|
|
111
|
-
|
|
254
|
+
Serve a string variant, bucketed by an attribute so every user in one team gets
|
|
255
|
+
the same value, and change one field later without touching the rest:
|
|
112
256
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
| `evaluate(ctx, key, options?)` | query | `FlagEvaluation` (`value` + `reason`) |
|
|
117
|
-
| `variant(ctx, key, options?)` | query | the resolved value |
|
|
118
|
-
| `get(ctx, key)` / `list(ctx)` | query | `FlagDoc \| null` / `FlagDoc[]` |
|
|
119
|
-
| `all(ctx, context?)` | query | `Record<string, FlagEvaluation>` (reactive bootstrap) |
|
|
120
|
-
| `define(ctx, definition)` | mutation | Create or **fully replace** a flag (value, variants, rules, rollout) |
|
|
121
|
-
| `update(ctx, key, patch)` | mutation | **Partially** update a flag — only supplied fields change, the rest are kept |
|
|
122
|
-
| `enable(ctx, key)` / `disable(ctx, key)` | mutation | Toggle a boolean flag (sets value `true`/`false`) |
|
|
123
|
-
| `archive(ctx, key)` / `restore(ctx, key)` | mutation | Reversibly retire / re-activate a flag |
|
|
124
|
-
| `setOverride(ctx, key, subjectRef, value)` / `clearOverride(ctx, key, subjectRef)` | mutation | Force / clear a per-subject value |
|
|
125
|
-
| `remove(ctx, key)` | mutation | Hard-delete a flag and its overrides |
|
|
257
|
+
```ts
|
|
258
|
+
// convex/layout.ts
|
|
259
|
+
import { v } from "convex/values";
|
|
126
260
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
convenience (they set the value to `true`/`false`) — use `define`/`update` for multivariate flags.
|
|
130
|
-
Full reference, types, operators, and the React hooks: [docs/API.md](docs/API.md).
|
|
261
|
+
import { internalMutation, query } from "./_generated/server";
|
|
262
|
+
import { flags } from "./flags";
|
|
131
263
|
|
|
132
|
-
|
|
264
|
+
export const setUpLayout = internalMutation({
|
|
265
|
+
args: {},
|
|
266
|
+
returns: v.string(),
|
|
267
|
+
handler: (ctx) =>
|
|
268
|
+
flags.define(ctx, {
|
|
269
|
+
key: "checkout-layout",
|
|
270
|
+
value: "one-page",
|
|
271
|
+
variants: [
|
|
272
|
+
{ value: "one-page", label: "One page" },
|
|
273
|
+
{ value: "steps", label: "Three steps" },
|
|
274
|
+
],
|
|
275
|
+
rollout: {
|
|
276
|
+
by: "teamId",
|
|
277
|
+
splits: [
|
|
278
|
+
{ value: "one-page", weight: 50 },
|
|
279
|
+
{ value: "steps", weight: 50 },
|
|
280
|
+
],
|
|
281
|
+
},
|
|
282
|
+
}),
|
|
283
|
+
});
|
|
133
284
|
|
|
134
|
-
|
|
135
|
-
|
|
285
|
+
export const describeLayout = internalMutation({
|
|
286
|
+
args: { description: v.string() },
|
|
287
|
+
returns: v.null(),
|
|
288
|
+
handler: (ctx, { description }) =>
|
|
289
|
+
flags.update(ctx, "checkout-layout", { description }),
|
|
290
|
+
});
|
|
136
291
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
292
|
+
export const layoutFor = query({
|
|
293
|
+
args: { teamId: v.string() },
|
|
294
|
+
returns: v.string(),
|
|
295
|
+
handler: async (ctx, { teamId }) => {
|
|
296
|
+
const layout = await flags.variant(ctx, "checkout-layout", {
|
|
297
|
+
context: { attributes: { teamId } },
|
|
298
|
+
default: "one-page",
|
|
299
|
+
});
|
|
300
|
+
return typeof layout === "string" ? layout : "one-page";
|
|
301
|
+
},
|
|
302
|
+
});
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
Force a value for one subject (support, QA), switch a flag off, retire it, bring
|
|
306
|
+
it back, delete it, and list the defined keys:
|
|
140
307
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
308
|
+
```ts
|
|
309
|
+
// convex/admin.ts
|
|
310
|
+
import { v } from "convex/values";
|
|
311
|
+
|
|
312
|
+
import { internalMutation, internalQuery } from "./_generated/server";
|
|
313
|
+
import { flags } from "./flags";
|
|
314
|
+
|
|
315
|
+
export const forceOn = internalMutation({
|
|
316
|
+
args: { subjectRef: v.string() },
|
|
317
|
+
returns: v.null(),
|
|
318
|
+
handler: (ctx, { subjectRef }) =>
|
|
319
|
+
flags.setOverride(ctx, "new-checkout", subjectRef, true),
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
export const unforce = internalMutation({
|
|
323
|
+
args: { subjectRef: v.string() },
|
|
324
|
+
returns: v.null(),
|
|
325
|
+
handler: (ctx, { subjectRef }) =>
|
|
326
|
+
flags.clearOverride(ctx, "new-checkout", subjectRef),
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
export const killSwitch = internalMutation({
|
|
330
|
+
args: {},
|
|
331
|
+
returns: v.null(),
|
|
332
|
+
handler: (ctx) => flags.disable(ctx, "new-checkout"),
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
export const retire = internalMutation({
|
|
336
|
+
args: {},
|
|
337
|
+
returns: v.null(),
|
|
338
|
+
handler: (ctx) => flags.archive(ctx, "new-checkout"),
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
export const bringBack = internalMutation({
|
|
342
|
+
args: {},
|
|
343
|
+
returns: v.null(),
|
|
344
|
+
handler: (ctx) => flags.restore(ctx, "new-checkout"),
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
export const deleteForGood = internalMutation({
|
|
348
|
+
args: {},
|
|
349
|
+
returns: v.null(),
|
|
350
|
+
handler: (ctx) => flags.remove(ctx, "new-checkout"),
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
export const flagKeys = internalQuery({
|
|
354
|
+
args: {},
|
|
355
|
+
returns: v.array(v.string()),
|
|
356
|
+
handler: async (ctx) => (await flags.list(ctx)).map((flag) => flag.key),
|
|
357
|
+
});
|
|
144
358
|
```
|
|
145
359
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
| `useFlag(query, key, options?)` | `FlagEvaluation \| undefined` |
|
|
149
|
-
| `useFlags(query, context?)` | `Record<string, FlagEvaluation> \| undefined` |
|
|
360
|
+
Mount the component twice for two separate sets of flags. Each named mount has
|
|
361
|
+
its own tables:
|
|
150
362
|
|
|
151
|
-
|
|
363
|
+
```ts
|
|
364
|
+
// convex/convex.config.ts
|
|
365
|
+
import flags from "@vllnt/convex-flags/convex.config";
|
|
366
|
+
import { defineApp } from "convex/server";
|
|
367
|
+
|
|
368
|
+
const app = defineApp();
|
|
369
|
+
app.use(flags, { name: "webFlags" });
|
|
370
|
+
app.use(flags, { name: "mobileFlags" });
|
|
371
|
+
export default app;
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
```ts
|
|
375
|
+
// convex/clients.ts
|
|
376
|
+
import { Flags } from "@vllnt/convex-flags";
|
|
152
377
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
378
|
+
import { components } from "./_generated/api";
|
|
379
|
+
|
|
380
|
+
export const webFlags = new Flags(components.webFlags);
|
|
381
|
+
export const mobileFlags = new Flags(components.mobileFlags);
|
|
382
|
+
```
|
|
156
383
|
|
|
157
|
-
|
|
384
|
+
## Errors and fixes
|
|
385
|
+
|
|
386
|
+
Management methods throw these errors from the component. Reads (`get`, `list`,
|
|
387
|
+
`evaluate`, `variant`, `isEnabled`, `all`) never throw them.
|
|
388
|
+
|
|
389
|
+
| Code | Message | Cause | Fix |
|
|
390
|
+
| ------------------ | ---------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------- |
|
|
391
|
+
| `FLAG_NOT_FOUND` | contains `FLAG_NOT_FOUND` (a plain `Error`) | `update`, `enable`, `disable`, `archive`, `restore`, `remove` or `setOverride` got a key that was never defined, or was removed | Call `define` first, or check with `get(ctx, key)`; keys are case-sensitive |
|
|
392
|
+
| `INVALID_ARGUMENT` | `key must be non-empty`, `subjectRef must be non-empty`, `rollout weights must be finite and non-negative` | A management method got an empty `key`; `setOverride` / `clearOverride` got an empty `subjectRef`; `define` / `update` got a split weight that is `NaN`, infinite or negative (flag-level or in a rule) | Pass non-empty strings; use weights `>= 0` (a `0` weight turns a split off) |
|
|
393
|
+
|
|
394
|
+
`INVALID_ARGUMENT` is a `ConvexError` whose `data` is `{ code, message }`: read
|
|
395
|
+
`error.data.code`. `FLAG_NOT_FOUND` is a plain `Error` whose message contains
|
|
396
|
+
`FLAG_NOT_FOUND`. Convex hides the message of a plain `Error` from clients in
|
|
397
|
+
production, so catch it in your host mutation and rethrow a `ConvexError` if the
|
|
398
|
+
client must see it. Arguments of the wrong type (for example an object as
|
|
399
|
+
`value`) are rejected by Convex argument validation before the component runs.
|
|
400
|
+
|
|
401
|
+
## Limits and gotchas
|
|
402
|
+
|
|
403
|
+
- Unknown keys do not throw on read. `evaluate` returns your `default` (reason
|
|
404
|
+
`default`) or `false` (reason `unknown`); `isEnabled` returns `false`; `all`
|
|
405
|
+
leaves the key out. A misspelled key silently reads as off.
|
|
406
|
+
- `isEnabled` is `true` only for the boolean `true`; a string `"true"` or the
|
|
407
|
+
number `1` reads as `false`.
|
|
408
|
+
- Evaluation order: per-subject override, then archived, then rules in order
|
|
409
|
+
(first match wins), then the flag-level rollout, then the flag's `value`.
|
|
410
|
+
- Archived means disabled: an archived flag serves its base `value` with reason
|
|
411
|
+
`disabled` and skips rules and rollout. Overrides still win over archiving.
|
|
412
|
+
Archived flags stay in `list` and `all`; `restore` makes them active again.
|
|
413
|
+
- Rollout bucketing is deterministic: the subject is `context.subjectRef`, or
|
|
414
|
+
`String(context.attributes[by])` when the rollout sets `by`. The bucket is the
|
|
415
|
+
32-bit FNV-1a hash of `"<flag key>:<subject>"`, so one subject gets the same
|
|
416
|
+
value on every read while the rollout is unchanged, and can land in different
|
|
417
|
+
splits of different flags. Renaming the key, reordering splits or changing
|
|
418
|
+
weights can move subjects.
|
|
419
|
+
- Weights are relative to the sum of the positive weights; a `0` weight is never
|
|
420
|
+
served. With whole-number weights the bucket is `hash % sum`; when any weight
|
|
421
|
+
is not a whole number, `hash % 1,000,000` is scaled into `[0, sum)`.
|
|
422
|
+
- Upgrading from 0.1.0: rollouts with a non-whole-number weight are re-bucketed
|
|
423
|
+
once, so some of their subjects change value. In 0.1.0 weights such as `0.5` /
|
|
424
|
+
`0.5` sent every subject to the first split. Rollouts whose weights are all
|
|
425
|
+
whole numbers, and rows stored with an `Infinity` weight before weights were
|
|
426
|
+
validated, bucket as before.
|
|
427
|
+
- A rollout with no subject (no `subjectRef`, or the `by` attribute missing) or
|
|
428
|
+
no positive weight serves the flag's `value` with reason `flag`. Inside a
|
|
429
|
+
matched rule this also stops evaluation: later rules and the flag-level
|
|
430
|
+
rollout are not tried. A matched rule with neither `value` nor `rollout` does
|
|
431
|
+
the same. A rule's `value` wins over its `rollout`.
|
|
432
|
+
- Conditions: a missing attribute never matches, not even `neq` or `nin`.
|
|
433
|
+
Equality is strict (`"1"` is not `1`). `contains` is case-sensitive and
|
|
434
|
+
strings only; `gt`, `gte`, `lt`, `lte` compare numbers only. `eq`, `neq`,
|
|
435
|
+
`contains` and the comparisons read only `values[0]`. A rule with no
|
|
436
|
+
conditions matches everyone.
|
|
437
|
+
- Overrides apply only when the context has a `subjectRef`. Override values and
|
|
438
|
+
rule values are not checked against `variants`, which are labels only.
|
|
439
|
+
- `define` replaces the whole definition: fields you leave out are cleared, and
|
|
440
|
+
the status (active or archived) is kept. `update` cannot clear a field,
|
|
441
|
+
because an omitted or `undefined` field is ignored; use `define`, or pass an
|
|
442
|
+
empty value such as `rules: []`.
|
|
443
|
+
- `enable` and `disable` write `true` / `false` even on a string or number flag.
|
|
444
|
+
- Writes that change nothing are skipped, so `updatedAt` stays and subscribed
|
|
445
|
+
queries do not re-run.
|
|
446
|
+
- `clearOverride` on a missing override or flag does nothing. `remove` deletes
|
|
447
|
+
the flag's overrides too.
|
|
448
|
+
- `all` (and so `useFlags`) returns a record keyed by flag key, and Convex
|
|
449
|
+
rejects record keys that start with `$` or `_`, contain a character outside
|
|
450
|
+
printable ASCII (such as `é` or a tab), or are longer than 1024 characters.
|
|
451
|
+
`define` accepts such a key, and `get` and `evaluate` serve it, but every
|
|
452
|
+
`all` call then fails while that flag exists, for all flags. Example:
|
|
453
|
+
`Field name café has invalid character 'é': Field names can only contain non-control ASCII characters`.
|
|
454
|
+
The `$`, non-ASCII, control-character and length cases were reproduced in
|
|
455
|
+
`convex-test`; the `_` rule is Convex's documented record-key rule and was not
|
|
456
|
+
reproduced there. Use printable ASCII keys of at most 1024 characters that do
|
|
457
|
+
not start with `$` or `_`.
|
|
458
|
+
- `all` returns one field per flag, and Convex limits an object to 1024 fields,
|
|
459
|
+
so with more than 1024 flags `all` and `useFlags` fail (Convex's documented
|
|
460
|
+
limit, not tested here). Use `evaluate` per key for larger sets. `list` and
|
|
461
|
+
`all` also read every flag in one query, with no pagination.
|
|
462
|
+
- Evaluation is a query and writes nothing: there is no evaluation log, no
|
|
463
|
+
stale-flag detection and no change history.
|
|
464
|
+
|
|
465
|
+
## Compatibility
|
|
466
|
+
|
|
467
|
+
- Peer dependencies: `convex@^1.45.0`; `react@^18.0.0 || ^19.0.0` (optional,
|
|
468
|
+
only for `@vllnt/convex-flags/react`). CI runs the unit tests on the locked
|
|
469
|
+
convex version (1.51.0), and type-checks and runs the packed package against
|
|
470
|
+
convex 1.45.0 and React 18.
|
|
471
|
+
- Node.js 20 or newer; convex 1.49 and newer needs Node.js 22 or newer. ES
|
|
472
|
+
modules only.
|
|
473
|
+
- Runtime: the component's functions are Convex queries and mutations in the
|
|
474
|
+
default Convex runtime, with no actions, crons, HTTP routes or network calls.
|
|
475
|
+
The client methods run in host queries, mutations and actions; the mutation
|
|
476
|
+
methods need `runMutation` (a mutation or action context). CI does not run
|
|
477
|
+
against a self-hosted Convex backend.
|
|
478
|
+
- `@vllnt/convex-flags/test` ships as TypeScript source and needs `convex-test`,
|
|
479
|
+
`vitest`, `vite` and `@edge-runtime/vm`, with the Vitest config shown under
|
|
480
|
+
Testing. `convex-test` 0.0.72 and newer require convex `^1.46.0`; on convex
|
|
481
|
+
1.45 use `convex-test` 0.0.56 to 0.0.71. The packed-package check uses convex
|
|
482
|
+
1.45.0 with `convex-test` 0.0.56.
|
|
158
483
|
|
|
159
484
|
## Testing
|
|
160
485
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
486
|
+
### Testing your app
|
|
487
|
+
|
|
488
|
+
Mount the component in your own `convex-test` suite with `register`. Use the
|
|
489
|
+
Vitest settings `convex-test` recommends (edge runtime, `convex-test` inlined).
|
|
490
|
+
`register` uses Vite's `import.meta.glob`, so it only works under Vitest/Vite:
|
|
491
|
+
|
|
492
|
+
```ts
|
|
493
|
+
// vitest.config.mts
|
|
494
|
+
import { defineConfig } from "vitest/config";
|
|
495
|
+
|
|
496
|
+
export default defineConfig({
|
|
497
|
+
test: {
|
|
498
|
+
environment: "edge-runtime",
|
|
499
|
+
server: { deps: { inline: ["convex-test"] } },
|
|
500
|
+
},
|
|
501
|
+
});
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
```ts
|
|
505
|
+
// convex/flags.test.ts
|
|
506
|
+
/// <reference types="vite/client" />
|
|
507
|
+
import { register } from "@vllnt/convex-flags/test";
|
|
508
|
+
import { convexTest } from "convex-test";
|
|
509
|
+
import { expect, test } from "vitest";
|
|
510
|
+
|
|
511
|
+
import { api, internal } from "./_generated/api";
|
|
512
|
+
import schema from "./schema";
|
|
513
|
+
|
|
514
|
+
test("the new checkout turns on", async () => {
|
|
515
|
+
const t = convexTest(schema, import.meta.glob("./**/*.ts"));
|
|
516
|
+
register(t);
|
|
517
|
+
await t.mutation(internal.flags.createNewCheckout, {});
|
|
518
|
+
expect(await t.query(api.flags.newCheckoutOn, {})).toBe(false);
|
|
519
|
+
await t.mutation(internal.flags.turnOnNewCheckout, {});
|
|
520
|
+
expect(await t.query(api.flags.newCheckoutOn, {})).toBe(true);
|
|
521
|
+
});
|
|
164
522
|
```
|
|
165
523
|
|
|
166
|
-
|
|
524
|
+
`register(t, name)` takes the mount name when you used
|
|
525
|
+
`app.use(flags, { name })`. It accepts a `convexTest` instance built from a host
|
|
526
|
+
schema with its own tables.
|
|
167
527
|
|
|
168
|
-
|
|
528
|
+
### This repository
|
|
169
529
|
|
|
170
|
-
|
|
530
|
+
`pnpm test:coverage` runs the tests on the simulated `convex-test` runtime with
|
|
531
|
+
100% coverage enforced. `pnpm test:package` installs the packed tarball in a
|
|
532
|
+
temporary app and checks these docs. It type-checks every README `ts` and `tsx`
|
|
533
|
+
example and the `docs/API.md` signatures of `register`, `useFlag` and
|
|
534
|
+
`useFlags`, and runs only the README test example (`convex/flags.test.ts`).
|
|
171
535
|
|
|
172
|
-
##
|
|
536
|
+
## Security
|
|
537
|
+
|
|
538
|
+
- The component does not authenticate. Its management functions are internal;
|
|
539
|
+
wrap them in your own mutations and authorize there.
|
|
540
|
+
- A flag is not an access check. Context sent by a client (`subjectRef`,
|
|
541
|
+
`attributes`) can be forged; derive identity and attributes on the server when
|
|
542
|
+
targeting matters.
|
|
543
|
+
- Its `flags` and `overrides` tables are reachable only through the component's
|
|
544
|
+
functions, and it never reads host tables.
|
|
545
|
+
- Flag keys, subject refs and attribute names are opaque strings. On writes the
|
|
546
|
+
component rejects only empty keys and subject refs; it does not check a key's
|
|
547
|
+
characters or length (see Limits and gotchas for keys that break `all`).
|
|
173
548
|
|
|
174
|
-
|
|
549
|
+
Report vulnerabilities as described in
|
|
550
|
+
[SECURITY.md](https://github.com/vllnt/convex-flags/blob/main/SECURITY.md).
|
|
175
551
|
|
|
176
|
-
|
|
552
|
+
## Related packages
|
|
177
553
|
|
|
178
|
-
|
|
554
|
+
None: no runtime or peer dependency, shared test or example uses another
|
|
555
|
+
`@vllnt` package.
|
|
556
|
+
|
|
557
|
+
## Contributing
|
|
558
|
+
|
|
559
|
+
See
|
|
560
|
+
[CONTRIBUTING.md](https://github.com/vllnt/convex-flags/blob/main/CONTRIBUTING.md).
|
|
561
|
+
|
|
562
|
+
## Links
|
|
563
|
+
|
|
564
|
+
- Docs and changelog: [oss.vllnt.com/packages/convex-flags](https://oss.vllnt.com/packages/convex-flags) · [release feed](https://oss.vllnt.com/packages/convex-flags/rss.xml)
|
|
565
|
+
- All @vllnt packages: [oss.vllnt.com](https://oss.vllnt.com) · [search packages and docs on index.vllnt.com](https://index.vllnt.com)
|
|
566
|
+
- Source and issues: [GitHub](https://github.com/vllnt/convex-flags) · [issues](https://github.com/vllnt/convex-flags/issues) · [npm](https://www.npmjs.com/package/@vllnt/convex-flags)
|
|
567
|
+
- Questions and updates: [Discord](https://bntvllnt.com/discord) · [X @bntvllnt](https://x.com/bntvllnt)
|
|
568
|
+
- Company and author: [vllnt.com](https://vllnt.com) · [bntvllnt.com](https://bntvllnt.com) · [GitHub @bntvllnt](https://github.com/bntvllnt) · [LinkedIn](https://linkedin.com/in/bntvllnt)
|
|
569
|
+
- Sponsor: [GitHub Sponsors](https://github.com/sponsors/bntvllnt)
|
|
179
570
|
|
|
180
571
|
## License
|
|
181
572
|
|
|
182
|
-
MIT
|
|
573
|
+
MIT. See [LICENSE](LICENSE).
|
|
574
|
+
|
|
575
|
+
[](https://www.npmjs.com/package/@vllnt/convex-flags)
|
|
576
|
+
[](https://github.com/vllnt/convex-flags/actions/workflows/ci.yml)
|
|
577
|
+
|
|
578
|
+
---
|
|
579
|
+
|
|
580
|
+
Part of [vllnt open source](https://oss.vllnt.com) · [Package page](https://oss.vllnt.com/packages/convex-flags) · [vllnt.com](https://vllnt.com) · Built by [bntvllnt](https://bntvllnt.com)
|