@vllnt/convex-flags 0.1.0 → 0.1.1-canary.c79227c
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/README.md +156 -65
- package/dist/component/mutations.d.ts.map +1 -1
- package/dist/component/mutations.js +34 -1
- package/dist/component/mutations.js.map +1 -1
- package/package.json +33 -22
- package/src/component/mutations.ts +43 -1
- package/src/react/index.test.ts +0 -52
- package/src/status-backward-compat.test.ts +0 -51
package/README.md
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
<!-- Badges -->
|
|
2
|
+
|
|
2
3
|
[](https://www.convex.dev/components)
|
|
3
4
|
[](https://www.npmjs.com/package/@vllnt/convex-flags)
|
|
4
5
|
[](https://github.com/vllnt/convex-flags/actions/workflows/ci.yml)
|
|
@@ -6,7 +7,10 @@
|
|
|
6
7
|
|
|
7
8
|
# @vllnt/convex-flags
|
|
8
9
|
|
|
9
|
-
Feature flags as a Convex component — backend-evaluated and streamed to every
|
|
10
|
+
Feature flags as a Convex component — backend-evaluated and streamed to every
|
|
11
|
+
connected client in real time: boolean kill-switches, multivariate variants,
|
|
12
|
+
percentage rollouts, attribute targeting, and per-subject overrides, with no
|
|
13
|
+
redeploy and no third-party flag SaaS.
|
|
10
14
|
|
|
11
15
|
```ts
|
|
12
16
|
const flags = new Flags(components.flags);
|
|
@@ -17,14 +21,20 @@ const on = await flags.isEnabled(ctx, "new-checkout", { subjectRef: userId }); /
|
|
|
17
21
|
|
|
18
22
|
## Features
|
|
19
23
|
|
|
20
|
-
- **Boolean, multivariate & rollouts** — boolean kill-switches, string/number
|
|
21
|
-
|
|
24
|
+
- **Boolean, multivariate & rollouts** — boolean kill-switches, string/number
|
|
25
|
+
variants, and weighted percentage rollouts with stable per-subject bucketing.
|
|
26
|
+
- **Attribute targeting** — ordered rules (`eq`, `in`, `contains`, `gt`, …) over
|
|
27
|
+
a host-supplied typed context.
|
|
22
28
|
- **Per-subject overrides** — force any flag's value for one subject.
|
|
23
29
|
- **Lifecycle** — archive (reversible) distinct from permanent delete.
|
|
24
|
-
- **Real-time** — evaluated in Convex queries, so changes stream to clients over
|
|
25
|
-
|
|
26
|
-
- **
|
|
27
|
-
|
|
30
|
+
- **Real-time** — evaluated in Convex queries, so changes stream to clients over
|
|
31
|
+
the same reactive subscriptions as your app data.
|
|
32
|
+
- **Data isolation** — definitions and overrides live in the component's own
|
|
33
|
+
sandboxed tables; no blast radius into your schema.
|
|
34
|
+
- **Reactive React hooks** — optional `./react` entry (`useFlag`/`useFlags`);
|
|
35
|
+
backend-only consumers pull in zero React.
|
|
36
|
+
- **Auth-agnostic & dual-target** — the host gates every management call; runs
|
|
37
|
+
unchanged on Convex Cloud and self-hosted `convex-backend`.
|
|
28
38
|
|
|
29
39
|
## Installation
|
|
30
40
|
|
|
@@ -43,7 +53,17 @@ app.use(flags);
|
|
|
43
53
|
export default app;
|
|
44
54
|
```
|
|
45
55
|
|
|
46
|
-
Peer dependency: `convex@^1.
|
|
56
|
+
Peer dependency on current `main`/canary: `convex@^1.45.0`. `react` is an
|
|
57
|
+
optional peer dependency, needed only for the `./react` entry.
|
|
58
|
+
|
|
59
|
+
The untagged install selects npm `latest` (0.1.0); `@canary` tracks `main`.
|
|
60
|
+
Check the installed version's peer range before upgrading.
|
|
61
|
+
|
|
62
|
+
For isolated flag sets, mount twice with names, for example
|
|
63
|
+
`app.use(flags, { name: "webFlags" })` and
|
|
64
|
+
`app.use(flags, { name: "mobileFlags" })`, then construct clients with
|
|
65
|
+
`components.webFlags` and `components.mobileFlags`. Run `npx convex dev` after
|
|
66
|
+
mounting to generate these references.
|
|
47
67
|
|
|
48
68
|
## Usage
|
|
49
69
|
|
|
@@ -54,29 +74,54 @@ Peer dependency: `convex@^1.36.1`. `react` is an optional peer dependency, neede
|
|
|
54
74
|
import { v } from "convex/values";
|
|
55
75
|
import { Flags } from "@vllnt/convex-flags";
|
|
56
76
|
import { components } from "./_generated/api";
|
|
57
|
-
import {
|
|
77
|
+
import { internalMutation, query } from "./_generated/server";
|
|
58
78
|
|
|
59
79
|
export const flags = new Flags(components.flags);
|
|
80
|
+
const value = v.union(v.boolean(), v.string(), v.number());
|
|
81
|
+
const context = v.object({
|
|
82
|
+
subjectRef: v.optional(v.string()),
|
|
83
|
+
attributes: v.optional(v.record(v.string(), value)),
|
|
84
|
+
});
|
|
85
|
+
const evaluation = v.object({
|
|
86
|
+
value,
|
|
87
|
+
reason: v.union(
|
|
88
|
+
v.literal("flag"),
|
|
89
|
+
v.literal("rule"),
|
|
90
|
+
v.literal("rollout"),
|
|
91
|
+
v.literal("override"),
|
|
92
|
+
v.literal("disabled"),
|
|
93
|
+
v.literal("default"),
|
|
94
|
+
v.literal("unknown"),
|
|
95
|
+
),
|
|
96
|
+
});
|
|
60
97
|
|
|
61
98
|
// Evaluation queries — reactive; `api.flags.evaluate` is what the React hooks below use.
|
|
62
99
|
export const evaluate = query({
|
|
63
|
-
args: {
|
|
100
|
+
args: {
|
|
101
|
+
key: v.string(),
|
|
102
|
+
context: v.optional(context),
|
|
103
|
+
default: v.optional(value),
|
|
104
|
+
},
|
|
105
|
+
returns: evaluation,
|
|
64
106
|
handler: (ctx, args) =>
|
|
65
|
-
flags.evaluate(ctx, args.key, {
|
|
107
|
+
flags.evaluate(ctx, args.key, {
|
|
108
|
+
context: args.context,
|
|
109
|
+
default: args.default,
|
|
110
|
+
}),
|
|
66
111
|
});
|
|
67
112
|
|
|
68
113
|
export const all = query({
|
|
69
|
-
args: { context: v.optional(
|
|
114
|
+
args: { context: v.optional(context) },
|
|
115
|
+
returns: v.record(v.string(), evaluation),
|
|
70
116
|
handler: (ctx, args) => flags.all(ctx, args.context),
|
|
71
117
|
});
|
|
72
118
|
|
|
73
|
-
//
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
},
|
|
119
|
+
// Internal-only management: not callable directly from a browser.
|
|
120
|
+
// If exposing a public mutation, enforce your application's admin policy first.
|
|
121
|
+
export const define = internalMutation({
|
|
122
|
+
args: { key: v.string(), value },
|
|
123
|
+
returns: v.string(),
|
|
124
|
+
handler: (ctx, args) => flags.define(ctx, args),
|
|
80
125
|
});
|
|
81
126
|
|
|
82
127
|
// Wrap the rest the same way — `update`, `enable`, `disable`, `archive`,
|
|
@@ -84,74 +129,101 @@ export const define = mutation({
|
|
|
84
129
|
```
|
|
85
130
|
|
|
86
131
|
```ts
|
|
87
|
-
//
|
|
88
|
-
|
|
89
|
-
import { query } from "./_generated/server";
|
|
132
|
+
// convex/checkout.ts
|
|
133
|
+
import { v } from "convex/values";
|
|
134
|
+
import { internalMutation, query } from "./_generated/server";
|
|
90
135
|
import { flags } from "./flags";
|
|
91
136
|
|
|
92
137
|
// A targeted rollout: pro users get the new checkout; everyone else is a 50/50 split.
|
|
93
|
-
export const setup =
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
138
|
+
export const setup = internalMutation({
|
|
139
|
+
args: {},
|
|
140
|
+
returns: v.string(),
|
|
141
|
+
handler: (ctx) =>
|
|
142
|
+
flags.define(ctx, {
|
|
143
|
+
key: "new-checkout",
|
|
144
|
+
value: false,
|
|
145
|
+
rules: [
|
|
146
|
+
{
|
|
147
|
+
conditions: [{ attribute: "plan", op: "eq", values: ["pro"] }],
|
|
148
|
+
value: true,
|
|
149
|
+
},
|
|
150
|
+
],
|
|
151
|
+
rollout: {
|
|
152
|
+
splits: [
|
|
153
|
+
{ value: true, weight: 50 },
|
|
154
|
+
{ value: false, weight: 50 },
|
|
155
|
+
],
|
|
156
|
+
},
|
|
157
|
+
}),
|
|
158
|
+
});
|
|
101
159
|
|
|
102
160
|
export const checkout = query({
|
|
103
161
|
args: {},
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
162
|
+
returns: v.boolean(),
|
|
163
|
+
// Demo identity; derive these values from trusted host data in your app.
|
|
164
|
+
handler: (ctx) =>
|
|
165
|
+
flags.isEnabled(ctx, "new-checkout", {
|
|
166
|
+
subjectRef: "user-123",
|
|
167
|
+
attributes: { plan: "free" },
|
|
168
|
+
}),
|
|
108
169
|
});
|
|
109
170
|
```
|
|
110
171
|
|
|
111
172
|
## API Reference
|
|
112
173
|
|
|
113
|
-
| Method
|
|
114
|
-
|
|
115
|
-
| `isEnabled(ctx, key, context?)`
|
|
116
|
-
| `evaluate(ctx, key, options?)`
|
|
117
|
-
| `variant(ctx, key, options?)`
|
|
118
|
-
| `get(ctx, key)` / `list(ctx)`
|
|
119
|
-
| `all(ctx, context?)`
|
|
120
|
-
| `define(ctx, definition)`
|
|
121
|
-
| `update(ctx, key, patch)`
|
|
122
|
-
| `enable(ctx, key)` / `disable(ctx, key)`
|
|
123
|
-
| `archive(ctx, key)` / `restore(ctx, key)`
|
|
124
|
-
| `setOverride(ctx, key, subjectRef, value)` / `clearOverride(ctx, key, subjectRef)` | mutation | Force / clear a per-subject value
|
|
125
|
-
| `remove(ctx, key)`
|
|
126
|
-
|
|
127
|
-
`evaluate` resolves in order: override → archived → targeting rules →
|
|
128
|
-
value; an unknown key serves the caller `default` or
|
|
129
|
-
convenience (they set the value to
|
|
130
|
-
|
|
174
|
+
| Method | Kind | Result |
|
|
175
|
+
| ---------------------------------------------------------------------------------- | -------- | ---------------------------------------------------------------------------- |
|
|
176
|
+
| `isEnabled(ctx, key, context?)` | query | `boolean` (value `=== true`) |
|
|
177
|
+
| `evaluate(ctx, key, options?)` | query | `FlagEvaluation` (`value` + `reason`) |
|
|
178
|
+
| `variant(ctx, key, options?)` | query | the resolved value |
|
|
179
|
+
| `get(ctx, key)` / `list(ctx)` | query | `FlagDoc \| null` / `FlagDoc[]` |
|
|
180
|
+
| `all(ctx, context?)` | query | `Record<string, FlagEvaluation>` (reactive bootstrap) |
|
|
181
|
+
| `define(ctx, definition)` | mutation | Create or **fully replace** a flag (value, variants, rules, rollout) |
|
|
182
|
+
| `update(ctx, key, patch)` | mutation | **Partially** update a flag — only supplied fields change, the rest are kept |
|
|
183
|
+
| `enable(ctx, key)` / `disable(ctx, key)` | mutation | Toggle a boolean flag (sets value `true`/`false`) |
|
|
184
|
+
| `archive(ctx, key)` / `restore(ctx, key)` | mutation | Reversibly retire / re-activate a flag |
|
|
185
|
+
| `setOverride(ctx, key, subjectRef, value)` / `clearOverride(ctx, key, subjectRef)` | mutation | Force / clear a per-subject value |
|
|
186
|
+
| `remove(ctx, key)` | mutation | Hard-delete a flag and its overrides |
|
|
187
|
+
|
|
188
|
+
`evaluate` resolves in order: override → archived → targeting rules →
|
|
189
|
+
fallthrough rollout → flag value; an unknown key serves the caller `default` or
|
|
190
|
+
`false`. `enable`/`disable` are boolean convenience (they set the value to
|
|
191
|
+
`true`/`false`) — use `define`/`update` for multivariate flags. Full reference,
|
|
192
|
+
types, operators, and the React hooks: [docs/API.md](docs/API.md).
|
|
131
193
|
|
|
132
194
|
## React
|
|
133
195
|
|
|
134
|
-
Optional reactive hooks via the `./react` entry (`react` is an optional peer
|
|
135
|
-
`evaluate` query you exported from `convex/flags.ts` above
|
|
196
|
+
Optional reactive hooks via the `./react` entry (`react` is an optional peer
|
|
197
|
+
dependency). Pass the `evaluate` query you exported from `convex/flags.ts` above
|
|
198
|
+
— `api.flags.evaluate`:
|
|
136
199
|
|
|
137
200
|
```tsx
|
|
138
201
|
import { useFlag } from "@vllnt/convex-flags/react";
|
|
139
202
|
import { api } from "../convex/_generated/api";
|
|
140
203
|
|
|
141
204
|
// api.flags.evaluate is the query exported in convex/flags.ts (Usage, above).
|
|
142
|
-
const checkout = useFlag(api.flags.evaluate, "new-checkout", {
|
|
143
|
-
|
|
205
|
+
const checkout = useFlag(api.flags.evaluate, "new-checkout", {
|
|
206
|
+
context: { subjectRef: userId },
|
|
207
|
+
});
|
|
208
|
+
if (checkout?.value === true) {
|
|
209
|
+
/* render the new checkout */
|
|
210
|
+
}
|
|
144
211
|
```
|
|
145
212
|
|
|
146
|
-
| Hook
|
|
147
|
-
|
|
148
|
-
| `useFlag(query, key, options?)` | `FlagEvaluation \| undefined`
|
|
149
|
-
| `useFlags(query, context?)`
|
|
213
|
+
| Hook | Returns |
|
|
214
|
+
| ------------------------------- | --------------------------------------------- |
|
|
215
|
+
| `useFlag(query, key, options?)` | `FlagEvaluation \| undefined` |
|
|
216
|
+
| `useFlags(query, context?)` | `Record<string, FlagEvaluation> \| undefined` |
|
|
150
217
|
|
|
151
218
|
## Security
|
|
152
219
|
|
|
153
|
-
- Auth-agnostic — all management mutations are internal; wrap them in your own
|
|
154
|
-
|
|
220
|
+
- Auth-agnostic — all management mutations are internal; wrap them in your own
|
|
221
|
+
authorized mutations and apply auth there.
|
|
222
|
+
- The example evaluation queries accept client-supplied context. For trusted
|
|
223
|
+
targeting, derive identity and attributes server-side and gate reads; a flag
|
|
224
|
+
is not an authorization check.
|
|
225
|
+
- Flag keys are opaque host-chosen strings; the component never interprets or
|
|
226
|
+
validates their structure.
|
|
155
227
|
- Tables are sandboxed — the component never reads host or sibling tables.
|
|
156
228
|
|
|
157
229
|
See [docs/API.md](docs/API.md).
|
|
@@ -163,7 +235,24 @@ pnpm test # single run
|
|
|
163
235
|
pnpm test:coverage # enforced 100% on covered files
|
|
164
236
|
```
|
|
165
237
|
|
|
166
|
-
Tests run against the real component runtime via `convex-test`
|
|
238
|
+
Tests run against the real component runtime via `convex-test`
|
|
239
|
+
(`@edge-runtime/vm`), not mocks.
|
|
240
|
+
|
|
241
|
+
### Testing your app
|
|
242
|
+
|
|
243
|
+
`@vllnt/convex-flags/test` exports `register(t, name = "flags")` to mount the
|
|
244
|
+
component in a `convex-test` instance. It needs `convex-test` and `vitest`
|
|
245
|
+
(with Vite's `import.meta.glob`) in the consuming app:
|
|
246
|
+
|
|
247
|
+
```ts
|
|
248
|
+
/// <reference types="vite/client" />
|
|
249
|
+
import { convexTest } from "convex-test";
|
|
250
|
+
import { register } from "@vllnt/convex-flags/test";
|
|
251
|
+
import schema from "./schema";
|
|
252
|
+
|
|
253
|
+
const t = convexTest(schema, import.meta.glob("./**/*.ts"));
|
|
254
|
+
register(t);
|
|
255
|
+
```
|
|
167
256
|
|
|
168
257
|
## Contributing
|
|
169
258
|
|
|
@@ -171,9 +260,11 @@ See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
|
171
260
|
|
|
172
261
|
## Author
|
|
173
262
|
|
|
174
|
-
Built by [bntvllnt](https://github.com/bntvllnt) ·
|
|
263
|
+
Built by [bntvllnt](https://github.com/bntvllnt) ·
|
|
264
|
+
[bntvllnt.com](https://bntvllnt.com) · [X @bntvllnt](https://x.com/bntvllnt)
|
|
175
265
|
|
|
176
|
-
Part of the [@vllnt](https://github.com/vllnt) Convex component fleet —
|
|
266
|
+
Part of the [@vllnt](https://github.com/vllnt) Convex component fleet —
|
|
267
|
+
[vllnt.com](https://vllnt.com)
|
|
177
268
|
|
|
178
269
|
If this is useful, [sponsor the work](https://github.com/sponsors/bntvllnt).
|
|
179
270
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AAoDA;;;GAGG;AACH,eAAO,MAAM,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;uDAsCjB,CAAC;AAEH;;;;GAIG;AACH,eAAO,MAAM,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAoCjB,CAAC;AAWH,wDAAwD;AACxD,eAAO,MAAM,MAAM;;iBAOjB,CAAC;AAEH,0DAA0D;AAC1D,eAAO,MAAM,OAAO;;iBAOlB,CAAC;AAeH;mDACmD;AACnD,eAAO,MAAM,OAAO;;iBAOlB,CAAC;AAEH,0CAA0C;AAC1C,eAAO,MAAM,OAAO;;iBAOlB,CAAC;AAEH,uEAAuE;AACvE,eAAO,MAAM,MAAM;;iBAmBjB,CAAC;AAEH,iEAAiE;AACjE,eAAO,MAAM,WAAW;;;;iBA2BtB,CAAC;AAEH,mEAAmE;AACnE,eAAO,MAAM,aAAa;;;iBAgBxB,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { v } from "convex/values";
|
|
1
|
+
import { ConvexError, v } from "convex/values";
|
|
2
2
|
import { mutation } from "./_generated/server.js";
|
|
3
3
|
import { rollout, rule, variant, variantValue } from "./validators.js";
|
|
4
4
|
async function findByKey(ctx, key) {
|
|
@@ -7,6 +7,30 @@ async function findByKey(ctx, key) {
|
|
|
7
7
|
.withIndex("by_key", (q) => q.eq("key", key))
|
|
8
8
|
.unique();
|
|
9
9
|
}
|
|
10
|
+
function invalidArgument(message) {
|
|
11
|
+
throw new ConvexError({ code: "INVALID_ARGUMENT", message });
|
|
12
|
+
}
|
|
13
|
+
/** Reject empty identifiers: an empty key or subject ref can never be targeted
|
|
14
|
+
* meaningfully and would store an unreachable row. */
|
|
15
|
+
function assertNonEmpty(fields) {
|
|
16
|
+
for (const [name, value] of Object.entries(fields)) {
|
|
17
|
+
if (value === "") {
|
|
18
|
+
invalidArgument(`${name} must be non-empty`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/** Reject rollout weights that cannot bucket: NaN, infinite, or negative. Zero
|
|
23
|
+
* is allowed (it excludes a split). */
|
|
24
|
+
function assertRollouts(rollouts, rules) {
|
|
25
|
+
const all = [...rollouts, ...(rules ?? []).map((r) => r.rollout)];
|
|
26
|
+
for (const r of all) {
|
|
27
|
+
for (const { weight } of r?.splits ?? []) {
|
|
28
|
+
if (!Number.isFinite(weight) || weight < 0) {
|
|
29
|
+
invalidArgument("rollout weights must be finite and non-negative");
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
10
34
|
/** Throws the component's single code-tagged error. Returns `never` so callers
|
|
11
35
|
* narrow the flag to non-null after the guard. */
|
|
12
36
|
function flagNotFound() {
|
|
@@ -27,6 +51,8 @@ export const define = mutation({
|
|
|
27
51
|
},
|
|
28
52
|
returns: v.id("flags"),
|
|
29
53
|
handler: async (ctx, args) => {
|
|
54
|
+
assertNonEmpty({ key: args.key });
|
|
55
|
+
assertRollouts([args.rollout], args.rules);
|
|
30
56
|
const now = Date.now();
|
|
31
57
|
const existing = await findByKey(ctx, args.key);
|
|
32
58
|
if (existing !== null) {
|
|
@@ -69,6 +95,8 @@ export const update = mutation({
|
|
|
69
95
|
},
|
|
70
96
|
returns: v.null(),
|
|
71
97
|
handler: async (ctx, args) => {
|
|
98
|
+
assertNonEmpty({ key: args.key });
|
|
99
|
+
assertRollouts([args.rollout], args.rules);
|
|
72
100
|
const flag = await findByKey(ctx, args.key);
|
|
73
101
|
if (flag === null) {
|
|
74
102
|
flagNotFound();
|
|
@@ -94,6 +122,7 @@ export const update = mutation({
|
|
|
94
122
|
},
|
|
95
123
|
});
|
|
96
124
|
async function setValue(ctx, key, value) {
|
|
125
|
+
assertNonEmpty({ key });
|
|
97
126
|
const flag = await findByKey(ctx, key);
|
|
98
127
|
if (flag === null) {
|
|
99
128
|
flagNotFound();
|
|
@@ -119,6 +148,7 @@ export const disable = mutation({
|
|
|
119
148
|
},
|
|
120
149
|
});
|
|
121
150
|
async function setStatus(ctx, key, status) {
|
|
151
|
+
assertNonEmpty({ key });
|
|
122
152
|
const flag = await findByKey(ctx, key);
|
|
123
153
|
if (flag === null) {
|
|
124
154
|
flagNotFound();
|
|
@@ -149,6 +179,7 @@ export const remove = mutation({
|
|
|
149
179
|
args: { key: v.string() },
|
|
150
180
|
returns: v.null(),
|
|
151
181
|
handler: async (ctx, args) => {
|
|
182
|
+
assertNonEmpty({ key: args.key });
|
|
152
183
|
const flag = await findByKey(ctx, args.key);
|
|
153
184
|
if (flag === null) {
|
|
154
185
|
flagNotFound();
|
|
@@ -169,6 +200,7 @@ export const setOverride = mutation({
|
|
|
169
200
|
args: { key: v.string(), subjectRef: v.string(), value: variantValue },
|
|
170
201
|
returns: v.null(),
|
|
171
202
|
handler: async (ctx, args) => {
|
|
203
|
+
assertNonEmpty({ key: args.key, subjectRef: args.subjectRef });
|
|
172
204
|
const flag = await findByKey(ctx, args.key);
|
|
173
205
|
if (flag === null) {
|
|
174
206
|
flagNotFound();
|
|
@@ -196,6 +228,7 @@ export const clearOverride = mutation({
|
|
|
196
228
|
args: { key: v.string(), subjectRef: v.string() },
|
|
197
229
|
returns: v.null(),
|
|
198
230
|
handler: async (ctx, args) => {
|
|
231
|
+
assertNonEmpty({ key: args.key, subjectRef: args.subjectRef });
|
|
199
232
|
const existing = await ctx.db
|
|
200
233
|
.query("overrides")
|
|
201
234
|
.withIndex("by_key_subject", (q) => q.eq("key", args.key).eq("subjectRef", args.subjectRef))
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mutations.js","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"mutations.js","sourceRoot":"","sources":["../../src/component/mutations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,CAAC,EAAc,MAAM,eAAe,CAAC;AAC3D,OAAO,EAAE,QAAQ,EAAE,MAAM,wBAAwB,CAAC;AAGlD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAKvE,KAAK,UAAU,SAAS,CAAC,GAAgB,EAAE,GAAW;IACpD,OAAO,MAAM,GAAG,CAAC,EAAE;SAChB,KAAK,CAAC,OAAO,CAAC;SACd,SAAS,CAAC,QAAQ,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;SAC5C,MAAM,EAAE,CAAC;AACd,CAAC;AAED,SAAS,eAAe,CAAC,OAAe;IACtC,MAAM,IAAI,WAAW,CAAC,EAAE,IAAI,EAAE,kBAAkB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ACjD,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;CACF,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vllnt/convex-flags",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1-canary.c79227c",
|
|
4
4
|
"description": "Feature flags as a Convex component — variants, targeting, percentage rollouts, and per-subject overrides, evaluated on the backend and streamed live to clients.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"packageManager": "pnpm@
|
|
7
|
+
"packageManager": "pnpm@10.34.8",
|
|
8
8
|
"files": [
|
|
9
9
|
"dist",
|
|
10
|
-
"src"
|
|
10
|
+
"src",
|
|
11
|
+
"!src/**/*.test.*"
|
|
11
12
|
],
|
|
12
13
|
"exports": {
|
|
13
14
|
"./package.json": "./package.json",
|
|
@@ -41,17 +42,13 @@
|
|
|
41
42
|
"build": "tsc --project ./tsconfig.build.json",
|
|
42
43
|
"build:codegen": "pnpm codegen && pnpm build",
|
|
43
44
|
"build:clean": "rm -rf dist *.tsbuildinfo && pnpm build:codegen",
|
|
44
|
-
"typecheck": "tsc --noEmit",
|
|
45
|
-
"typecheck:ci": "tsc --noEmit --project tsconfig.ci.json",
|
|
46
|
-
"lint": "pnpm check:agents && eslint .",
|
|
45
|
+
"typecheck": "tsc --noEmit --incremental --tsBuildInfoFile node_modules/.cache/tsc/typecheck.tsbuildinfo",
|
|
46
|
+
"typecheck:ci": "tsc --noEmit --incremental --tsBuildInfoFile node_modules/.cache/tsc/typecheck-ci.tsbuildinfo --project tsconfig.ci.json",
|
|
47
|
+
"lint": "pnpm check:agents && eslint --cache --cache-location node_modules/.cache/eslint/ .",
|
|
47
48
|
"test": "vitest run --passWithNoTests",
|
|
48
49
|
"test:watch": "vitest --typecheck --clearScreen false",
|
|
49
50
|
"test:coverage": "vitest run --coverage --coverage.reporter=text",
|
|
50
|
-
"
|
|
51
|
-
"preversion": "pnpm install --frozen-lockfile && pnpm build && pnpm test:coverage && pnpm typecheck && pnpm generate:llms",
|
|
52
|
-
"prepublishOnly": "npm whoami || npm login",
|
|
53
|
-
"alpha": "npm version prerelease --preid alpha && npm publish --tag alpha && git push --follow-tags",
|
|
54
|
-
"release": "npm version patch && npm publish && git push --follow-tags",
|
|
51
|
+
"preversion": "pnpm install --frozen-lockfile && pnpm build && pnpm test:coverage && pnpm typecheck",
|
|
55
52
|
"check:agents": "node -e \"const f=require('node:fs');if(!f.existsSync('AGENTS.md')||f.existsSync('CLAUDE.md')||f.existsSync('.claude'))process.exit(1)\"",
|
|
56
53
|
"codegen": "pnpm convex codegen --component-dir ./src/component"
|
|
57
54
|
},
|
|
@@ -67,22 +64,36 @@
|
|
|
67
64
|
"devDependencies": {
|
|
68
65
|
"@edge-runtime/vm": "^5.0.0",
|
|
69
66
|
"@testing-library/react": "^16.3.3",
|
|
70
|
-
"@types/react": "^19.
|
|
71
|
-
"@types/react-dom": "^19.
|
|
67
|
+
"@types/react": "^19.3.0",
|
|
68
|
+
"@types/react-dom": "^19.3.0",
|
|
72
69
|
"@typescript/native": "npm:typescript@7.0.2",
|
|
73
|
-
"@vitest/coverage-v8": "^
|
|
70
|
+
"@vitest/coverage-v8": "^5.0.5",
|
|
74
71
|
"@vllnt/eslint-config": "2.0.0",
|
|
75
72
|
"@vllnt/typescript": "2.0.0",
|
|
76
|
-
"convex": "^1.
|
|
77
|
-
"convex-test": "^0.0.
|
|
73
|
+
"convex": "^1.48.0",
|
|
74
|
+
"convex-test": "^0.0.65",
|
|
78
75
|
"eslint": "^9.39.5",
|
|
79
|
-
"jsdom": "^
|
|
80
|
-
"prettier": "^3.9.
|
|
81
|
-
"react": "^19.
|
|
82
|
-
"react-dom": "^19.
|
|
76
|
+
"jsdom": "^30.1.3",
|
|
77
|
+
"prettier": "^3.9.10",
|
|
78
|
+
"react": "^19.3.1",
|
|
79
|
+
"react-dom": "^19.3.1",
|
|
83
80
|
"typescript": "npm:@typescript/typescript6@^6.0.2",
|
|
84
|
-
"vite": "^8.
|
|
85
|
-
"vitest": "^
|
|
81
|
+
"vite": "^8.3.5",
|
|
82
|
+
"vitest": "^5.0.5"
|
|
83
|
+
},
|
|
84
|
+
"pnpm": {
|
|
85
|
+
"minimumReleaseAge": 10080,
|
|
86
|
+
"minimumReleaseAgeExclude": [
|
|
87
|
+
"@vllnt/*"
|
|
88
|
+
],
|
|
89
|
+
"ignoredBuiltDependencies": [
|
|
90
|
+
"esbuild"
|
|
91
|
+
],
|
|
92
|
+
"overrides": {
|
|
93
|
+
"deepmerge-ts@<8.0.0": "8.0.0",
|
|
94
|
+
"esbuild@>=0.27.3 <0.28.1": "0.28.1",
|
|
95
|
+
"handlebars@<4.7.10": "4.7.10"
|
|
96
|
+
}
|
|
86
97
|
},
|
|
87
98
|
"homepage": "https://github.com/vllnt/convex-flags#readme",
|
|
88
99
|
"bugs": {
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import { v } from "convex/values";
|
|
1
|
+
import { ConvexError, v, type Infer } from "convex/values";
|
|
2
2
|
import { mutation } from "./_generated/server.js";
|
|
3
3
|
import type { Doc } from "./_generated/dataModel.js";
|
|
4
4
|
import type { MutationCtx } from "./_generated/server.js";
|
|
5
5
|
import { rollout, rule, variant, variantValue } from "./validators.js";
|
|
6
6
|
|
|
7
|
+
type Rollout = Infer<typeof rollout>;
|
|
8
|
+
type Rule = Infer<typeof rule>;
|
|
9
|
+
|
|
7
10
|
async function findByKey(ctx: MutationCtx, key: string): Promise<Doc<"flags"> | null> {
|
|
8
11
|
return await ctx.db
|
|
9
12
|
.query("flags")
|
|
@@ -11,6 +14,36 @@ async function findByKey(ctx: MutationCtx, key: string): Promise<Doc<"flags"> |
|
|
|
11
14
|
.unique();
|
|
12
15
|
}
|
|
13
16
|
|
|
17
|
+
function invalidArgument(message: string): never {
|
|
18
|
+
throw new ConvexError({ code: "INVALID_ARGUMENT", message });
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Reject empty identifiers: an empty key or subject ref can never be targeted
|
|
22
|
+
* meaningfully and would store an unreachable row. */
|
|
23
|
+
function assertNonEmpty(fields: Record<string, string>): void {
|
|
24
|
+
for (const [name, value] of Object.entries(fields)) {
|
|
25
|
+
if (value === "") {
|
|
26
|
+
invalidArgument(`${name} must be non-empty`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Reject rollout weights that cannot bucket: NaN, infinite, or negative. Zero
|
|
32
|
+
* is allowed (it excludes a split). */
|
|
33
|
+
function assertRollouts(
|
|
34
|
+
rollouts: Array<Rollout | undefined>,
|
|
35
|
+
rules: Rule[] | undefined,
|
|
36
|
+
): void {
|
|
37
|
+
const all = [...rollouts, ...(rules ?? []).map((r) => r.rollout)];
|
|
38
|
+
for (const r of all) {
|
|
39
|
+
for (const { weight } of r?.splits ?? []) {
|
|
40
|
+
if (!Number.isFinite(weight) || weight < 0) {
|
|
41
|
+
invalidArgument("rollout weights must be finite and non-negative");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
14
47
|
/** Throws the component's single code-tagged error. Returns `never` so callers
|
|
15
48
|
* narrow the flag to non-null after the guard. */
|
|
16
49
|
function flagNotFound(): never {
|
|
@@ -32,6 +65,8 @@ export const define = mutation({
|
|
|
32
65
|
},
|
|
33
66
|
returns: v.id("flags"),
|
|
34
67
|
handler: async (ctx, args) => {
|
|
68
|
+
assertNonEmpty({ key: args.key });
|
|
69
|
+
assertRollouts([args.rollout], args.rules);
|
|
35
70
|
const now = Date.now();
|
|
36
71
|
const existing = await findByKey(ctx, args.key);
|
|
37
72
|
if (existing !== null) {
|
|
@@ -75,6 +110,8 @@ export const update = mutation({
|
|
|
75
110
|
},
|
|
76
111
|
returns: v.null(),
|
|
77
112
|
handler: async (ctx, args) => {
|
|
113
|
+
assertNonEmpty({ key: args.key });
|
|
114
|
+
assertRollouts([args.rollout], args.rules);
|
|
78
115
|
const flag = await findByKey(ctx, args.key);
|
|
79
116
|
if (flag === null) {
|
|
80
117
|
flagNotFound();
|
|
@@ -101,6 +138,7 @@ export const update = mutation({
|
|
|
101
138
|
});
|
|
102
139
|
|
|
103
140
|
async function setValue(ctx: MutationCtx, key: string, value: boolean): Promise<void> {
|
|
141
|
+
assertNonEmpty({ key });
|
|
104
142
|
const flag = await findByKey(ctx, key);
|
|
105
143
|
if (flag === null) {
|
|
106
144
|
flagNotFound();
|
|
@@ -133,6 +171,7 @@ async function setStatus(
|
|
|
133
171
|
key: string,
|
|
134
172
|
status: "active" | "archived",
|
|
135
173
|
): Promise<void> {
|
|
174
|
+
assertNonEmpty({ key });
|
|
136
175
|
const flag = await findByKey(ctx, key);
|
|
137
176
|
if (flag === null) {
|
|
138
177
|
flagNotFound();
|
|
@@ -166,6 +205,7 @@ export const remove = mutation({
|
|
|
166
205
|
args: { key: v.string() },
|
|
167
206
|
returns: v.null(),
|
|
168
207
|
handler: async (ctx, args) => {
|
|
208
|
+
assertNonEmpty({ key: args.key });
|
|
169
209
|
const flag = await findByKey(ctx, args.key);
|
|
170
210
|
if (flag === null) {
|
|
171
211
|
flagNotFound();
|
|
@@ -187,6 +227,7 @@ export const setOverride = mutation({
|
|
|
187
227
|
args: { key: v.string(), subjectRef: v.string(), value: variantValue },
|
|
188
228
|
returns: v.null(),
|
|
189
229
|
handler: async (ctx, args) => {
|
|
230
|
+
assertNonEmpty({ key: args.key, subjectRef: args.subjectRef });
|
|
190
231
|
const flag = await findByKey(ctx, args.key);
|
|
191
232
|
if (flag === null) {
|
|
192
233
|
flagNotFound();
|
|
@@ -216,6 +257,7 @@ export const clearOverride = mutation({
|
|
|
216
257
|
args: { key: v.string(), subjectRef: v.string() },
|
|
217
258
|
returns: v.null(),
|
|
218
259
|
handler: async (ctx, args) => {
|
|
260
|
+
assertNonEmpty({ key: args.key, subjectRef: args.subjectRef });
|
|
219
261
|
const existing = await ctx.db
|
|
220
262
|
.query("overrides")
|
|
221
263
|
.withIndex("by_key_subject", (q) =>
|
package/src/react/index.test.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
// @vitest-environment jsdom
|
|
2
|
-
import { renderHook } from "@testing-library/react";
|
|
3
|
-
import { makeFunctionReference } from "convex/server";
|
|
4
|
-
import { beforeEach, expect, test, vi } from "vitest";
|
|
5
|
-
import { useFlag, useFlags, type AllRef, type EvaluateRef } from "./index.js";
|
|
6
|
-
|
|
7
|
-
const { useQuerySpy } = vi.hoisted(() => ({ useQuerySpy: vi.fn() }));
|
|
8
|
-
vi.mock("convex/react", () => ({ useQuery: useQuerySpy }));
|
|
9
|
-
|
|
10
|
-
const evalRef: EvaluateRef = makeFunctionReference<"query">("flags:evaluate");
|
|
11
|
-
const allRef: AllRef = makeFunctionReference<"query">("flags:all");
|
|
12
|
-
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
useQuerySpy.mockReset();
|
|
15
|
-
useQuerySpy.mockReturnValue({ value: true, reason: "flag" });
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
test("useFlag forwards key + context + default and returns the query result", () => {
|
|
19
|
-
const { result } = renderHook(() =>
|
|
20
|
-
useFlag(evalRef, "new-checkout", {
|
|
21
|
-
context: { subjectRef: "u1", attributes: { plan: "pro" } },
|
|
22
|
-
default: "off",
|
|
23
|
-
}),
|
|
24
|
-
);
|
|
25
|
-
expect(useQuerySpy).toHaveBeenCalledWith(evalRef, {
|
|
26
|
-
key: "new-checkout",
|
|
27
|
-
context: { subjectRef: "u1", attributes: { plan: "pro" } },
|
|
28
|
-
default: "off",
|
|
29
|
-
});
|
|
30
|
-
expect(result.current).toEqual({ value: true, reason: "flag" });
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test("useFlag works with no options", () => {
|
|
34
|
-
renderHook(() => useFlag(evalRef, "k"));
|
|
35
|
-
expect(useQuerySpy).toHaveBeenCalledWith(evalRef, {
|
|
36
|
-
key: "k",
|
|
37
|
-
context: undefined,
|
|
38
|
-
default: undefined,
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
test("useFlags forwards context", () => {
|
|
43
|
-
useQuerySpy.mockReturnValue({ k: { value: true, reason: "flag" } });
|
|
44
|
-
const { result } = renderHook(() => useFlags(allRef, { subjectRef: "u1" }));
|
|
45
|
-
expect(useQuerySpy).toHaveBeenCalledWith(allRef, { context: { subjectRef: "u1" } });
|
|
46
|
-
expect(result.current).toEqual({ k: { value: true, reason: "flag" } });
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test("useFlags works with no context", () => {
|
|
50
|
-
renderHook(() => useFlags(allRef));
|
|
51
|
-
expect(useQuerySpy).toHaveBeenCalledWith(allRef, { context: undefined });
|
|
52
|
-
});
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "vitest";
|
|
2
|
-
import { flagFields } from "./component/validators.js";
|
|
3
|
-
import { evaluateFlag, withStatus } from "./shared.js";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Regression guard for #5 — a required `status` field broke deploys for any
|
|
7
|
-
* consumer with flag rows written before `status` existed. The expand fix makes
|
|
8
|
-
* `status` optional and reads an absent status as `"active"`.
|
|
9
|
-
*/
|
|
10
|
-
describe("status backward compatibility (#5)", () => {
|
|
11
|
-
test("the schema validator accepts a row without `status` (legacy data)", () => {
|
|
12
|
-
expect(flagFields.status.isOptional).toBe("optional");
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
test("a flag with no `status` is evaluated as active, not disabled", () => {
|
|
16
|
-
expect(evaluateFlag("legacy", { value: true }, {})).toEqual({
|
|
17
|
-
value: true,
|
|
18
|
-
reason: "flag",
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test("a status-less flag still runs targeting (absent status !== archived)", () => {
|
|
23
|
-
expect(
|
|
24
|
-
evaluateFlag(
|
|
25
|
-
"legacy",
|
|
26
|
-
{ value: "BASE", rules: [{ conditions: [], value: "RULE" }] },
|
|
27
|
-
{},
|
|
28
|
-
),
|
|
29
|
-
).toEqual({ value: "RULE", reason: "rule" });
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
test("an explicitly archived flag is still disabled", () => {
|
|
33
|
-
expect(evaluateFlag("k", { value: "BASE", status: "archived" }, {})).toEqual({
|
|
34
|
-
value: "BASE",
|
|
35
|
-
reason: "disabled",
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test("withStatus materializes an absent status as active for returned docs", () => {
|
|
40
|
-
const legacyRow: { key: string; value: boolean; status?: "active" | "archived" } = {
|
|
41
|
-
key: "legacy",
|
|
42
|
-
value: true,
|
|
43
|
-
};
|
|
44
|
-
expect(withStatus(legacyRow).status).toBe("active");
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
test("withStatus preserves an explicit status", () => {
|
|
48
|
-
const archivedRow = { key: "k", value: true, status: "archived" as const };
|
|
49
|
-
expect(withStatus(archivedRow).status).toBe("archived");
|
|
50
|
-
});
|
|
51
|
-
});
|