@voltro/plugin-flags 0.32.0 → 0.34.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.
@@ -0,0 +1,79 @@
1
+ import { Either as e, Schema as t } from "effect";
2
+ //#region src/define.ts
3
+ var n = (e) => typeof e == "object" && !!e && e._voltroTypedFlag === !0, r = (e) => e, i = (n) => {
4
+ let i = (e) => /* @__PURE__ */ Error(`defineFlag(${JSON.stringify(n.key)}): ${e}`);
5
+ if (typeof n.key != "string" || n.key.length === 0) throw Error("defineFlag: a non-empty `key` is required");
6
+ if (n.value === void 0) throw i("`value` (an Effect Schema) is required");
7
+ let a = n.variants ?? [], o = /* @__PURE__ */ new Set();
8
+ for (let e of a) {
9
+ if (typeof e != "object" || !e || typeof e.name != "string" || e.name.length === 0) throw i("each variant needs a non-empty `name`");
10
+ if (o.has(e.name)) throw i(`duplicate variant name ${JSON.stringify(e.name)}`);
11
+ if (o.add(e.name), e.weight !== void 0 && !(Number.isFinite(e.weight) && e.weight > 0)) throw i(`variant ${JSON.stringify(e.name)} weight must be a positive, finite number`);
12
+ }
13
+ if (n.offVariant !== void 0 && !o.has(n.offVariant)) throw i(`offVariant ${JSON.stringify(n.offVariant)} is not one of the declared variants`);
14
+ if (n.rollout !== void 0 && !(Number.isFinite(n.rollout) && n.rollout >= 0 && n.rollout <= 100)) throw i("rollout must be a number in 0..100");
15
+ if (n.experiment !== void 0 && (typeof n.experiment != "string" || n.experiment.length === 0)) throw i("experiment, when present, must be a non-empty experiment name");
16
+ let s = t.decodeUnknownEither(n.value, {
17
+ errors: "all",
18
+ onExcessProperty: "ignore"
19
+ }), c = (t) => {
20
+ let n = s(t);
21
+ return e.isRight(n) ? {
22
+ ok: !0,
23
+ value: n.right
24
+ } : {
25
+ ok: !1,
26
+ reason: n.left.message
27
+ };
28
+ }, l = c(n.default);
29
+ if (!l.ok) throw i(`\`default\` does not satisfy the value Schema: ${l.reason}`);
30
+ for (let e of a) {
31
+ let t = c(e.value);
32
+ if (!t.ok) throw i(`variant ${JSON.stringify(e.name)} value does not satisfy the value Schema: ${t.reason}`);
33
+ }
34
+ let u = {
35
+ ...n.enabled === void 0 ? {} : { enabled: n.enabled },
36
+ ...n.rollout === void 0 ? {} : { rollout: n.rollout },
37
+ ...n.targeting === void 0 ? {} : { targeting: n.targeting },
38
+ ...n.rolloutBy === void 0 ? {} : { rolloutBy: n.rolloutBy },
39
+ ...a.length > 0 ? { variants: a.map((e) => ({
40
+ name: e.name,
41
+ value: r(e.value),
42
+ ...e.weight === void 0 ? {} : { weight: e.weight }
43
+ })) } : {},
44
+ ...n.offVariant === void 0 ? {} : { offVariant: n.offVariant },
45
+ ...n.schedule === void 0 ? {} : { schedule: n.schedule },
46
+ ...n.description === void 0 ? {} : { description: n.description }
47
+ };
48
+ return {
49
+ _voltroTypedFlag: !0,
50
+ key: n.key,
51
+ valueType: String(n.value.ast),
52
+ defaultValue: n.default,
53
+ variants: a,
54
+ decode: c,
55
+ definition: u,
56
+ ...n.experiment === void 0 ? {} : { experiment: n.experiment }
57
+ };
58
+ }, a = (e, t) => {
59
+ if (t === void 0) return e.defaultValue;
60
+ let n = e.decode(t.value);
61
+ return n.ok ? n.value : e.defaultValue;
62
+ }, o = (e, t) => {
63
+ for (let n of t.variants ?? []) {
64
+ let t = e.decode(n.value);
65
+ if (!t.ok) return {
66
+ ok: !1,
67
+ refusal: {
68
+ key: e.key,
69
+ reason: `variant ${JSON.stringify(n.name)} does not satisfy the flag's declared value type (${e.valueType}): ${t.reason}`
70
+ }
71
+ };
72
+ }
73
+ return {
74
+ ok: !0,
75
+ definition: t
76
+ };
77
+ };
78
+ //#endregion
79
+ export { a as i, o as n, n as r, i as t };