@builder-builder/builder 0.0.14 → 0.0.15

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 (61) hide show
  1. package/dist/bb.d.ts +3 -1
  2. package/dist/bb.js +6 -1
  3. package/dist/check.d.ts +2 -2
  4. package/dist/entities/builder/builder.d.ts +11 -1
  5. package/dist/entities/builder/builder.js +22 -6
  6. package/dist/entities/collection/collection.d.ts +7 -1
  7. package/dist/entities/collection/collection.js +9 -2
  8. package/dist/entities/collection/config.d.ts +6 -1
  9. package/dist/entities/collection/config.js +9 -2
  10. package/dist/entities/component/component.d.ts +41 -1
  11. package/dist/entities/component/component.js +9 -2
  12. package/dist/entities/component/details.d.ts +11 -2
  13. package/dist/entities/component/details.js +9 -2
  14. package/dist/entities/component/field.d.ts +10 -1
  15. package/dist/entities/component/field.js +13 -3
  16. package/dist/entities/index.d.ts +5 -1
  17. package/dist/entities/index.js +2 -0
  18. package/dist/entities/kind.d.ts +1 -1
  19. package/dist/entities/model/model.d.ts +5 -1
  20. package/dist/entities/model/model.js +10 -3
  21. package/dist/entities/model/models.js +9 -5
  22. package/dist/entities/option/option.d.ts +41 -1
  23. package/dist/entities/option/option.js +9 -2
  24. package/dist/entities/option/select.d.ts +7 -1
  25. package/dist/entities/option/select.js +17 -6
  26. package/dist/entities/option/toggle.d.ts +7 -1
  27. package/dist/entities/option/toggle.js +16 -4
  28. package/dist/entities/option/values.d.ts +6 -0
  29. package/dist/entities/pricing.d.ts +64 -0
  30. package/dist/entities/pricing.js +48 -0
  31. package/dist/entities/serialise.d.ts +78 -0
  32. package/dist/entities/serialise.js +30 -11
  33. package/dist/entities/tags.d.ts +3 -0
  34. package/dist/entities/tags.js +2 -0
  35. package/dist/entities/ui/describe.d.ts +10 -1
  36. package/dist/entities/ui/describe.js +9 -2
  37. package/dist/entities/ui/input.d.ts +10 -1
  38. package/dist/entities/ui/input.js +17 -5
  39. package/dist/entities/ui/page.d.ts +10 -1
  40. package/dist/entities/ui/page.js +9 -2
  41. package/dist/entities/ui/pages.d.ts +5 -1
  42. package/dist/entities/ui/pages.js +9 -2
  43. package/dist/entities/ui/ui.d.ts +6 -1
  44. package/dist/entities/ui/ui.js +27 -12
  45. package/dist/entities/validated.d.ts +3 -1
  46. package/dist/exception.d.ts +2 -2
  47. package/dist/index.d.ts +2 -2
  48. package/dist/instance.d.ts +9 -0
  49. package/dist/instance.js +3 -1
  50. package/dist/mappers/index.d.ts +3 -2
  51. package/dist/mappers/index.js +2 -1
  52. package/dist/mappers/pricing.d.ts +3 -0
  53. package/dist/mappers/pricing.js +101 -0
  54. package/dist/validate/builder.js +5 -1
  55. package/dist/validate/errors.d.ts +9 -1
  56. package/dist/validate/errors.js +7 -0
  57. package/dist/validate/index.d.ts +3 -1
  58. package/dist/validate/index.js +1 -0
  59. package/dist/validate/pricing.d.ts +6 -0
  60. package/dist/validate/pricing.js +71 -0
  61. package/package.json +1 -1
@@ -0,0 +1,101 @@
1
+ import * as v from 'valibot';
2
+ import { check } from '../check.js';
3
+ import { BuilderPricingSchema } from '../entities/index.js';
4
+ import { BuilderVariantSchema } from '../instance.js';
5
+ import { resolveRef } from './resolve.js';
6
+ export function pricing(model, pricingInput, order, refs = []) {
7
+ const resolved = resolveRef(pricingInput, refs);
8
+ check.assert(BuilderPricingSchema, resolved, 'Pricing did not resolve to a pricing entity! ❌');
9
+ const { rates, formula } = resolved;
10
+ const modelTags = model.tags ?? [];
11
+ const variants = collectVariants(order);
12
+ return evaluate(formula);
13
+ function evaluate(expression, currentVariant = null) {
14
+ switch (expression.kind) {
15
+ case 'num':
16
+ return expression.value;
17
+ case 'variantPrice':
18
+ return variantPrice(currentVariant);
19
+ case 'rate':
20
+ return evaluateRate(expression, currentVariant);
21
+ case 'variants':
22
+ return evaluateVariants(expression);
23
+ case 'add':
24
+ return (evaluate(expression.left, currentVariant) + evaluate(expression.right, currentVariant));
25
+ case 'sub':
26
+ return (evaluate(expression.left, currentVariant) - evaluate(expression.right, currentVariant));
27
+ case 'mul':
28
+ return (evaluate(expression.left, currentVariant) * evaluate(expression.right, currentVariant));
29
+ case 'div':
30
+ return (evaluate(expression.left, currentVariant) / evaluate(expression.right, currentVariant));
31
+ }
32
+ }
33
+ function evaluateRate(expression, currentVariant) {
34
+ const table = rates[expression.rate];
35
+ check.truthy(table, `Pricing: rate '${expression.rate}' not found! ❌`);
36
+ const tagOrTags = resolveTagExpression(expression.tag, currentVariant);
37
+ const keys = Array.isArray(tagOrTags) ? tagOrTags : [tagOrTags];
38
+ const matches = keys
39
+ .map((key) => table[key])
40
+ .filter((value) => typeof value === 'number');
41
+ check.truthy(matches.length > 0, `Pricing: rate '${expression.rate}' has no entry for keys [${keys.join(', ')}]! ❌`);
42
+ return reduceValues(matches, expression.reduce ?? 'product');
43
+ }
44
+ function evaluateVariants(expression) {
45
+ const { tag } = expression;
46
+ const candidates = tag ? variants.filter((variant) => matchesFilter(variant, tag)) : variants;
47
+ const results = candidates.map((variant) => evaluate(expression.expression, variant));
48
+ return reduceValues(results, expression.reduce ?? 'sum');
49
+ }
50
+ function resolveTagExpression(tag, currentVariant) {
51
+ switch (tag.kind) {
52
+ case 'tag':
53
+ return tag.value;
54
+ case 'modelTags':
55
+ return modelTags;
56
+ case 'variantTags':
57
+ check.truthy(currentVariant, 'Pricing: variantTags used outside variants iteration! ❌');
58
+ return currentVariant.tags ?? [];
59
+ }
60
+ }
61
+ function matchesFilter(variant, filter) {
62
+ if (filter.kind === 'tag') {
63
+ return (variant.tags ?? []).includes(filter.value);
64
+ }
65
+ if (filter.kind === 'modelTags') {
66
+ return (variant.tags ?? []).some((tag) => modelTags.includes(tag));
67
+ }
68
+ check.falsy(true, 'Pricing: variantTags is not a valid variants.tag filter! ❌');
69
+ return false;
70
+ }
71
+ }
72
+ function reduceValues(values, mode) {
73
+ if (mode === 'product') {
74
+ return values.reduce((accumulator, value) => accumulator * value, 1);
75
+ }
76
+ if (mode === 'sum') {
77
+ return values.reduce((accumulator, value) => accumulator + value, 0);
78
+ }
79
+ check.truthy(values.length > 0, 'Pricing: cannot reduce empty set with "first"! ❌');
80
+ return values[0];
81
+ }
82
+ function variantPrice(variant) {
83
+ check.truthy(variant, 'Pricing: variantPrice used outside variants iteration! ❌');
84
+ const price = variant.details?.price;
85
+ check.assert(v.number(), price, 'Pricing: variant missing numeric price detail! ❌');
86
+ return price;
87
+ }
88
+ function collectVariants(order) {
89
+ return Object.values(order).flatMap((value) => {
90
+ if (value == null) {
91
+ return [];
92
+ }
93
+ if (check.is(BuilderVariantSchema, value)) {
94
+ return [value];
95
+ }
96
+ if (Array.isArray(value)) {
97
+ return value.flatMap(collectVariants);
98
+ }
99
+ return collectVariants(value);
100
+ });
101
+ }
@@ -3,6 +3,7 @@ import { builder, BuilderSerialisedSchema, modelsMerge, serialise } from '../ent
3
3
  import { validate } from './brand.js';
4
4
  import { BuilderValidateErrors } from './errors.js';
5
5
  import { checkModelExpectations, validateModelStructure } from './model.js';
6
+ import { validatePricingStructure } from './pricing.js';
6
7
  import { resolver } from './resolve.js';
7
8
  import { checkUIExpectations, validateUIStructure } from './ui.js';
8
9
  export function validateBuilder(input, references = [], errors = new BuilderValidateErrors()) {
@@ -20,8 +21,11 @@ export function validateBuilder(input, references = [], errors = new BuilderVali
20
21
  errors.scope('ui', () => {
21
22
  checkUIExpectations(mergedModel, validatedUI, errors);
22
23
  });
24
+ if (input.pricing) {
25
+ errors.scope('pricing', () => validatePricingStructure(resolve(input.pricing) ?? input.pricing, errors));
26
+ }
23
27
  const data = validate({
24
- ...serialise.builder(builder()),
28
+ ...input,
25
29
  model: mergedModel,
26
30
  ui: validatedUI
27
31
  });
@@ -1,8 +1,9 @@
1
1
  import type { BuilderExpectationKind } from '../entities/index';
2
2
  import type { BuilderErrorLocation, BuilderErrors } from '../exception';
3
3
  import type { BuilderInstance } from '../instance';
4
- export type BuilderInvalidInputTarget = 'builder' | 'model' | 'ui' | 'variants';
4
+ export type BuilderInvalidInputTarget = 'builder' | 'model' | 'pricing' | 'ui' | 'variants';
5
5
  export type BuilderErrorInvalidPathReason = 'shape' | 'out-of-bounds' | 'missing-collection' | 'option-not-found';
6
+ export type BuilderErrorInvalidPricingReason = 'scope' | 'nested-variants' | 'missing-rate' | 'invalid-filter';
6
7
  export declare class BuilderValidateErrors {
7
8
  #private;
8
9
  get errors(): BuilderErrors;
@@ -109,6 +110,12 @@ export declare class BuilderValidateErrors {
109
110
  kind: "invalid-detail";
110
111
  location: BuilderErrorLocation;
111
112
  };
113
+ invalidPricing(reason: BuilderErrorInvalidPricingReason, detail?: string): {
114
+ reason: BuilderErrorInvalidPricingReason;
115
+ detail: string | undefined;
116
+ kind: "invalid-pricing";
117
+ location: BuilderErrorLocation;
118
+ };
112
119
  }
113
120
  export type BuilderErrorInvalidInput = ReturnType<BuilderValidateErrors['invalidInput']>;
114
121
  export type BuilderErrorInvalidOption = ReturnType<BuilderValidateErrors['invalidOption']>;
@@ -128,3 +135,4 @@ export type BuilderErrorInvalidVariant = ReturnType<BuilderValidateErrors['inval
128
135
  export type BuilderErrorMissingDetail = ReturnType<BuilderValidateErrors['missingDetail']>;
129
136
  export type BuilderErrorUnexpectedDetail = ReturnType<BuilderValidateErrors['unexpectedDetail']>;
130
137
  export type BuilderErrorInvalidDetail = ReturnType<BuilderValidateErrors['invalidDetail']>;
138
+ export type BuilderErrorInvalidPricing = ReturnType<BuilderValidateErrors['invalidPricing']>;
@@ -134,6 +134,13 @@ export class BuilderValidateErrors {
134
134
  instance
135
135
  });
136
136
  }
137
+ invalidPricing(reason, detail) {
138
+ return this.#addError({
139
+ ...builderError('invalid-pricing', this.#location),
140
+ reason,
141
+ detail
142
+ });
143
+ }
137
144
  #addError(error) {
138
145
  this.#errors = [...this.#errors, error];
139
146
  return error;
@@ -1,12 +1,14 @@
1
1
  export type { BuilderValidationResult } from './builder';
2
- export type { BuilderErrorDuplicateName, BuilderErrorInvalidCollection, BuilderErrorInvalidCollectionBounds, BuilderErrorInvalidDetail, BuilderErrorInvalidInput, BuilderErrorInvalidOption, BuilderErrorInvalidPath, BuilderErrorInvalidPathReason, BuilderErrorInvalidSelectMapKey, BuilderErrorInvalidVariant, BuilderErrorMissingComponent, BuilderErrorMissingDetail, BuilderErrorMissingReference, BuilderErrorMissingVariant, BuilderErrorUnboundParameter, BuilderErrorUnexpectedComponent, BuilderErrorUnexpectedDetail, BuilderErrorUnmetExpectation, BuilderErrorUnvalidated, BuilderInvalidInputTarget } from './errors';
2
+ export type { BuilderErrorDuplicateName, BuilderErrorInvalidCollection, BuilderErrorInvalidCollectionBounds, BuilderErrorInvalidDetail, BuilderErrorInvalidInput, BuilderErrorInvalidOption, BuilderErrorInvalidPath, BuilderErrorInvalidPathReason, BuilderErrorInvalidPricing, BuilderErrorInvalidPricingReason, BuilderErrorInvalidSelectMapKey, BuilderErrorInvalidVariant, BuilderErrorMissingComponent, BuilderErrorMissingDetail, BuilderErrorMissingReference, BuilderErrorMissingVariant, BuilderErrorUnboundParameter, BuilderErrorUnexpectedComponent, BuilderErrorUnexpectedDetail, BuilderErrorUnmetExpectation, BuilderErrorUnvalidated, BuilderInvalidInputTarget } from './errors';
3
3
  export type { BuilderInstanceValidationResult } from './instance';
4
4
  export type { BuilderModelValidationResult } from './model';
5
+ export type { BuilderPricingValidationResult } from './pricing';
5
6
  export type { BuilderUIValidationResult } from './ui';
6
7
  export type { BuilderComponentVariantsValidationResult, BuilderVariantsValidationOptions } from './variants';
7
8
  export { assertValidated } from './brand.js';
8
9
  export { validateBuilder } from './builder.js';
9
10
  export { validateInstance } from './instance.js';
10
11
  export { validateModel } from './model.js';
12
+ export { validatePricing } from './pricing.js';
11
13
  export { validateUI } from './ui.js';
12
14
  export { validateVariants } from './variants.js';
@@ -2,5 +2,6 @@ export { assertValidated } from './brand.js';
2
2
  export { validateBuilder } from './builder.js';
3
3
  export { validateInstance } from './instance.js';
4
4
  export { validateModel } from './model.js';
5
+ export { validatePricing } from './pricing.js';
5
6
  export { validateUI } from './ui.js';
6
7
  export { validateVariants } from './variants.js';
@@ -0,0 +1,6 @@
1
+ import type { BuilderPricingValidated } from '../entities/index';
2
+ import type { ValidationResult } from './result';
3
+ import { BuilderValidateErrors } from './errors.js';
4
+ export type BuilderPricingValidationResult = ValidationResult<BuilderPricingValidated>;
5
+ export declare function validatePricing(input: unknown, errors?: BuilderValidateErrors): BuilderPricingValidationResult;
6
+ export declare function validatePricingStructure(pricingInput: unknown, errors: BuilderValidateErrors): void;
@@ -0,0 +1,71 @@
1
+ import { check } from '../check.js';
2
+ import { BuilderPricingSchema } from '../entities/index.js';
3
+ import { validate } from './brand.js';
4
+ import { BuilderValidateErrors } from './errors.js';
5
+ const EMPTY_PRICING = validate({
6
+ rates: {},
7
+ formula: { kind: 'num', value: 0 }
8
+ });
9
+ export function validatePricing(input, errors = new BuilderValidateErrors()) {
10
+ if (!check.is(BuilderPricingSchema, input)) {
11
+ errors.invalidInput('pricing');
12
+ return [EMPTY_PRICING, errors.errors];
13
+ }
14
+ validatePricingStructure(input, errors);
15
+ return [validate(input), errors.errors];
16
+ }
17
+ export function validatePricingStructure(pricingInput, errors) {
18
+ if (!check.is(BuilderPricingSchema, pricingInput)) {
19
+ return;
20
+ }
21
+ const pricing = pricingInput;
22
+ walkExpr(pricing.formula, false);
23
+ function walkExpr(expression, insideVariants) {
24
+ switch (expression.kind) {
25
+ case 'num':
26
+ return;
27
+ case 'variantPrice':
28
+ if (!insideVariants) {
29
+ errors.invalidPricing('scope', 'variantPrice may only be used inside a variants.expression');
30
+ }
31
+ return;
32
+ case 'rate':
33
+ errors.scope('rate', () => {
34
+ if (pricing.rates[expression.rate] == null) {
35
+ errors.invalidPricing('missing-rate', `rate '${expression.rate}' is not defined`);
36
+ }
37
+ checkTagExpr(expression.tag, insideVariants);
38
+ });
39
+ return;
40
+ case 'variants':
41
+ errors.scope('variants', () => {
42
+ if (insideVariants) {
43
+ errors.invalidPricing('nested-variants', 'variants cannot nest inside variants');
44
+ }
45
+ const { tag } = expression;
46
+ if (tag) {
47
+ errors.scope('tag', () => checkFilterTagExpr(tag));
48
+ }
49
+ errors.scope('expression', () => walkExpr(expression.expression, true));
50
+ });
51
+ return;
52
+ case 'add':
53
+ case 'sub':
54
+ case 'mul':
55
+ case 'div':
56
+ errors.scope('left', () => walkExpr(expression.left, insideVariants));
57
+ errors.scope('right', () => walkExpr(expression.right, insideVariants));
58
+ return;
59
+ }
60
+ }
61
+ function checkTagExpr(tag, insideVariants) {
62
+ if (tag.kind === 'variantTags' && !insideVariants) {
63
+ errors.invalidPricing('scope', 'variantTags may only be used inside a variants.expression');
64
+ }
65
+ }
66
+ function checkFilterTagExpr(tag) {
67
+ if (tag.kind === 'variantTags') {
68
+ errors.invalidPricing('invalid-filter', 'variantTags is not a valid variants.tag filter');
69
+ }
70
+ }
71
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@builder-builder/builder",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {