@showwhat/core 2.0.0 → 2.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.
- package/README.md +6 -2
- package/dist/index.d.ts +83 -34
- package/dist/index.js +379 -290
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -25,12 +25,16 @@ const data = await MemoryData.fromObject({
|
|
|
25
25
|
|
|
26
26
|
const def = await data.get("checkout_v2");
|
|
27
27
|
|
|
28
|
-
const
|
|
28
|
+
const results = await resolve({
|
|
29
29
|
definitions: { checkout_v2: def! },
|
|
30
30
|
context: { env: "prod" },
|
|
31
31
|
options: { evaluators: builtinEvaluators },
|
|
32
32
|
});
|
|
33
|
-
|
|
33
|
+
|
|
34
|
+
const entry = results["checkout_v2"];
|
|
35
|
+
if (entry.success) {
|
|
36
|
+
console.log(entry.value); // true
|
|
37
|
+
}
|
|
34
38
|
```
|
|
35
39
|
|
|
36
40
|
> **Note:** The resolver is strict — evaluators must be passed explicitly. There is no default evaluator set injected automatically.
|
package/dist/index.d.ts
CHANGED
|
@@ -38,6 +38,31 @@ declare class ConditionError extends ShowwhatError {
|
|
|
38
38
|
readonly conditionType: string;
|
|
39
39
|
constructor(conditionType: string, message: string, cause?: unknown);
|
|
40
40
|
}
|
|
41
|
+
declare class UnknownConditionTypeError extends ShowwhatError {
|
|
42
|
+
readonly conditionType: string;
|
|
43
|
+
readonly condition: unknown;
|
|
44
|
+
constructor(conditionType: string, condition: unknown);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
declare const DataPrimitiveSchema: z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean]>;
|
|
48
|
+
/**
|
|
49
|
+
* Primitive values inferred from the schema — string | number | boolean.
|
|
50
|
+
*/
|
|
51
|
+
type DataPrimitive = z.infer<typeof DataPrimitiveSchema>;
|
|
52
|
+
/**
|
|
53
|
+
* The set of value types that showwhat's data model understands.
|
|
54
|
+
*
|
|
55
|
+
* Used as the structural base for both `ContextValue` and `AnnotationValue`.
|
|
56
|
+
*
|
|
57
|
+
* **Why this isn't `z.infer`:** TypeScript cannot infer recursive types from
|
|
58
|
+
* self-referencing `const` declarations (TS7022). The schema below must be
|
|
59
|
+
* annotated with this type; `DataPrimitive` is still inferred from its schema,
|
|
60
|
+
* keeping the non-recursive portion derived from the single source of truth.
|
|
61
|
+
*/
|
|
62
|
+
type DataValue = DataPrimitive | DataPrimitive[] | {
|
|
63
|
+
[key: string]: DataValue;
|
|
64
|
+
};
|
|
65
|
+
declare const DataValueSchema: z.ZodType<DataValue>;
|
|
41
66
|
|
|
42
67
|
declare const PRIMITIVE_CONDITION_TYPES: {
|
|
43
68
|
readonly string: "string";
|
|
@@ -52,6 +77,7 @@ declare const CONDITION_TYPES: {
|
|
|
52
77
|
readonly endAt: "endAt";
|
|
53
78
|
readonly and: "and";
|
|
54
79
|
readonly or: "or";
|
|
80
|
+
readonly checkAnnotations: "checkAnnotations";
|
|
55
81
|
readonly string: "string";
|
|
56
82
|
readonly number: "number";
|
|
57
83
|
readonly bool: "bool";
|
|
@@ -66,9 +92,9 @@ declare const StringConditionSchema: z.ZodObject<{
|
|
|
66
92
|
type: z.ZodLiteral<"string">;
|
|
67
93
|
key: z.ZodString;
|
|
68
94
|
op: z.ZodEnum<{
|
|
95
|
+
in: "in";
|
|
69
96
|
eq: "eq";
|
|
70
97
|
neq: "neq";
|
|
71
|
-
in: "in";
|
|
72
98
|
nin: "nin";
|
|
73
99
|
regex: "regex";
|
|
74
100
|
}>;
|
|
@@ -80,9 +106,9 @@ declare const NumberConditionSchema: z.ZodObject<{
|
|
|
80
106
|
type: z.ZodLiteral<"number">;
|
|
81
107
|
key: z.ZodString;
|
|
82
108
|
op: z.ZodEnum<{
|
|
109
|
+
in: "in";
|
|
83
110
|
eq: "eq";
|
|
84
111
|
neq: "neq";
|
|
85
|
-
in: "in";
|
|
86
112
|
nin: "nin";
|
|
87
113
|
gt: "gt";
|
|
88
114
|
gte: "gte";
|
|
@@ -138,9 +164,9 @@ declare const BuiltinConditionSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
138
164
|
type: z.ZodLiteral<"string">;
|
|
139
165
|
key: z.ZodString;
|
|
140
166
|
op: z.ZodEnum<{
|
|
167
|
+
in: "in";
|
|
141
168
|
eq: "eq";
|
|
142
169
|
neq: "neq";
|
|
143
|
-
in: "in";
|
|
144
170
|
nin: "nin";
|
|
145
171
|
regex: "regex";
|
|
146
172
|
}>;
|
|
@@ -150,9 +176,9 @@ declare const BuiltinConditionSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
|
150
176
|
type: z.ZodLiteral<"number">;
|
|
151
177
|
key: z.ZodString;
|
|
152
178
|
op: z.ZodEnum<{
|
|
179
|
+
in: "in";
|
|
153
180
|
eq: "eq";
|
|
154
181
|
neq: "neq";
|
|
155
|
-
in: "in";
|
|
156
182
|
nin: "nin";
|
|
157
183
|
gt: "gt";
|
|
158
184
|
gte: "gte";
|
|
@@ -201,6 +227,10 @@ type Condition = BuiltinCondition | {
|
|
|
201
227
|
id?: string;
|
|
202
228
|
type: "or";
|
|
203
229
|
conditions: Condition[];
|
|
230
|
+
} | {
|
|
231
|
+
id?: string;
|
|
232
|
+
type: "checkAnnotations";
|
|
233
|
+
conditions: Condition[];
|
|
204
234
|
} | {
|
|
205
235
|
type: string;
|
|
206
236
|
[key: string]: unknown;
|
|
@@ -215,19 +245,24 @@ declare const OrConditionSchema: z.ZodObject<{
|
|
|
215
245
|
type: z.ZodLiteral<"or">;
|
|
216
246
|
conditions: z.ZodArray<z.ZodLazy<z.ZodType<Condition, unknown, z.core.$ZodTypeInternals<Condition, unknown>>>>;
|
|
217
247
|
}, z.core.$strip>;
|
|
248
|
+
declare const CheckAnnotationsConditionSchema: z.ZodObject<{
|
|
249
|
+
id: z.ZodOptional<z.ZodString>;
|
|
250
|
+
type: z.ZodLiteral<"checkAnnotations">;
|
|
251
|
+
conditions: z.ZodArray<z.ZodLazy<z.ZodType<Condition, unknown, z.core.$ZodTypeInternals<Condition, unknown>>>>;
|
|
252
|
+
}, z.core.$strip>;
|
|
218
253
|
declare const ConditionSchema: z.ZodType<Condition>;
|
|
219
254
|
type AndCondition = z.infer<typeof AndConditionSchema>;
|
|
220
255
|
type OrCondition = z.infer<typeof OrConditionSchema>;
|
|
221
|
-
type
|
|
256
|
+
type CheckAnnotationsCondition = z.infer<typeof CheckAnnotationsConditionSchema>;
|
|
257
|
+
type CompositeCondition = AndCondition | OrCondition | CheckAnnotationsCondition;
|
|
222
258
|
declare function isAndCondition(c: Condition): c is AndCondition;
|
|
223
259
|
declare function isOrCondition(c: Condition): c is OrCondition;
|
|
260
|
+
declare function isCheckAnnotationsCondition(c: Condition): c is CheckAnnotationsCondition;
|
|
224
261
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
};
|
|
230
|
-
type Context<T extends Record<string, ContextValue> = Record<string, ContextValue>> = T;
|
|
262
|
+
type ContextValue = DataValue;
|
|
263
|
+
declare const ContextValueSchema: z.ZodType<DataValue, unknown, z.core.$ZodTypeInternals<DataValue, unknown>>;
|
|
264
|
+
declare const ContextSchema: z.ZodRecord<z.ZodString, z.ZodType<DataValue, unknown, z.core.$ZodTypeInternals<DataValue, unknown>>>;
|
|
265
|
+
type Context = Record<string, ContextValue>;
|
|
231
266
|
|
|
232
267
|
type VariationValue = unknown;
|
|
233
268
|
declare const VariationValueSchema: z.ZodType<VariationValue>;
|
|
@@ -289,7 +324,6 @@ type PresetDefinition = {
|
|
|
289
324
|
overrides?: Record<string, unknown>;
|
|
290
325
|
};
|
|
291
326
|
type Presets = Record<string, PresetDefinition>;
|
|
292
|
-
declare const PRIMITIVE_TYPES: Set<string>;
|
|
293
327
|
declare const PresetsSchema: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
294
328
|
type: z.ZodString;
|
|
295
329
|
key: z.ZodOptional<z.ZodString>;
|
|
@@ -320,11 +354,28 @@ type ResolutionError = {
|
|
|
320
354
|
error: ShowwhatError;
|
|
321
355
|
};
|
|
322
356
|
|
|
323
|
-
|
|
324
|
-
|
|
357
|
+
interface Logger {
|
|
358
|
+
debug(message: string, data?: Record<string, unknown>): void;
|
|
359
|
+
info(message: string, data?: Record<string, unknown>): void;
|
|
360
|
+
warn(message: string, data?: Record<string, unknown>): void;
|
|
361
|
+
error(message: string, data?: Record<string, unknown>): void;
|
|
362
|
+
}
|
|
363
|
+
declare const noopLogger: Logger;
|
|
364
|
+
|
|
365
|
+
declare const AnnotationValueSchema: z.ZodType<DataValue, unknown, z.core.$ZodTypeInternals<DataValue, unknown>>;
|
|
366
|
+
/**
|
|
367
|
+
* Semantic alias for `DataValue`. Annotations share the same structural base today,
|
|
368
|
+
* but may diverge in the future (e.g. restricted to flat records). Keeping the alias
|
|
369
|
+
* lets call sites express intent and makes a future split a non-breaking change.
|
|
370
|
+
*/
|
|
371
|
+
type AnnotationValue = DataValue;
|
|
372
|
+
declare const AnnotationsSchema: z.ZodRecord<z.ZodString, z.ZodType<DataValue, unknown, z.core.$ZodTypeInternals<DataValue, unknown>>>;
|
|
373
|
+
type Annotations = Record<string, AnnotationValue>;
|
|
374
|
+
type Dependencies = Record<string, unknown>;
|
|
325
375
|
type RegexFactory = (pattern: string) => {
|
|
326
376
|
test: (input: string) => boolean;
|
|
327
377
|
};
|
|
378
|
+
type AnnotationsFactory = (definitionKey?: string) => Annotations;
|
|
328
379
|
declare const defaultCreateRegex: RegexFactory;
|
|
329
380
|
type ConditionEvaluatorArgs = {
|
|
330
381
|
condition: unknown;
|
|
@@ -333,19 +384,16 @@ type ConditionEvaluatorArgs = {
|
|
|
333
384
|
deps: Readonly<Dependencies>;
|
|
334
385
|
depth: string;
|
|
335
386
|
createRegex: RegexFactory;
|
|
387
|
+
evaluators: ConditionEvaluators;
|
|
388
|
+
logger?: Logger;
|
|
336
389
|
};
|
|
337
390
|
type ConditionEvaluator = (args: ConditionEvaluatorArgs) => Promise<boolean>;
|
|
338
|
-
|
|
391
|
+
declare const FALLBACK_EVALUATOR_KEY: unique symbol;
|
|
392
|
+
type ConditionEvaluators = Record<string, ConditionEvaluator> & {
|
|
393
|
+
[FALLBACK_EVALUATOR_KEY]?: ConditionEvaluator;
|
|
394
|
+
};
|
|
339
395
|
declare const noConditionEvaluator: ConditionEvaluator;
|
|
340
396
|
|
|
341
|
-
interface Logger {
|
|
342
|
-
debug(message: string, data?: Record<string, unknown>): void;
|
|
343
|
-
info(message: string, data?: Record<string, unknown>): void;
|
|
344
|
-
warn(message: string, data?: Record<string, unknown>): void;
|
|
345
|
-
error(message: string, data?: Record<string, unknown>): void;
|
|
346
|
-
}
|
|
347
|
-
declare const noopLogger: Logger;
|
|
348
|
-
|
|
349
397
|
type EvaluateConditionArgs = {
|
|
350
398
|
condition: Condition;
|
|
351
399
|
context: Readonly<Context>;
|
|
@@ -354,24 +402,25 @@ type EvaluateConditionArgs = {
|
|
|
354
402
|
deps?: Readonly<Dependencies>;
|
|
355
403
|
depth?: string;
|
|
356
404
|
logger?: Logger;
|
|
357
|
-
fallback?: ConditionEvaluator;
|
|
358
405
|
createRegex?: RegexFactory;
|
|
359
406
|
};
|
|
360
|
-
declare function evaluateCondition({ condition, context, evaluators, annotations, deps, depth, logger,
|
|
407
|
+
declare function evaluateCondition({ condition, context, evaluators, annotations, deps, depth, logger, createRegex, }: EvaluateConditionArgs): Promise<boolean>;
|
|
361
408
|
|
|
362
|
-
declare const builtinEvaluators: ConditionEvaluators
|
|
409
|
+
declare const builtinEvaluators: ConditionEvaluators;
|
|
363
410
|
|
|
364
411
|
type ResolverOptions = {
|
|
365
412
|
evaluators?: ConditionEvaluators;
|
|
366
413
|
fallback?: ConditionEvaluator;
|
|
367
414
|
logger?: Logger;
|
|
368
415
|
createRegex?: RegexFactory;
|
|
416
|
+
createAnnotations?: AnnotationsFactory;
|
|
369
417
|
};
|
|
370
|
-
declare function resolveVariation
|
|
418
|
+
declare function resolveVariation({ variations, context, deps, options, definitionKey, }: {
|
|
371
419
|
variations: Variation[];
|
|
372
|
-
context: Readonly<Context
|
|
373
|
-
deps?: Dependencies
|
|
420
|
+
context: Readonly<Context>;
|
|
421
|
+
deps?: Dependencies;
|
|
374
422
|
options?: ResolverOptions;
|
|
423
|
+
definitionKey?: string;
|
|
375
424
|
}): Promise<{
|
|
376
425
|
variation: Variation;
|
|
377
426
|
variationIndex: number;
|
|
@@ -383,10 +432,10 @@ declare function resolveVariation<T extends Record<string, ContextValue> = Recor
|
|
|
383
432
|
* Each key is resolved independently — a failure for one key does not
|
|
384
433
|
* affect others. Failed keys are returned as `ResolutionError` entries.
|
|
385
434
|
*/
|
|
386
|
-
declare function resolve
|
|
435
|
+
declare function resolve({ definitions, context, deps, options, }: {
|
|
387
436
|
definitions: Definitions;
|
|
388
|
-
context: Readonly<Context
|
|
389
|
-
deps?: Dependencies
|
|
437
|
+
context: Readonly<Context>;
|
|
438
|
+
deps?: Dependencies;
|
|
390
439
|
options?: ResolverOptions;
|
|
391
440
|
}): Promise<Record<string, Resolution | ResolutionError>>;
|
|
392
441
|
|
|
@@ -423,7 +472,7 @@ declare class MemoryData implements DefinitionReader, PresetReader {
|
|
|
423
472
|
get(key: string): Promise<Definition | null>;
|
|
424
473
|
getAll(): Promise<Definitions>;
|
|
425
474
|
listKeys(): Promise<string[]>;
|
|
426
|
-
getPresets(): Promise<Presets>;
|
|
475
|
+
getPresets(_key?: string): Promise<Presets>;
|
|
427
476
|
}
|
|
428
477
|
|
|
429
478
|
declare function parseYaml(input: string): Promise<FileFormat>;
|
|
@@ -433,4 +482,4 @@ declare function parsePresetsYaml(input: string): Promise<Presets>;
|
|
|
433
482
|
|
|
434
483
|
declare function createPresetConditions(presets: Presets): ConditionEvaluators;
|
|
435
484
|
|
|
436
|
-
export { type AndCondition, type Annotations, type BoolCondition, BoolConditionSchema, type BuiltinCondition, BuiltinConditionSchema, CONDITION_TYPES, CONTEXT_KEYS, type CompositeCondition, type Condition, ConditionError, type ConditionEvaluator, type ConditionEvaluatorArgs, type ConditionEvaluators, ConditionSchema, type Context, ContextSchema, type ContextValue, DataError, type DatetimeCondition, DatetimeConditionSchema, type Definition, type DefinitionData, DefinitionInactiveError, DefinitionNotFoundError, type DefinitionReader, DefinitionSchema, type DefinitionWriter, type Definitions, DefinitionsSchema, type Dependencies, type EndAtCondition, EndAtConditionSchema, type EnvCondition, EnvConditionSchema, type EvaluateConditionArgs, type FileFormat, FileFormatSchema, InvalidContextError, type Logger, MemoryData, type NumberCondition, NumberConditionSchema, type OrCondition, PRIMITIVE_CONDITION_TYPES,
|
|
485
|
+
export { type AndCondition, type AnnotationValue, AnnotationValueSchema, type Annotations, type AnnotationsFactory, AnnotationsSchema, type BoolCondition, BoolConditionSchema, type BuiltinCondition, BuiltinConditionSchema, CONDITION_TYPES, CONTEXT_KEYS, type CheckAnnotationsCondition, type CompositeCondition, type Condition, ConditionError, type ConditionEvaluator, type ConditionEvaluatorArgs, type ConditionEvaluators, ConditionSchema, type Context, ContextSchema, type ContextValue, ContextValueSchema, DataError, type DataValue, DataValueSchema, type DatetimeCondition, DatetimeConditionSchema, type Definition, type DefinitionData, DefinitionInactiveError, DefinitionNotFoundError, type DefinitionReader, DefinitionSchema, type DefinitionWriter, type Definitions, DefinitionsSchema, type Dependencies, type EndAtCondition, EndAtConditionSchema, type EnvCondition, EnvConditionSchema, type EvaluateConditionArgs, FALLBACK_EVALUATOR_KEY, type FileFormat, FileFormatSchema, InvalidContextError, type Logger, MemoryData, type NumberCondition, NumberConditionSchema, type OrCondition, PRIMITIVE_CONDITION_TYPES, ParseError, type PresetDefinition, type PresetReader, type Presets, PresetsSchema, type PrimitiveConditionType, type RegexFactory, type Resolution, type ResolutionError, ResolutionSchema, type ResolverOptions, SchemaValidationError, ShowwhatError, type StartAtCondition, StartAtConditionSchema, type StringCondition, StringConditionSchema, UnknownConditionTypeError, ValidationError, type Variation, VariationNotFoundError, VariationSchema, type VariationValue, VariationValueSchema, builtinEvaluators, createPresetConditions, defaultCreateRegex, evaluateCondition, isAndCondition, isCheckAnnotationsCondition, isOrCondition, isWritable, noConditionEvaluator, noopLogger, parseObject, parsePresetsObject, parsePresetsYaml, parseYaml, resolve, resolveVariation };
|