Pricing and Widgets
Rule Step Shape
src/lib/widgets.ts defines:
RuleStep:{ id, widgetId, config }WidgetManifest: widget metadata, configurable fields, and engine definition.WidgetField:numberorselectfield metadata used by UI and validation.
Default Widget Catalog
DEFAULT_WIDGETS includes:
price.addPercentprice.marginprice.round9price.round5
Current default engines:
price.addPercent->percentAdjustprice.margin->marginFromCostprice.round9->roundNearest9price.round5->roundNearest5
Engine Execution
src/lib/rules.ts:
applyStep(input, step, widgetsById)resolves widget engine and applies one transformation.applyPipeline(start, steps, widgetsById)applies all steps in order and returns intermediate outputs plus final value.money(n)formats USD display strings.
Engine Rules
- Unknown widget ID: step is ignored (input passes through).
- Numeric config values are read using the engine's configured key names.
marginFromCostcomputesinput / (1 - percent/100)and safely passes through when denominator is<= 0.roundNearest9rounds to the nearest whole-dollar value ending in9.roundNearest5rounds to the nearest multiple of5.
Widget Helpers
src/lib/widgets.ts also provides:
createStepFromWidget(widget, id)for generating step defaults from widget field defaults.mapWidgetsById(widgets)for building a lookup map used by execution and UI.
Tier Pricing Integration
- Tier policy parsing and calculation live in
src/lib/tier-pricing-policy.ts. - Products and builder previews combine:
- pipeline final output from
applyPipeline - tier derivation from
computeTierPrices
- pipeline final output from
- See
_docs/tier-pricing-policy.mdfor full tier model details.
Key Separation of Concerns
- Widget schemas/defaults/helpers:
src/lib/widgets.ts - Runtime step math:
src/lib/rules.ts - Tier math and policy parsing:
src/lib/tier-pricing-policy.ts - Step and tier validation when persisting rulesets:
src/lib/s3-rulesets.ts