@contractspec/lib.feature-flags 1.57.0 → 1.59.0

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.
Files changed (53) hide show
  1. package/dist/browser/contracts/index.js +636 -0
  2. package/dist/browser/docs/feature-flags.docblock.js +71 -0
  3. package/dist/browser/docs/index.js +71 -0
  4. package/dist/browser/entities/index.js +306 -0
  5. package/dist/browser/evaluation/index.js +223 -0
  6. package/dist/browser/events.js +296 -0
  7. package/dist/browser/feature-flags.capability.js +28 -0
  8. package/dist/browser/feature-flags.feature.js +55 -0
  9. package/dist/browser/index.js +1583 -0
  10. package/dist/contracts/index.d.ts +944 -950
  11. package/dist/contracts/index.d.ts.map +1 -1
  12. package/dist/contracts/index.js +635 -906
  13. package/dist/docs/feature-flags.docblock.d.ts +2 -1
  14. package/dist/docs/feature-flags.docblock.d.ts.map +1 -0
  15. package/dist/docs/feature-flags.docblock.js +18 -22
  16. package/dist/docs/index.d.ts +2 -1
  17. package/dist/docs/index.d.ts.map +1 -0
  18. package/dist/docs/index.js +72 -1
  19. package/dist/entities/index.d.ts +159 -164
  20. package/dist/entities/index.d.ts.map +1 -1
  21. package/dist/entities/index.js +297 -315
  22. package/dist/evaluation/index.d.ts +119 -122
  23. package/dist/evaluation/index.d.ts.map +1 -1
  24. package/dist/evaluation/index.js +215 -212
  25. package/dist/events.d.ts +480 -486
  26. package/dist/events.d.ts.map +1 -1
  27. package/dist/events.js +272 -511
  28. package/dist/feature-flags.capability.d.ts +2 -7
  29. package/dist/feature-flags.capability.d.ts.map +1 -1
  30. package/dist/feature-flags.capability.js +29 -25
  31. package/dist/feature-flags.feature.d.ts +1 -6
  32. package/dist/feature-flags.feature.d.ts.map +1 -1
  33. package/dist/feature-flags.feature.js +54 -146
  34. package/dist/index.d.ts +7 -6
  35. package/dist/index.d.ts.map +1 -0
  36. package/dist/index.js +1584 -8
  37. package/dist/node/contracts/index.js +636 -0
  38. package/dist/node/docs/feature-flags.docblock.js +71 -0
  39. package/dist/node/docs/index.js +71 -0
  40. package/dist/node/entities/index.js +306 -0
  41. package/dist/node/evaluation/index.js +223 -0
  42. package/dist/node/events.js +296 -0
  43. package/dist/node/feature-flags.capability.js +28 -0
  44. package/dist/node/feature-flags.feature.js +55 -0
  45. package/dist/node/index.js +1583 -0
  46. package/package.json +117 -30
  47. package/dist/contracts/index.js.map +0 -1
  48. package/dist/docs/feature-flags.docblock.js.map +0 -1
  49. package/dist/entities/index.js.map +0 -1
  50. package/dist/evaluation/index.js.map +0 -1
  51. package/dist/events.js.map +0 -1
  52. package/dist/feature-flags.capability.js.map +0 -1
  53. package/dist/feature-flags.feature.js.map +0 -1
@@ -0,0 +1,636 @@
1
+ // src/contracts/index.ts
2
+ import { ScalarTypeEnum, defineSchemaModel } from "@contractspec/lib.schema";
3
+ import { defineCommand, defineQuery } from "@contractspec/lib.contracts";
4
+ var OWNERS = ["platform.feature-flags"];
5
+ var FeatureFlagModel = defineSchemaModel({
6
+ name: "FeatureFlag",
7
+ description: "Represents a feature flag",
8
+ fields: {
9
+ id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
10
+ key: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
11
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
12
+ description: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
13
+ status: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
14
+ defaultValue: { type: ScalarTypeEnum.Boolean(), isOptional: false },
15
+ variants: { type: ScalarTypeEnum.JSON(), isOptional: true },
16
+ orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
17
+ tags: { type: ScalarTypeEnum.JSON(), isOptional: true },
18
+ createdAt: { type: ScalarTypeEnum.DateTime(), isOptional: false },
19
+ updatedAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
20
+ }
21
+ });
22
+ var TargetingRuleModel = defineSchemaModel({
23
+ name: "TargetingRule",
24
+ description: "Represents a targeting rule",
25
+ fields: {
26
+ id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
27
+ flagId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
28
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
29
+ priority: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false },
30
+ enabled: { type: ScalarTypeEnum.Boolean(), isOptional: false },
31
+ attribute: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
32
+ operator: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
33
+ value: { type: ScalarTypeEnum.JSON(), isOptional: false },
34
+ rolloutPercentage: {
35
+ type: ScalarTypeEnum.Int_unsecure(),
36
+ isOptional: true
37
+ },
38
+ serveValue: { type: ScalarTypeEnum.Boolean(), isOptional: true },
39
+ serveVariant: { type: ScalarTypeEnum.String_unsecure(), isOptional: true }
40
+ }
41
+ });
42
+ var ExperimentModel = defineSchemaModel({
43
+ name: "Experiment",
44
+ description: "Represents an experiment",
45
+ fields: {
46
+ id: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
47
+ key: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
48
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
49
+ description: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
50
+ hypothesis: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
51
+ flagId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
52
+ status: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
53
+ variants: { type: ScalarTypeEnum.JSON(), isOptional: false },
54
+ metrics: { type: ScalarTypeEnum.JSON(), isOptional: true },
55
+ audiencePercentage: {
56
+ type: ScalarTypeEnum.Int_unsecure(),
57
+ isOptional: false
58
+ },
59
+ startedAt: { type: ScalarTypeEnum.DateTime(), isOptional: true },
60
+ endedAt: { type: ScalarTypeEnum.DateTime(), isOptional: true },
61
+ winningVariant: {
62
+ type: ScalarTypeEnum.String_unsecure(),
63
+ isOptional: true
64
+ },
65
+ results: { type: ScalarTypeEnum.JSON(), isOptional: true },
66
+ createdAt: { type: ScalarTypeEnum.DateTime(), isOptional: false }
67
+ }
68
+ });
69
+ var EvaluationResultModel = defineSchemaModel({
70
+ name: "EvaluationResult",
71
+ description: "Result of flag evaluation",
72
+ fields: {
73
+ enabled: { type: ScalarTypeEnum.Boolean(), isOptional: false },
74
+ variant: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
75
+ reason: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
76
+ ruleId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
77
+ experimentId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true }
78
+ }
79
+ });
80
+ var CreateFlagInput = defineSchemaModel({
81
+ name: "CreateFlagInput",
82
+ description: "Input for creating a feature flag",
83
+ fields: {
84
+ key: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
85
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
86
+ description: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
87
+ defaultValue: { type: ScalarTypeEnum.Boolean(), isOptional: true },
88
+ variants: { type: ScalarTypeEnum.JSON(), isOptional: true },
89
+ orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
90
+ tags: { type: ScalarTypeEnum.JSON(), isOptional: true }
91
+ }
92
+ });
93
+ var UpdateFlagInput = defineSchemaModel({
94
+ name: "UpdateFlagInput",
95
+ description: "Input for updating a feature flag",
96
+ fields: {
97
+ flagId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
98
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
99
+ description: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
100
+ defaultValue: { type: ScalarTypeEnum.Boolean(), isOptional: true },
101
+ variants: { type: ScalarTypeEnum.JSON(), isOptional: true },
102
+ tags: { type: ScalarTypeEnum.JSON(), isOptional: true }
103
+ }
104
+ });
105
+ var DeleteFlagInput = defineSchemaModel({
106
+ name: "DeleteFlagInput",
107
+ description: "Input for deleting a feature flag",
108
+ fields: {
109
+ flagId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false }
110
+ }
111
+ });
112
+ var ToggleFlagInput = defineSchemaModel({
113
+ name: "ToggleFlagInput",
114
+ description: "Input for toggling a feature flag",
115
+ fields: {
116
+ flagId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
117
+ status: { type: ScalarTypeEnum.String_unsecure(), isOptional: false }
118
+ }
119
+ });
120
+ var GetFlagInput = defineSchemaModel({
121
+ name: "GetFlagInput",
122
+ description: "Input for getting a feature flag",
123
+ fields: {
124
+ key: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
125
+ orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true }
126
+ }
127
+ });
128
+ var ListFlagsInput = defineSchemaModel({
129
+ name: "ListFlagsInput",
130
+ description: "Input for listing feature flags",
131
+ fields: {
132
+ orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
133
+ status: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
134
+ tags: { type: ScalarTypeEnum.JSON(), isOptional: true },
135
+ limit: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true },
136
+ offset: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true }
137
+ }
138
+ });
139
+ var ListFlagsOutput = defineSchemaModel({
140
+ name: "ListFlagsOutput",
141
+ description: "Output for listing feature flags",
142
+ fields: {
143
+ flags: { type: FeatureFlagModel, isArray: true, isOptional: false },
144
+ total: { type: ScalarTypeEnum.Int_unsecure(), isOptional: false }
145
+ }
146
+ });
147
+ var EvaluateFlagInput = defineSchemaModel({
148
+ name: "EvaluateFlagInput",
149
+ description: "Input for evaluating a feature flag",
150
+ fields: {
151
+ key: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
152
+ context: { type: ScalarTypeEnum.JSON(), isOptional: false }
153
+ }
154
+ });
155
+ var CreateRuleInput = defineSchemaModel({
156
+ name: "CreateRuleInput",
157
+ description: "Input for creating a targeting rule",
158
+ fields: {
159
+ flagId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
160
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
161
+ priority: { type: ScalarTypeEnum.Int_unsecure(), isOptional: true },
162
+ attribute: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
163
+ operator: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
164
+ value: { type: ScalarTypeEnum.JSON(), isOptional: false },
165
+ rolloutPercentage: {
166
+ type: ScalarTypeEnum.Int_unsecure(),
167
+ isOptional: true
168
+ },
169
+ serveValue: { type: ScalarTypeEnum.Boolean(), isOptional: true },
170
+ serveVariant: { type: ScalarTypeEnum.String_unsecure(), isOptional: true }
171
+ }
172
+ });
173
+ var DeleteRuleInput = defineSchemaModel({
174
+ name: "DeleteRuleInput",
175
+ description: "Input for deleting a targeting rule",
176
+ fields: {
177
+ ruleId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false }
178
+ }
179
+ });
180
+ var CreateExperimentInput = defineSchemaModel({
181
+ name: "CreateExperimentInput",
182
+ description: "Input for creating an experiment",
183
+ fields: {
184
+ key: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
185
+ name: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
186
+ description: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
187
+ hypothesis: { type: ScalarTypeEnum.String_unsecure(), isOptional: true },
188
+ flagId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
189
+ variants: { type: ScalarTypeEnum.JSON(), isOptional: false },
190
+ metrics: { type: ScalarTypeEnum.JSON(), isOptional: true },
191
+ audiencePercentage: {
192
+ type: ScalarTypeEnum.Int_unsecure(),
193
+ isOptional: true
194
+ },
195
+ scheduledStartAt: { type: ScalarTypeEnum.DateTime(), isOptional: true },
196
+ scheduledEndAt: { type: ScalarTypeEnum.DateTime(), isOptional: true },
197
+ orgId: { type: ScalarTypeEnum.String_unsecure(), isOptional: true }
198
+ }
199
+ });
200
+ var StartExperimentInput = defineSchemaModel({
201
+ name: "StartExperimentInput",
202
+ description: "Input for starting an experiment",
203
+ fields: {
204
+ experimentId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false }
205
+ }
206
+ });
207
+ var StopExperimentInput = defineSchemaModel({
208
+ name: "StopExperimentInput",
209
+ description: "Input for stopping an experiment",
210
+ fields: {
211
+ experimentId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
212
+ reason: { type: ScalarTypeEnum.String_unsecure(), isOptional: false },
213
+ winningVariant: {
214
+ type: ScalarTypeEnum.String_unsecure(),
215
+ isOptional: true
216
+ }
217
+ }
218
+ });
219
+ var GetExperimentInput = defineSchemaModel({
220
+ name: "GetExperimentInput",
221
+ description: "Input for getting an experiment",
222
+ fields: {
223
+ experimentId: { type: ScalarTypeEnum.String_unsecure(), isOptional: false }
224
+ }
225
+ });
226
+ var SuccessOutput = defineSchemaModel({
227
+ name: "SuccessOutput",
228
+ description: "Generic success output",
229
+ fields: {
230
+ success: { type: ScalarTypeEnum.Boolean(), isOptional: false }
231
+ }
232
+ });
233
+ var CreateFlagContract = defineCommand({
234
+ meta: {
235
+ key: "flag.create",
236
+ version: "1.0.0",
237
+ stability: "stable",
238
+ owners: [...OWNERS],
239
+ tags: ["feature-flags", "create"],
240
+ description: "Create a new feature flag.",
241
+ goal: "Define a new feature flag for toggling features.",
242
+ context: "Called when setting up a new feature flag."
243
+ },
244
+ io: {
245
+ input: CreateFlagInput,
246
+ output: FeatureFlagModel,
247
+ errors: {
248
+ KEY_ALREADY_EXISTS: {
249
+ description: "Flag key already exists",
250
+ http: 409,
251
+ gqlCode: "FLAG_KEY_EXISTS",
252
+ when: "A flag with this key already exists"
253
+ }
254
+ }
255
+ },
256
+ policy: {
257
+ auth: "admin"
258
+ }
259
+ });
260
+ var UpdateFlagContract = defineCommand({
261
+ meta: {
262
+ key: "flag.update",
263
+ version: "1.0.0",
264
+ stability: "stable",
265
+ owners: [...OWNERS],
266
+ tags: ["feature-flags", "update"],
267
+ description: "Update an existing feature flag.",
268
+ goal: "Modify flag configuration.",
269
+ context: "Called when adjusting flag settings."
270
+ },
271
+ io: {
272
+ input: UpdateFlagInput,
273
+ output: FeatureFlagModel,
274
+ errors: {
275
+ FLAG_NOT_FOUND: {
276
+ description: "Flag does not exist",
277
+ http: 404,
278
+ gqlCode: "FLAG_NOT_FOUND",
279
+ when: "Flag ID is invalid"
280
+ }
281
+ }
282
+ },
283
+ policy: {
284
+ auth: "admin"
285
+ }
286
+ });
287
+ var DeleteFlagContract = defineCommand({
288
+ meta: {
289
+ key: "flag.delete",
290
+ version: "1.0.0",
291
+ stability: "stable",
292
+ owners: [...OWNERS],
293
+ tags: ["feature-flags", "delete"],
294
+ description: "Delete a feature flag.",
295
+ goal: "Remove a feature flag and all its rules.",
296
+ context: "Called when a flag is no longer needed."
297
+ },
298
+ io: {
299
+ input: DeleteFlagInput,
300
+ output: SuccessOutput,
301
+ errors: {
302
+ FLAG_NOT_FOUND: {
303
+ description: "Flag does not exist",
304
+ http: 404,
305
+ gqlCode: "FLAG_NOT_FOUND",
306
+ when: "Flag ID is invalid"
307
+ },
308
+ FLAG_HAS_ACTIVE_EXPERIMENT: {
309
+ description: "Flag has an active experiment",
310
+ http: 409,
311
+ gqlCode: "FLAG_HAS_ACTIVE_EXPERIMENT",
312
+ when: "Cannot delete flag with running experiment"
313
+ }
314
+ }
315
+ },
316
+ policy: {
317
+ auth: "admin"
318
+ }
319
+ });
320
+ var ToggleFlagContract = defineCommand({
321
+ meta: {
322
+ key: "flag.toggle",
323
+ version: "1.0.0",
324
+ stability: "stable",
325
+ owners: [...OWNERS],
326
+ tags: ["feature-flags", "toggle"],
327
+ description: "Toggle a feature flag status.",
328
+ goal: "Quickly enable or disable a feature.",
329
+ context: "Called when turning a feature on or off."
330
+ },
331
+ io: {
332
+ input: ToggleFlagInput,
333
+ output: FeatureFlagModel,
334
+ errors: {
335
+ FLAG_NOT_FOUND: {
336
+ description: "Flag does not exist",
337
+ http: 404,
338
+ gqlCode: "FLAG_NOT_FOUND",
339
+ when: "Flag ID is invalid"
340
+ },
341
+ INVALID_STATUS: {
342
+ description: "Invalid status value",
343
+ http: 400,
344
+ gqlCode: "INVALID_STATUS",
345
+ when: "Status must be OFF, ON, or GRADUAL"
346
+ }
347
+ }
348
+ },
349
+ policy: {
350
+ auth: "admin"
351
+ }
352
+ });
353
+ var GetFlagContract = defineQuery({
354
+ meta: {
355
+ key: "flag.get",
356
+ version: "1.0.0",
357
+ stability: "stable",
358
+ owners: [...OWNERS],
359
+ tags: ["feature-flags", "get"],
360
+ description: "Get a feature flag by key.",
361
+ goal: "Retrieve flag configuration.",
362
+ context: "Called to inspect flag details."
363
+ },
364
+ io: {
365
+ input: GetFlagInput,
366
+ output: FeatureFlagModel,
367
+ errors: {
368
+ FLAG_NOT_FOUND: {
369
+ description: "Flag does not exist",
370
+ http: 404,
371
+ gqlCode: "FLAG_NOT_FOUND",
372
+ when: "Flag key is invalid"
373
+ }
374
+ }
375
+ },
376
+ policy: {
377
+ auth: "user"
378
+ }
379
+ });
380
+ var ListFlagsContract = defineQuery({
381
+ meta: {
382
+ key: "flag.list",
383
+ version: "1.0.0",
384
+ stability: "stable",
385
+ owners: [...OWNERS],
386
+ tags: ["feature-flags", "list"],
387
+ description: "List all feature flags.",
388
+ goal: "View all configured flags.",
389
+ context: "Admin dashboard."
390
+ },
391
+ io: {
392
+ input: ListFlagsInput,
393
+ output: ListFlagsOutput
394
+ },
395
+ policy: {
396
+ auth: "admin"
397
+ }
398
+ });
399
+ var EvaluateFlagContract = defineQuery({
400
+ meta: {
401
+ key: "flag.evaluate",
402
+ version: "1.0.0",
403
+ stability: "stable",
404
+ owners: [...OWNERS],
405
+ tags: ["feature-flags", "evaluate"],
406
+ description: "Evaluate a feature flag for a given context.",
407
+ goal: "Determine if a feature should be enabled.",
408
+ context: "Called at runtime to check feature availability."
409
+ },
410
+ io: {
411
+ input: EvaluateFlagInput,
412
+ output: EvaluationResultModel,
413
+ errors: {
414
+ FLAG_NOT_FOUND: {
415
+ description: "Flag does not exist",
416
+ http: 404,
417
+ gqlCode: "FLAG_NOT_FOUND",
418
+ when: "Flag key is invalid"
419
+ }
420
+ }
421
+ },
422
+ policy: {
423
+ auth: "anonymous"
424
+ }
425
+ });
426
+ var CreateRuleContract = defineCommand({
427
+ meta: {
428
+ key: "flag.rule.create",
429
+ version: "1.0.0",
430
+ stability: "stable",
431
+ owners: [...OWNERS],
432
+ tags: ["feature-flags", "rule", "create"],
433
+ description: "Create a targeting rule for a flag.",
434
+ goal: "Add conditional targeting to a flag.",
435
+ context: "Called when setting up targeting."
436
+ },
437
+ io: {
438
+ input: CreateRuleInput,
439
+ output: TargetingRuleModel,
440
+ errors: {
441
+ FLAG_NOT_FOUND: {
442
+ description: "Flag does not exist",
443
+ http: 404,
444
+ gqlCode: "FLAG_NOT_FOUND",
445
+ when: "Flag ID is invalid"
446
+ },
447
+ INVALID_OPERATOR: {
448
+ description: "Invalid operator",
449
+ http: 400,
450
+ gqlCode: "INVALID_OPERATOR",
451
+ when: "Operator is not supported"
452
+ }
453
+ }
454
+ },
455
+ policy: {
456
+ auth: "admin"
457
+ }
458
+ });
459
+ var DeleteRuleContract = defineCommand({
460
+ meta: {
461
+ key: "flag.rule.delete",
462
+ version: "1.0.0",
463
+ stability: "stable",
464
+ owners: [...OWNERS],
465
+ tags: ["feature-flags", "rule", "delete"],
466
+ description: "Delete a targeting rule.",
467
+ goal: "Remove a targeting rule from a flag.",
468
+ context: "Called when removing targeting conditions."
469
+ },
470
+ io: {
471
+ input: DeleteRuleInput,
472
+ output: SuccessOutput,
473
+ errors: {
474
+ RULE_NOT_FOUND: {
475
+ description: "Rule does not exist",
476
+ http: 404,
477
+ gqlCode: "RULE_NOT_FOUND",
478
+ when: "Rule ID is invalid"
479
+ }
480
+ }
481
+ },
482
+ policy: {
483
+ auth: "admin"
484
+ }
485
+ });
486
+ var CreateExperimentContract = defineCommand({
487
+ meta: {
488
+ key: "experiment.create",
489
+ version: "1.0.0",
490
+ stability: "stable",
491
+ owners: [...OWNERS],
492
+ tags: ["feature-flags", "experiment", "create"],
493
+ description: "Create an A/B test experiment.",
494
+ goal: "Set up an experiment with variants.",
495
+ context: "Called when setting up A/B testing."
496
+ },
497
+ io: {
498
+ input: CreateExperimentInput,
499
+ output: ExperimentModel,
500
+ errors: {
501
+ FLAG_NOT_FOUND: {
502
+ description: "Flag does not exist",
503
+ http: 404,
504
+ gqlCode: "FLAG_NOT_FOUND",
505
+ when: "Flag ID is invalid"
506
+ },
507
+ EXPERIMENT_KEY_EXISTS: {
508
+ description: "Experiment key already exists",
509
+ http: 409,
510
+ gqlCode: "EXPERIMENT_KEY_EXISTS",
511
+ when: "An experiment with this key already exists"
512
+ },
513
+ INVALID_VARIANTS: {
514
+ description: "Invalid variant configuration",
515
+ http: 400,
516
+ gqlCode: "INVALID_VARIANTS",
517
+ when: "Variant percentages must sum to 100"
518
+ }
519
+ }
520
+ },
521
+ policy: {
522
+ auth: "admin"
523
+ }
524
+ });
525
+ var StartExperimentContract = defineCommand({
526
+ meta: {
527
+ key: "experiment.start",
528
+ version: "1.0.0",
529
+ stability: "stable",
530
+ owners: [...OWNERS],
531
+ tags: ["feature-flags", "experiment", "start"],
532
+ description: "Start an experiment.",
533
+ goal: "Begin collecting data for an experiment.",
534
+ context: "Called when ready to run an A/B test."
535
+ },
536
+ io: {
537
+ input: StartExperimentInput,
538
+ output: ExperimentModel,
539
+ errors: {
540
+ EXPERIMENT_NOT_FOUND: {
541
+ description: "Experiment does not exist",
542
+ http: 404,
543
+ gqlCode: "EXPERIMENT_NOT_FOUND",
544
+ when: "Experiment ID is invalid"
545
+ },
546
+ EXPERIMENT_ALREADY_RUNNING: {
547
+ description: "Experiment is already running",
548
+ http: 409,
549
+ gqlCode: "EXPERIMENT_ALREADY_RUNNING",
550
+ when: "Cannot start an experiment that is already running"
551
+ }
552
+ }
553
+ },
554
+ policy: {
555
+ auth: "admin"
556
+ }
557
+ });
558
+ var StopExperimentContract = defineCommand({
559
+ meta: {
560
+ key: "experiment.stop",
561
+ version: "1.0.0",
562
+ stability: "stable",
563
+ owners: [...OWNERS],
564
+ tags: ["feature-flags", "experiment", "stop"],
565
+ description: "Stop an experiment.",
566
+ goal: "End an experiment and optionally declare a winner.",
567
+ context: "Called when concluding an A/B test."
568
+ },
569
+ io: {
570
+ input: StopExperimentInput,
571
+ output: ExperimentModel,
572
+ errors: {
573
+ EXPERIMENT_NOT_FOUND: {
574
+ description: "Experiment does not exist",
575
+ http: 404,
576
+ gqlCode: "EXPERIMENT_NOT_FOUND",
577
+ when: "Experiment ID is invalid"
578
+ },
579
+ EXPERIMENT_NOT_RUNNING: {
580
+ description: "Experiment is not running",
581
+ http: 409,
582
+ gqlCode: "EXPERIMENT_NOT_RUNNING",
583
+ when: "Cannot stop an experiment that is not running"
584
+ }
585
+ }
586
+ },
587
+ policy: {
588
+ auth: "admin"
589
+ }
590
+ });
591
+ var GetExperimentContract = defineQuery({
592
+ meta: {
593
+ key: "experiment.get",
594
+ version: "1.0.0",
595
+ stability: "stable",
596
+ owners: [...OWNERS],
597
+ tags: ["feature-flags", "experiment", "get"],
598
+ description: "Get experiment details.",
599
+ goal: "View experiment configuration and results.",
600
+ context: "Called to inspect experiment status."
601
+ },
602
+ io: {
603
+ input: GetExperimentInput,
604
+ output: ExperimentModel,
605
+ errors: {
606
+ EXPERIMENT_NOT_FOUND: {
607
+ description: "Experiment does not exist",
608
+ http: 404,
609
+ gqlCode: "EXPERIMENT_NOT_FOUND",
610
+ when: "Experiment ID is invalid"
611
+ }
612
+ }
613
+ },
614
+ policy: {
615
+ auth: "user"
616
+ }
617
+ });
618
+ export {
619
+ UpdateFlagContract,
620
+ ToggleFlagContract,
621
+ TargetingRuleModel,
622
+ StopExperimentContract,
623
+ StartExperimentContract,
624
+ ListFlagsContract,
625
+ GetFlagContract,
626
+ GetExperimentContract,
627
+ FeatureFlagModel,
628
+ ExperimentModel,
629
+ EvaluationResultModel,
630
+ EvaluateFlagContract,
631
+ DeleteRuleContract,
632
+ DeleteFlagContract,
633
+ CreateRuleContract,
634
+ CreateFlagContract,
635
+ CreateExperimentContract
636
+ };
@@ -0,0 +1,71 @@
1
+ // src/docs/feature-flags.docblock.ts
2
+ import { registerDocBlocks } from "@contractspec/lib.contracts/docs";
3
+ var featureFlagsDocBlocks = [
4
+ {
5
+ id: "docs.feature-flags.overview",
6
+ title: "Feature Flags & Experiments",
7
+ summary: "Reusable, spec-first feature flag and experiment module with targeting, gradual rollout, multivariate variants, and evaluation logging.",
8
+ kind: "reference",
9
+ visibility: "public",
10
+ route: "/docs/feature-flags/overview",
11
+ tags: ["feature-flags", "experiments", "progressive-delivery"],
12
+ body: `## What this module provides
13
+
14
+ - **Entities**: FeatureFlag, FlagTargetingRule, Experiment, ExperimentAssignment, FlagEvaluation.
15
+ - **Contracts**: create/update/delete/toggle/list/get flags; create/delete rules; evaluate flags; create/start/stop/get experiments.
16
+ - **Events**: flag.created/updated/deleted/toggled, rule.created/deleted, experiment.created/started/stopped, flag.evaluated, experiment.variant_assigned.
17
+ - **Evaluation Engine**: Deterministic evaluator with gradual rollout, rule priority, audience filters, and experiment bucketing.
18
+
19
+ ## How to use
20
+
21
+ 1) Compose schema
22
+ - Add \`featureFlagsSchemaContribution\` to your module composition.
23
+
24
+ 2) Register contracts/events
25
+ - Import exports from \`@contractspec/lib.feature-flags\` into your spec registry.
26
+
27
+ 3) Evaluate at runtime
28
+ - Instantiate \`FlagEvaluator\` with a repository implementation and optional logger.
29
+ - Evaluate with context attributes (userId, orgId, plan, segment, sessionId, attributes).
30
+
31
+ 4) Wire observability
32
+ - Emit audit trail on config changes; emit \`flag.evaluated\` for analytics.
33
+
34
+ ## Usage example
35
+
36
+ ${"```"}ts
37
+ import {
38
+ FlagEvaluator,
39
+ InMemoryFlagRepository,
40
+ } from '@contractspec/lib.feature-flags';
41
+
42
+ const repo = new InMemoryFlagRepository();
43
+ repo.addFlag({
44
+ id: 'flag-1',
45
+ key: 'new_dashboard',
46
+ status: 'GRADUAL',
47
+ defaultValue: false,
48
+ });
49
+
50
+ const evaluator = new FlagEvaluator({ repository: repo });
51
+ const result = await evaluator.evaluate('new_dashboard', {
52
+ userId: 'user-123',
53
+ orgId: 'org-456',
54
+ plan: 'pro',
55
+ });
56
+
57
+ if (result.enabled) {
58
+ // serve the new dashboard
59
+ }
60
+ ${"```"},
61
+
62
+ ## Guardrails
63
+
64
+ - Keep flag keys stable and human-readable; avoid PII in context.
65
+ - Ensure experiments’ variant percentages sum to 100; default flag status to OFF.
66
+ - Use org-scoped flags for multi-tenant isolation.
67
+ - Log evaluations only when needed to control volume; prefer sampling for noisy paths.
68
+ `
69
+ }
70
+ ];
71
+ registerDocBlocks(featureFlagsDocBlocks);