@wellth-app/experiments 0.0.0-stage → 0.1.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 (66) hide show
  1. package/README.md +223 -2
  2. package/dist/cjs/index.js +10 -0
  3. package/dist/cjs/index.js.map +1 -0
  4. package/dist/cjs/react.js +317 -0
  5. package/dist/cjs/react.js.map +1 -0
  6. package/dist/cjs/registry-pg5aaOm-.js +537 -0
  7. package/dist/cjs/registry-pg5aaOm-.js.map +1 -0
  8. package/dist/cjs/web.js +87 -0
  9. package/dist/cjs/web.js.map +1 -0
  10. package/dist/esm/index.js +3 -0
  11. package/dist/esm/index.js.map +1 -0
  12. package/dist/esm/react.js +290 -0
  13. package/dist/esm/react.js.map +1 -0
  14. package/dist/esm/registry-B73fwn9J.js +534 -0
  15. package/dist/esm/registry-B73fwn9J.js.map +1 -0
  16. package/dist/esm/web.js +85 -0
  17. package/dist/esm/web.js.map +1 -0
  18. package/dist/events.d.ts +65 -0
  19. package/dist/events.d.ts.map +1 -0
  20. package/dist/index.d.ts +10 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/react/context.d.ts +37 -0
  23. package/dist/react/context.d.ts.map +1 -0
  24. package/dist/react/guard.d.ts +11 -0
  25. package/dist/react/guard.d.ts.map +1 -0
  26. package/dist/react/index.d.ts +12 -0
  27. package/dist/react/index.d.ts.map +1 -0
  28. package/dist/react/provider.d.ts +38 -0
  29. package/dist/react/provider.d.ts.map +1 -0
  30. package/dist/react/use-analytics.d.ts +12 -0
  31. package/dist/react/use-analytics.d.ts.map +1 -0
  32. package/dist/react/use-experiment-qa.d.ts +14 -0
  33. package/dist/react/use-experiment-qa.d.ts.map +1 -0
  34. package/dist/react/use-experiment.d.ts +30 -0
  35. package/dist/react/use-experiment.d.ts.map +1 -0
  36. package/dist/react/use-feature-flag.d.ts +16 -0
  37. package/dist/react/use-feature-flag.d.ts.map +1 -0
  38. package/dist/react/use-feature-value.d.ts +16 -0
  39. package/dist/react/use-feature-value.d.ts.map +1 -0
  40. package/dist/registry.d.ts +143 -0
  41. package/dist/registry.d.ts.map +1 -0
  42. package/dist/types.d.ts +111 -0
  43. package/dist/types.d.ts.map +1 -0
  44. package/dist/uuid.d.ts +6 -0
  45. package/dist/uuid.d.ts.map +1 -0
  46. package/dist/web/batched-event-sender.d.ts +35 -0
  47. package/dist/web/batched-event-sender.d.ts.map +1 -0
  48. package/dist/web/index.d.ts +7 -0
  49. package/dist/web/index.d.ts.map +1 -0
  50. package/package.json +127 -4
  51. package/src/events.ts +90 -0
  52. package/src/index.ts +37 -0
  53. package/src/react/context.ts +62 -0
  54. package/src/react/guard.ts +25 -0
  55. package/src/react/index.ts +17 -0
  56. package/src/react/provider.tsx +109 -0
  57. package/src/react/use-analytics.ts +17 -0
  58. package/src/react/use-experiment-qa.ts +26 -0
  59. package/src/react/use-experiment.ts +72 -0
  60. package/src/react/use-feature-flag.ts +35 -0
  61. package/src/react/use-feature-value.ts +42 -0
  62. package/src/registry.ts +715 -0
  63. package/src/types.ts +120 -0
  64. package/src/uuid.ts +14 -0
  65. package/src/web/batched-event-sender.ts +111 -0
  66. package/src/web/index.ts +9 -0
@@ -0,0 +1,26 @@
1
+ import { useSyncExternalStore } from "react";
2
+ import type { QaPin } from "../types";
3
+ import { useExperiments } from "./context";
4
+
5
+ /**
6
+ * The QA override surface, for the dev/debug menu: force a variant for the
7
+ * current subject and session. Pinned assignments render the pinned arm but are
8
+ * never recorded as exposure.
9
+ */
10
+ export function useExperimentQa(): {
11
+ pins: readonly QaPin[];
12
+ pin: (pin: QaPin) => void;
13
+ unpin: (input: { experimentKey: string }) => void;
14
+ } {
15
+ const { registry } = useExperiments();
16
+ useSyncExternalStore(registry.subscribe, registry.getVersion);
17
+ return {
18
+ pins: registry.getPins(),
19
+ pin: (pin: QaPin): void => {
20
+ registry.pin(pin);
21
+ },
22
+ unpin: (input: { experimentKey: string }): void => {
23
+ registry.unpin(input);
24
+ },
25
+ };
26
+ }
@@ -0,0 +1,72 @@
1
+ import { useEffect, useSyncExternalStore } from "react";
2
+ import type { ExperimentAssignment, ExperimentContext } from "../types";
3
+ import { useExperiments, useInternalRegistry } from "./context";
4
+ import { assertHookKeyKind } from "./guard";
5
+
6
+ /**
7
+ * Read the experiment's assignment for the current subject:
8
+ *
9
+ * ```ts
10
+ * const { variant, params } = useExperiment("brick_wall");
11
+ * // variant: the assigned arm key from the experiment config
12
+ * // params: the configured parameter payload for the assigned arm
13
+ * ```
14
+ *
15
+ * Pass `context` to evaluate for a different subject or with extra targeting
16
+ * attributes; both default to the session's. Targeting gates eligibility before
17
+ * bucketing — a member who fails targeting gets the default arm with no
18
+ * exposure. The hash input is always the subject id.
19
+ *
20
+ * Exactly one exposure is recorded, when the assigned variant is actually
21
+ * rendered (post-commit) — never on compute, and never for a member who was
22
+ * bucketed but not shown the experience. Defaults, stopped experiments,
23
+ * targeting failures, and QA pins never emit exposure.
24
+ *
25
+ * With `exposure: "manual"` the hook instead returns a `markExposed` trigger —
26
+ * for call sites that evaluate now but render the experience later. Call it
27
+ * when the experience actually renders; it no-ops on an unexposed assignment.
28
+ *
29
+ * Misuse guard: calling this on a feature-flag key throws a clear error in
30
+ * development (flags go through useFeatureFlag/useFeatureValue); production
31
+ * reads fail closed to control.
32
+ */
33
+ export function useExperiment(
34
+ experimentKey: string,
35
+ context?: ExperimentContext,
36
+ ): ExperimentAssignment {
37
+ const { subjectId: sessionSubjectId, attributes: sessionAttributes } =
38
+ useExperiments();
39
+ // The real registry, trackExposure included — the internal channel, so the
40
+ // context's facade can never emit an exposure outside this hook.
41
+ const registry = useInternalRegistry();
42
+ // Dev-only misuse guard: a flag key here throws; production fails closed.
43
+ assertHookKeyKind(registry, experimentKey, "experiment", "useExperiment");
44
+ // Subscribed so a config refresh or pin change re-evaluates this assignment.
45
+ useSyncExternalStore(registry.subscribe, registry.getVersion);
46
+ const subjectId = context?.subjectId ?? sessionSubjectId;
47
+ const attributes = context?.attributes ?? sessionAttributes;
48
+ const assignment = registry.evaluateExperiment({
49
+ experimentKey,
50
+ subjectId,
51
+ attributes,
52
+ });
53
+
54
+ const exposure = assignment.exposure;
55
+ const manual = context?.exposure === "manual";
56
+ useEffect(() => {
57
+ if (manual || exposure === null) return;
58
+ registry.trackExposure(exposure);
59
+ }, [registry, exposure, manual]);
60
+
61
+ if (!manual) {
62
+ return { variant: assignment.variant, params: assignment.params };
63
+ }
64
+ return {
65
+ variant: assignment.variant,
66
+ params: assignment.params,
67
+ markExposed: () => {
68
+ if (exposure === null) return;
69
+ registry.trackExposure(exposure);
70
+ },
71
+ };
72
+ }
@@ -0,0 +1,35 @@
1
+ import { useSyncExternalStore } from "react";
2
+ import type { ExperimentContext } from "../types";
3
+ import { useExperiments } from "./context";
4
+ import { assertHookKeyKind } from "./guard";
5
+
6
+ /**
7
+ * Read a boolean feature flag for the current subject:
8
+ *
9
+ * ```ts
10
+ * const enabled = useFeatureFlag("dark_mode_checkout");
11
+ * ```
12
+ *
13
+ * Targeting conditions and percentage rollouts evaluate locally against the
14
+ * same deterministic hash the experiments use. Flags never log exposure. An
15
+ * unknown flag is off; an experiment key read here throws a clear error in
16
+ * development — experiments go through useExperiment, the exposure-aware path
17
+ * (production reads fail closed to the config's default).
18
+ */
19
+ export function useFeatureFlag(
20
+ featureKey: string,
21
+ context?: ExperimentContext,
22
+ ): boolean {
23
+ const {
24
+ registry,
25
+ subjectId: sessionSubjectId,
26
+ attributes: sessionAttributes,
27
+ } = useExperiments();
28
+ // Dev-only misuse guard: an experiment key here throws; production reads the config's default.
29
+ assertHookKeyKind(registry, featureKey, "flag", "useFeatureFlag");
30
+ // Subscribed so a config refresh re-evaluates this flag.
31
+ useSyncExternalStore(registry.subscribe, registry.getVersion);
32
+ const subjectId = context?.subjectId ?? sessionSubjectId;
33
+ const attributes = context?.attributes ?? sessionAttributes;
34
+ return registry.evaluateFlag({ featureKey, subjectId, attributes }).on;
35
+ }
@@ -0,0 +1,42 @@
1
+ import { useSyncExternalStore } from "react";
2
+ import type { ExperimentContext } from "../types";
3
+ import { useExperiments } from "./context";
4
+ import { assertHookKeyKind } from "./guard";
5
+
6
+ /**
7
+ * Read a valued feature flag (string/number/JSON remote config) for the current
8
+ * subject, falling back when the flag is unknown or has no value:
9
+ *
10
+ * ```ts
11
+ * const headline = useFeatureValue("checkout_headline", "Control headline");
12
+ * ```
13
+ *
14
+ * The fallback declares the flag's type. Flags never log exposure; an
15
+ * experiment key read here throws a clear error in development — experiments
16
+ * go through useExperiment, the exposure-aware path (production reads fail
17
+ * closed to the config's default).
18
+ */
19
+ export function useFeatureValue<T>(
20
+ featureKey: string,
21
+ fallback: T,
22
+ context?: ExperimentContext,
23
+ ): T {
24
+ const {
25
+ registry,
26
+ subjectId: sessionSubjectId,
27
+ attributes: sessionAttributes,
28
+ } = useExperiments();
29
+ // Dev-only misuse guard: an experiment key here throws; production reads the config's default.
30
+ assertHookKeyKind(registry, featureKey, "flag", "useFeatureValue");
31
+ // Subscribed so a config refresh re-evaluates this flag.
32
+ useSyncExternalStore(registry.subscribe, registry.getVersion);
33
+ const subjectId = context?.subjectId ?? sessionSubjectId;
34
+ const attributes = context?.attributes ?? sessionAttributes;
35
+ const { value } = registry.evaluateFlag({
36
+ featureKey,
37
+ subjectId,
38
+ attributes,
39
+ });
40
+ // The engine's value is untyped; the caller's fallback declares the flag's type.
41
+ return value === null || value === undefined ? fallback : (value as T);
42
+ }