axiom 0.29.0 → 0.30.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.
- package/dist/app-scope-Ca3IbW3a.d.cts +208 -0
- package/dist/app-scope-Ca3IbW3a.d.ts +208 -0
- package/dist/bin.cjs +420 -179
- package/dist/bin.cjs.map +1 -1
- package/dist/bin.js +48 -32
- package/dist/bin.js.map +1 -1
- package/dist/{chunk-KEXKKQVW.js → chunk-4VNFFUM5.js} +20 -2
- package/dist/chunk-4VNFFUM5.js.map +1 -0
- package/dist/{chunk-5TVCLHTM.js → chunk-CLH5OLB6.js} +7 -3
- package/dist/chunk-CLH5OLB6.js.map +1 -0
- package/dist/{chunk-BYO23LA2.js → chunk-EL3LTTAC.js} +974 -6
- package/dist/chunk-EL3LTTAC.js.map +1 -0
- package/dist/{chunk-ZLRPS5IN.js → chunk-GJ3422RE.js} +21 -2
- package/dist/chunk-GJ3422RE.js.map +1 -0
- package/dist/chunk-S26ZFCSC.js +1292 -0
- package/dist/chunk-S26ZFCSC.js.map +1 -0
- package/dist/chunk-S65FSMB3.js +51 -0
- package/dist/chunk-S65FSMB3.js.map +1 -0
- package/dist/{chunk-MM5FFQJT.js → chunk-X2LH7XLM.js} +6 -1
- package/dist/{chunk-MM5FFQJT.js.map → chunk-X2LH7XLM.js.map} +1 -1
- package/dist/{chunk-GPMG4NO7.js → chunk-ZELYIU3G.js} +7 -3
- package/dist/{chunk-GPMG4NO7.js.map → chunk-ZELYIU3G.js.map} +1 -1
- package/dist/config.cjs.map +1 -1
- package/dist/config.js +8 -3
- package/dist/config.js.map +1 -1
- package/dist/evals/custom-runner.cjs +1 -1
- package/dist/evals/custom-runner.cjs.map +1 -1
- package/dist/evals/custom-runner.js +7 -5
- package/dist/evals/custom-runner.js.map +1 -1
- package/dist/evals.cjs +1522 -2077
- package/dist/evals.cjs.map +1 -1
- package/dist/evals.d.cts +5 -155
- package/dist/evals.d.ts +5 -155
- package/dist/evals.js +95 -126
- package/dist/evals.js.map +1 -1
- package/dist/{handlebars-LMQRDB6O.js → handlebars-AIP3BZD5.js} +49 -3
- package/dist/{handlebars-LMQRDB6O.js.map → handlebars-AIP3BZD5.js.map} +1 -1
- package/dist/index.cjs +972 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +15 -4
- package/dist/index.js.map +1 -1
- package/dist/{token-5SBK3AVU.js → token-EZ57LVNC.js} +6 -4
- package/dist/{token-5SBK3AVU.js.map → token-EZ57LVNC.js.map} +1 -1
- package/dist/token-util-7FRSTCFN.js +6 -0
- package/package.json +1 -1
- package/dist/chunk-5TVCLHTM.js.map +0 -1
- package/dist/chunk-BYO23LA2.js.map +0 -1
- package/dist/chunk-NV3Y4T4G.js +0 -22
- package/dist/chunk-NV3Y4T4G.js.map +0 -1
- package/dist/chunk-YWPD74YG.js +0 -2112
- package/dist/chunk-YWPD74YG.js.map +0 -1
- package/dist/chunk-ZLRPS5IN.js.map +0 -1
- package/dist/name-validation.d-CDPeW_pV.d.cts +0 -81
- package/dist/name-validation.d-CDPeW_pV.d.ts +0 -81
- package/dist/token-util-KUW2O75G.js +0 -6
- package/dist/token-util-KUW2O75G.js.map +0 -1
- /package/dist/{chunk-KEXKKQVW.js.map → token-util-7FRSTCFN.js.map} +0 -0
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { ZodObject, ZodDefault, z } from 'zod';
|
|
2
|
+
import { $ZodObject } from 'zod/v4/core';
|
|
3
|
+
|
|
4
|
+
type ValidChars =
|
|
5
|
+
| 'a'
|
|
6
|
+
| 'b'
|
|
7
|
+
| 'c'
|
|
8
|
+
| 'd'
|
|
9
|
+
| 'e'
|
|
10
|
+
| 'f'
|
|
11
|
+
| 'g'
|
|
12
|
+
| 'h'
|
|
13
|
+
| 'i'
|
|
14
|
+
| 'j'
|
|
15
|
+
| 'k'
|
|
16
|
+
| 'l'
|
|
17
|
+
| 'm'
|
|
18
|
+
| 'n'
|
|
19
|
+
| 'o'
|
|
20
|
+
| 'p'
|
|
21
|
+
| 'q'
|
|
22
|
+
| 'r'
|
|
23
|
+
| 's'
|
|
24
|
+
| 't'
|
|
25
|
+
| 'u'
|
|
26
|
+
| 'v'
|
|
27
|
+
| 'w'
|
|
28
|
+
| 'x'
|
|
29
|
+
| 'y'
|
|
30
|
+
| 'z'
|
|
31
|
+
| 'A'
|
|
32
|
+
| 'B'
|
|
33
|
+
| 'C'
|
|
34
|
+
| 'D'
|
|
35
|
+
| 'E'
|
|
36
|
+
| 'F'
|
|
37
|
+
| 'G'
|
|
38
|
+
| 'H'
|
|
39
|
+
| 'I'
|
|
40
|
+
| 'J'
|
|
41
|
+
| 'K'
|
|
42
|
+
| 'L'
|
|
43
|
+
| 'M'
|
|
44
|
+
| 'N'
|
|
45
|
+
| 'O'
|
|
46
|
+
| 'P'
|
|
47
|
+
| 'Q'
|
|
48
|
+
| 'R'
|
|
49
|
+
| 'S'
|
|
50
|
+
| 'T'
|
|
51
|
+
| 'U'
|
|
52
|
+
| 'V'
|
|
53
|
+
| 'W'
|
|
54
|
+
| 'X'
|
|
55
|
+
| 'Y'
|
|
56
|
+
| 'Z'
|
|
57
|
+
| '0'
|
|
58
|
+
| '1'
|
|
59
|
+
| '2'
|
|
60
|
+
| '3'
|
|
61
|
+
| '4'
|
|
62
|
+
| '5'
|
|
63
|
+
| '6'
|
|
64
|
+
| '7'
|
|
65
|
+
| '8'
|
|
66
|
+
| '9'
|
|
67
|
+
| '-'
|
|
68
|
+
| '_';
|
|
69
|
+
|
|
70
|
+
type ValidateName<T extends string, Original extends string = T> =
|
|
71
|
+
// For widened strings, don't attempt validation – let them flow through unchanged
|
|
72
|
+
string extends T // string is not wider than T, ie T is string
|
|
73
|
+
? T
|
|
74
|
+
: T extends ''
|
|
75
|
+
? Original extends ''
|
|
76
|
+
? '❌ Name cannot be empty'
|
|
77
|
+
: Original
|
|
78
|
+
: T extends `${infer First}${infer Rest}`
|
|
79
|
+
? First extends ValidChars
|
|
80
|
+
? ValidateName<Rest, Original>
|
|
81
|
+
: `❌ Invalid character in "${Original}". Only A-Z, a-z, 0-9, -, _ allowed`
|
|
82
|
+
: never;
|
|
83
|
+
|
|
84
|
+
type DefaultMaxDepth = 8;
|
|
85
|
+
type HasDefaults<S> = S extends {
|
|
86
|
+
_zod: {
|
|
87
|
+
def: {
|
|
88
|
+
defaultValue: unknown;
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
} ? true : S extends $ZodObject<infer Shape> | ZodObject<infer Shape> ? {
|
|
92
|
+
[K in keyof Shape]: HasDefaults<Shape[K]>;
|
|
93
|
+
} extends Record<keyof Shape, true> ? true : false : false;
|
|
94
|
+
type UnwrapSchema<T> = T extends ZodDefault<infer U> ? U : T;
|
|
95
|
+
type AllFieldsHaveDefaults<Schema> = Schema extends {
|
|
96
|
+
_zod: {
|
|
97
|
+
def: {
|
|
98
|
+
defaultValue: unknown;
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
} ? true : HasDefaults<UnwrapSchema<Schema>>;
|
|
102
|
+
interface AppScopeConfig<FlagSchema extends ZodObject<any> | undefined = undefined, FactSchema extends ZodObject<any> | undefined = undefined> {
|
|
103
|
+
flagSchema: FlagSchema;
|
|
104
|
+
factSchema?: FactSchema;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Recursive type to extract all possible paths from an object type.
|
|
108
|
+
* Uses stack-based depth limiting for better performance.
|
|
109
|
+
*
|
|
110
|
+
* @template T - The object type to extract paths from
|
|
111
|
+
* @template Stack - Internal stack counter (do not set manually)
|
|
112
|
+
* @template MaxDepth - Maximum recursion depth (default: 8 for good balance)
|
|
113
|
+
*/
|
|
114
|
+
type ObjectPaths<T, Stack extends unknown[] = [], MaxDepth extends number = DefaultMaxDepth> = Stack['length'] extends MaxDepth ? never : T extends object ? {
|
|
115
|
+
[K in keyof T]-?: K extends string | number ? `${K}` | `${K}.${ObjectPaths<T[K], [1, ...Stack], MaxDepth>}` : never;
|
|
116
|
+
}[keyof T] : never;
|
|
117
|
+
type ObjectPathValue<T, P extends string> = P extends keyof T ? T[P] : P extends `${infer K}.${infer Rest}` ? K extends keyof T ? ObjectPathValue<T[K], Rest> : never : never;
|
|
118
|
+
/**
|
|
119
|
+
* Generate deep nested paths from flag schema.
|
|
120
|
+
*
|
|
121
|
+
* @template T - ZodObject to extract paths from
|
|
122
|
+
* @template MaxDepth - Maximum recursion depth (default: 8, override for deeper nesting)
|
|
123
|
+
* @example
|
|
124
|
+
* // Default 8-level depth
|
|
125
|
+
* type Paths = DotPaths<MySchema>
|
|
126
|
+
*
|
|
127
|
+
* // Custom depth for deeper nesting (impacts performance)
|
|
128
|
+
* type DeepPaths = DotPaths<MySchema, 12>
|
|
129
|
+
*/
|
|
130
|
+
type DotPaths<T extends ZodObject<any>, MaxDepth extends number = DefaultMaxDepth> = {
|
|
131
|
+
[NS in keyof T['shape']]: (string & NS) | {
|
|
132
|
+
[P in ObjectPaths<z.output<UnwrapSchema<T['shape'][NS]>>, [
|
|
133
|
+
], MaxDepth>]: `${string & NS}.${P}`;
|
|
134
|
+
}[ObjectPaths<z.output<UnwrapSchema<T['shape'][NS]>>, [], MaxDepth>];
|
|
135
|
+
}[keyof T['shape']];
|
|
136
|
+
type PathValue<T extends ZodObject<any>, P extends string> = P extends `${infer NS}.${infer Rest}` ? NS extends keyof T['shape'] ? ObjectPathValue<z.output<UnwrapSchema<T['shape'][NS]>>, Rest> : never : P extends keyof T['shape'] ? z.output<UnwrapSchema<T['shape'][P]>> : never;
|
|
137
|
+
type DotNotationFlagFunction<FS extends ZodObject<any> | undefined> = FS extends ZodObject<any> ? <P extends DotPaths<FS>>(path: P) => PathValue<FS, P> : never;
|
|
138
|
+
type FactFunction<SC extends ZodObject<any> | undefined> = SC extends ZodObject<any> ? <P extends DotPaths<SC> & string>(name: P, value: PathValue<SC, P>) => void : never;
|
|
139
|
+
type OverrideFlagsFunction<FS extends ZodObject<any> | undefined> = FS extends ZodObject<any> ? (partial: {
|
|
140
|
+
[K in DotPaths<FS>]?: PathValue<FS, K>;
|
|
141
|
+
}) => void : (partial: Record<string, any>) => void;
|
|
142
|
+
type WithFlagsFunction<FS extends ZodObject<any> | undefined> = FS extends ZodObject<any> ? <T>(overrides: {
|
|
143
|
+
[K in DotPaths<FS>]?: PathValue<FS, K>;
|
|
144
|
+
}, fn: () => T) => T : <T>(overrides: Record<string, any>, fn: () => T) => T;
|
|
145
|
+
type PickFlagsFunction<FS extends ZodObject<any> | undefined> = FS extends ZodObject<any> ? {
|
|
146
|
+
<K extends ReadonlyArray<DotPaths<FS> & string>>(...paths: K): K;
|
|
147
|
+
<K extends ReadonlyArray<DotPaths<FS> & string>>(paths: K): K;
|
|
148
|
+
} : never;
|
|
149
|
+
interface AppScope<FS extends ZodObject<any> | undefined, SC extends ZodObject<any> | undefined> {
|
|
150
|
+
flag: DotNotationFlagFunction<FS>;
|
|
151
|
+
fact: FactFunction<SC>;
|
|
152
|
+
overrideFlags: OverrideFlagsFunction<FS>;
|
|
153
|
+
withFlags: WithFlagsFunction<FS>;
|
|
154
|
+
pickFlags: PickFlagsFunction<FS>;
|
|
155
|
+
getAllDefaultFlags: () => Record<string, any>;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Create a new application-level evaluation scope.
|
|
159
|
+
*
|
|
160
|
+
* @param config.flagSchema A zod object describing the schema for flags **(required)**
|
|
161
|
+
* @param config.factSchema A zod object describing the schema for facts (optional)
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* import { z } from 'zod';
|
|
165
|
+
*
|
|
166
|
+
* const { flag, fact, withFlags, pickFlags, overrideFlags } = createAppScope({
|
|
167
|
+
* flagSchema: z.object({
|
|
168
|
+
* ui: z.object({
|
|
169
|
+
* darkMode: z.boolean().default(false),
|
|
170
|
+
* theme: z.object({
|
|
171
|
+
* primary: z.string().default('#00f'),
|
|
172
|
+
* }),
|
|
173
|
+
* }),
|
|
174
|
+
* api: z.object({
|
|
175
|
+
endpoint: z.string().default('/api')
|
|
176
|
+
}),
|
|
177
|
+
* }),
|
|
178
|
+
* factSchema: z.object({
|
|
179
|
+
* userAction: z.string(),
|
|
180
|
+
* timing: z.number(),
|
|
181
|
+
* }),
|
|
182
|
+
* });
|
|
183
|
+
*
|
|
184
|
+
* // Typed flag access
|
|
185
|
+
* const dark = flag('ui.darkMode'); // inferred boolean
|
|
186
|
+
* const theme = flag('ui.theme'); // entire object
|
|
187
|
+
* const primary = flag('ui.theme.primary'); // '#00f'
|
|
188
|
+
* const endpoint = flag('api.endpoint'); // uses schema default
|
|
189
|
+
*
|
|
190
|
+
* // Typed fact recording
|
|
191
|
+
* fact('userAction', 'clicked_button');
|
|
192
|
+
* fact('timing', 1250);
|
|
193
|
+
*
|
|
194
|
+
* // Temporarily override flags for a block of code
|
|
195
|
+
* withFlags({ 'ui.darkMode': true }, () => {
|
|
196
|
+
* // code here, `ui.darkMode` will be true in this block and reset after
|
|
197
|
+
* });
|
|
198
|
+
*
|
|
199
|
+
* // Override flags globally for the current evaluation run
|
|
200
|
+
* overrideFlags({ 'api.endpoint': '/custom' });
|
|
201
|
+
*/
|
|
202
|
+
declare function createAppScope<FlagSchema extends ZodObject<any>, FactSchema extends ZodObject<any> | undefined = undefined>(config: AllFieldsHaveDefaults<FlagSchema> extends true ? AppScopeConfig<FlagSchema, FactSchema> : {
|
|
203
|
+
flagSchema: FlagSchema;
|
|
204
|
+
factSchema?: FactSchema;
|
|
205
|
+
__error__: 'createAppScope: flagSchema must have .default() for all leaf fields';
|
|
206
|
+
}): AppScope<FlagSchema, FactSchema>;
|
|
207
|
+
|
|
208
|
+
export { type ValidateName as V, createAppScope as c };
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { ZodObject, ZodDefault, z } from 'zod';
|
|
2
|
+
import { $ZodObject } from 'zod/v4/core';
|
|
3
|
+
|
|
4
|
+
type ValidChars =
|
|
5
|
+
| 'a'
|
|
6
|
+
| 'b'
|
|
7
|
+
| 'c'
|
|
8
|
+
| 'd'
|
|
9
|
+
| 'e'
|
|
10
|
+
| 'f'
|
|
11
|
+
| 'g'
|
|
12
|
+
| 'h'
|
|
13
|
+
| 'i'
|
|
14
|
+
| 'j'
|
|
15
|
+
| 'k'
|
|
16
|
+
| 'l'
|
|
17
|
+
| 'm'
|
|
18
|
+
| 'n'
|
|
19
|
+
| 'o'
|
|
20
|
+
| 'p'
|
|
21
|
+
| 'q'
|
|
22
|
+
| 'r'
|
|
23
|
+
| 's'
|
|
24
|
+
| 't'
|
|
25
|
+
| 'u'
|
|
26
|
+
| 'v'
|
|
27
|
+
| 'w'
|
|
28
|
+
| 'x'
|
|
29
|
+
| 'y'
|
|
30
|
+
| 'z'
|
|
31
|
+
| 'A'
|
|
32
|
+
| 'B'
|
|
33
|
+
| 'C'
|
|
34
|
+
| 'D'
|
|
35
|
+
| 'E'
|
|
36
|
+
| 'F'
|
|
37
|
+
| 'G'
|
|
38
|
+
| 'H'
|
|
39
|
+
| 'I'
|
|
40
|
+
| 'J'
|
|
41
|
+
| 'K'
|
|
42
|
+
| 'L'
|
|
43
|
+
| 'M'
|
|
44
|
+
| 'N'
|
|
45
|
+
| 'O'
|
|
46
|
+
| 'P'
|
|
47
|
+
| 'Q'
|
|
48
|
+
| 'R'
|
|
49
|
+
| 'S'
|
|
50
|
+
| 'T'
|
|
51
|
+
| 'U'
|
|
52
|
+
| 'V'
|
|
53
|
+
| 'W'
|
|
54
|
+
| 'X'
|
|
55
|
+
| 'Y'
|
|
56
|
+
| 'Z'
|
|
57
|
+
| '0'
|
|
58
|
+
| '1'
|
|
59
|
+
| '2'
|
|
60
|
+
| '3'
|
|
61
|
+
| '4'
|
|
62
|
+
| '5'
|
|
63
|
+
| '6'
|
|
64
|
+
| '7'
|
|
65
|
+
| '8'
|
|
66
|
+
| '9'
|
|
67
|
+
| '-'
|
|
68
|
+
| '_';
|
|
69
|
+
|
|
70
|
+
type ValidateName<T extends string, Original extends string = T> =
|
|
71
|
+
// For widened strings, don't attempt validation – let them flow through unchanged
|
|
72
|
+
string extends T // string is not wider than T, ie T is string
|
|
73
|
+
? T
|
|
74
|
+
: T extends ''
|
|
75
|
+
? Original extends ''
|
|
76
|
+
? '❌ Name cannot be empty'
|
|
77
|
+
: Original
|
|
78
|
+
: T extends `${infer First}${infer Rest}`
|
|
79
|
+
? First extends ValidChars
|
|
80
|
+
? ValidateName<Rest, Original>
|
|
81
|
+
: `❌ Invalid character in "${Original}". Only A-Z, a-z, 0-9, -, _ allowed`
|
|
82
|
+
: never;
|
|
83
|
+
|
|
84
|
+
type DefaultMaxDepth = 8;
|
|
85
|
+
type HasDefaults<S> = S extends {
|
|
86
|
+
_zod: {
|
|
87
|
+
def: {
|
|
88
|
+
defaultValue: unknown;
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
} ? true : S extends $ZodObject<infer Shape> | ZodObject<infer Shape> ? {
|
|
92
|
+
[K in keyof Shape]: HasDefaults<Shape[K]>;
|
|
93
|
+
} extends Record<keyof Shape, true> ? true : false : false;
|
|
94
|
+
type UnwrapSchema<T> = T extends ZodDefault<infer U> ? U : T;
|
|
95
|
+
type AllFieldsHaveDefaults<Schema> = Schema extends {
|
|
96
|
+
_zod: {
|
|
97
|
+
def: {
|
|
98
|
+
defaultValue: unknown;
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
} ? true : HasDefaults<UnwrapSchema<Schema>>;
|
|
102
|
+
interface AppScopeConfig<FlagSchema extends ZodObject<any> | undefined = undefined, FactSchema extends ZodObject<any> | undefined = undefined> {
|
|
103
|
+
flagSchema: FlagSchema;
|
|
104
|
+
factSchema?: FactSchema;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Recursive type to extract all possible paths from an object type.
|
|
108
|
+
* Uses stack-based depth limiting for better performance.
|
|
109
|
+
*
|
|
110
|
+
* @template T - The object type to extract paths from
|
|
111
|
+
* @template Stack - Internal stack counter (do not set manually)
|
|
112
|
+
* @template MaxDepth - Maximum recursion depth (default: 8 for good balance)
|
|
113
|
+
*/
|
|
114
|
+
type ObjectPaths<T, Stack extends unknown[] = [], MaxDepth extends number = DefaultMaxDepth> = Stack['length'] extends MaxDepth ? never : T extends object ? {
|
|
115
|
+
[K in keyof T]-?: K extends string | number ? `${K}` | `${K}.${ObjectPaths<T[K], [1, ...Stack], MaxDepth>}` : never;
|
|
116
|
+
}[keyof T] : never;
|
|
117
|
+
type ObjectPathValue<T, P extends string> = P extends keyof T ? T[P] : P extends `${infer K}.${infer Rest}` ? K extends keyof T ? ObjectPathValue<T[K], Rest> : never : never;
|
|
118
|
+
/**
|
|
119
|
+
* Generate deep nested paths from flag schema.
|
|
120
|
+
*
|
|
121
|
+
* @template T - ZodObject to extract paths from
|
|
122
|
+
* @template MaxDepth - Maximum recursion depth (default: 8, override for deeper nesting)
|
|
123
|
+
* @example
|
|
124
|
+
* // Default 8-level depth
|
|
125
|
+
* type Paths = DotPaths<MySchema>
|
|
126
|
+
*
|
|
127
|
+
* // Custom depth for deeper nesting (impacts performance)
|
|
128
|
+
* type DeepPaths = DotPaths<MySchema, 12>
|
|
129
|
+
*/
|
|
130
|
+
type DotPaths<T extends ZodObject<any>, MaxDepth extends number = DefaultMaxDepth> = {
|
|
131
|
+
[NS in keyof T['shape']]: (string & NS) | {
|
|
132
|
+
[P in ObjectPaths<z.output<UnwrapSchema<T['shape'][NS]>>, [
|
|
133
|
+
], MaxDepth>]: `${string & NS}.${P}`;
|
|
134
|
+
}[ObjectPaths<z.output<UnwrapSchema<T['shape'][NS]>>, [], MaxDepth>];
|
|
135
|
+
}[keyof T['shape']];
|
|
136
|
+
type PathValue<T extends ZodObject<any>, P extends string> = P extends `${infer NS}.${infer Rest}` ? NS extends keyof T['shape'] ? ObjectPathValue<z.output<UnwrapSchema<T['shape'][NS]>>, Rest> : never : P extends keyof T['shape'] ? z.output<UnwrapSchema<T['shape'][P]>> : never;
|
|
137
|
+
type DotNotationFlagFunction<FS extends ZodObject<any> | undefined> = FS extends ZodObject<any> ? <P extends DotPaths<FS>>(path: P) => PathValue<FS, P> : never;
|
|
138
|
+
type FactFunction<SC extends ZodObject<any> | undefined> = SC extends ZodObject<any> ? <P extends DotPaths<SC> & string>(name: P, value: PathValue<SC, P>) => void : never;
|
|
139
|
+
type OverrideFlagsFunction<FS extends ZodObject<any> | undefined> = FS extends ZodObject<any> ? (partial: {
|
|
140
|
+
[K in DotPaths<FS>]?: PathValue<FS, K>;
|
|
141
|
+
}) => void : (partial: Record<string, any>) => void;
|
|
142
|
+
type WithFlagsFunction<FS extends ZodObject<any> | undefined> = FS extends ZodObject<any> ? <T>(overrides: {
|
|
143
|
+
[K in DotPaths<FS>]?: PathValue<FS, K>;
|
|
144
|
+
}, fn: () => T) => T : <T>(overrides: Record<string, any>, fn: () => T) => T;
|
|
145
|
+
type PickFlagsFunction<FS extends ZodObject<any> | undefined> = FS extends ZodObject<any> ? {
|
|
146
|
+
<K extends ReadonlyArray<DotPaths<FS> & string>>(...paths: K): K;
|
|
147
|
+
<K extends ReadonlyArray<DotPaths<FS> & string>>(paths: K): K;
|
|
148
|
+
} : never;
|
|
149
|
+
interface AppScope<FS extends ZodObject<any> | undefined, SC extends ZodObject<any> | undefined> {
|
|
150
|
+
flag: DotNotationFlagFunction<FS>;
|
|
151
|
+
fact: FactFunction<SC>;
|
|
152
|
+
overrideFlags: OverrideFlagsFunction<FS>;
|
|
153
|
+
withFlags: WithFlagsFunction<FS>;
|
|
154
|
+
pickFlags: PickFlagsFunction<FS>;
|
|
155
|
+
getAllDefaultFlags: () => Record<string, any>;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Create a new application-level evaluation scope.
|
|
159
|
+
*
|
|
160
|
+
* @param config.flagSchema A zod object describing the schema for flags **(required)**
|
|
161
|
+
* @param config.factSchema A zod object describing the schema for facts (optional)
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* import { z } from 'zod';
|
|
165
|
+
*
|
|
166
|
+
* const { flag, fact, withFlags, pickFlags, overrideFlags } = createAppScope({
|
|
167
|
+
* flagSchema: z.object({
|
|
168
|
+
* ui: z.object({
|
|
169
|
+
* darkMode: z.boolean().default(false),
|
|
170
|
+
* theme: z.object({
|
|
171
|
+
* primary: z.string().default('#00f'),
|
|
172
|
+
* }),
|
|
173
|
+
* }),
|
|
174
|
+
* api: z.object({
|
|
175
|
+
endpoint: z.string().default('/api')
|
|
176
|
+
}),
|
|
177
|
+
* }),
|
|
178
|
+
* factSchema: z.object({
|
|
179
|
+
* userAction: z.string(),
|
|
180
|
+
* timing: z.number(),
|
|
181
|
+
* }),
|
|
182
|
+
* });
|
|
183
|
+
*
|
|
184
|
+
* // Typed flag access
|
|
185
|
+
* const dark = flag('ui.darkMode'); // inferred boolean
|
|
186
|
+
* const theme = flag('ui.theme'); // entire object
|
|
187
|
+
* const primary = flag('ui.theme.primary'); // '#00f'
|
|
188
|
+
* const endpoint = flag('api.endpoint'); // uses schema default
|
|
189
|
+
*
|
|
190
|
+
* // Typed fact recording
|
|
191
|
+
* fact('userAction', 'clicked_button');
|
|
192
|
+
* fact('timing', 1250);
|
|
193
|
+
*
|
|
194
|
+
* // Temporarily override flags for a block of code
|
|
195
|
+
* withFlags({ 'ui.darkMode': true }, () => {
|
|
196
|
+
* // code here, `ui.darkMode` will be true in this block and reset after
|
|
197
|
+
* });
|
|
198
|
+
*
|
|
199
|
+
* // Override flags globally for the current evaluation run
|
|
200
|
+
* overrideFlags({ 'api.endpoint': '/custom' });
|
|
201
|
+
*/
|
|
202
|
+
declare function createAppScope<FlagSchema extends ZodObject<any>, FactSchema extends ZodObject<any> | undefined = undefined>(config: AllFieldsHaveDefaults<FlagSchema> extends true ? AppScopeConfig<FlagSchema, FactSchema> : {
|
|
203
|
+
flagSchema: FlagSchema;
|
|
204
|
+
factSchema?: FactSchema;
|
|
205
|
+
__error__: 'createAppScope: flagSchema must have .default() for all leaf fields';
|
|
206
|
+
}): AppScope<FlagSchema, FactSchema>;
|
|
207
|
+
|
|
208
|
+
export { type ValidateName as V, createAppScope as c };
|