@classytic/cart 0.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.
Files changed (47) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/LICENSE +21 -0
  3. package/README.md +109 -0
  4. package/dist/bundle.kind-BdgnyKZt.d.mts +114 -0
  5. package/dist/bundle.kind-DFydSxV_.mjs +362 -0
  6. package/dist/cart-event-catalog-BIbi2Q_n.d.mts +109 -0
  7. package/dist/cart-event-catalog-BOP9aDhL.mjs +301 -0
  8. package/dist/cart-status.vo-2iisZGhb.d.mts +46 -0
  9. package/dist/create-cart-q3CXxPP7.mjs +1751 -0
  10. package/dist/domain/errors/index.d.mts +2 -0
  11. package/dist/domain/errors/index.mjs +4 -0
  12. package/dist/domain/events/index.d.mts +3 -0
  13. package/dist/domain/events/index.mjs +3 -0
  14. package/dist/domain/index.d.mts +12 -0
  15. package/dist/domain/index.mjs +15 -0
  16. package/dist/domain/ports/index.d.mts +3 -0
  17. package/dist/domain/ports/index.mjs +2 -0
  18. package/dist/domain/saga/index.d.mts +2 -0
  19. package/dist/domain/saga/index.mjs +3 -0
  20. package/dist/domain-event-DHNkz15G.mjs +57 -0
  21. package/dist/engine/cart-config.schema.d.mts +41 -0
  22. package/dist/engine/cart-config.schema.mjs +40 -0
  23. package/dist/engine/index.d.mts +3 -0
  24. package/dist/engine/index.mjs +3 -0
  25. package/dist/errors-CHR9RlbY.mjs +97 -0
  26. package/dist/id.port-C3rlDAKP.mjs +29 -0
  27. package/dist/id.port-DPXLrqb2.d.mts +64 -0
  28. package/dist/idempotency-key.vo-D0lJsvGn.mjs +9 -0
  29. package/dist/idempotency-record.entity-DIFXjN1V.d.mts +221 -0
  30. package/dist/index-DEHBdV_z.d.mts +1 -0
  31. package/dist/index-Doi5uzux.d.mts +231 -0
  32. package/dist/index.d.mts +103 -0
  33. package/dist/index.mjs +92 -0
  34. package/dist/internal-event-bus-IkUfQJxn.d.mts +661 -0
  35. package/dist/kind-registry-Ba4PO4WC.mjs +35 -0
  36. package/dist/kind.error-CCjTB6kW.mjs +69 -0
  37. package/dist/kinds/index.d.mts +2 -0
  38. package/dist/kinds/index.mjs +2 -0
  39. package/dist/line-item.entity-B3M_i-pw.d.mts +157 -0
  40. package/dist/line-kind-Bndo6IS8.d.mts +231 -0
  41. package/dist/not-found.error-B6WYnAjX.mjs +95 -0
  42. package/dist/outbox-store-CTWz9pvG.d.mts +155 -0
  43. package/dist/saga-step-fn-Cyob38iU.d.mts +135 -0
  44. package/dist/saga-steps-B27LI7xp.mjs +27 -0
  45. package/dist/shipping.bridge-DD4A_O0O.d.mts +538 -0
  46. package/dist/state-transitions-CpJpZZTK.mjs +21 -0
  47. package/package.json +114 -0
@@ -0,0 +1,155 @@
1
+ import { Money } from "@classytic/primitives/money";
2
+ import { DomainEvent, EventMeta } from "@classytic/primitives/events";
3
+ import { InvalidOutboxEventError, OutboxAcknowledgeOptions, OutboxAcknowledgeOptions as OutboxAcknowledgeOptions$1, OutboxClaimOptions, OutboxErrorInfo, OutboxFailOptions, OutboxFailureContext, OutboxFailureDecision, OutboxFailurePolicy, OutboxOwnershipError, OutboxStore, OutboxStore as OutboxStore$1, OutboxWriteOptions, OutboxWriteOptions as OutboxWriteOptions$1 } from "@classytic/primitives/outbox";
4
+
5
+ //#region src/domain/events/cart-events.d.ts
6
+ /**
7
+ * Cart event name + payload map.
8
+ *
9
+ * Minimal surface. Cart emits events for its own lifecycle transitions
10
+ * only — draft mutations, checkout creation, and checkout commit/cancel.
11
+ * Payment, order creation, and fulfillment live in the host, so cart
12
+ * emits no events for them.
13
+ *
14
+ * For registration with an arc-compatible event registry, see the
15
+ * `cart-event-catalog.ts` file in this directory — it exposes each event
16
+ * as a `CartEventDefinition` via `defineCartEvent()`.
17
+ */
18
+ declare const CartEvents: {
19
+ readonly DRAFT_CREATED: "cart.draft.created";
20
+ readonly DRAFT_ITEM_ADDED: "cart.draft.item_added";
21
+ readonly DRAFT_ITEM_UPDATED: "cart.draft.item_updated";
22
+ readonly DRAFT_ITEM_REMOVED: "cart.draft.item_removed";
23
+ readonly DRAFT_PROMO_APPLIED: "cart.draft.promo_applied";
24
+ readonly DRAFT_MERGED: "cart.draft.merged";
25
+ readonly DRAFT_ABANDONED: "cart.draft.abandoned";
26
+ readonly DRAFT_EXPIRED: "cart.draft.expired";
27
+ readonly CHECKOUT_CREATED: "cart.checkout.created";
28
+ readonly CHECKOUT_COMMITTED: "cart.checkout.committed";
29
+ readonly CHECKOUT_CANCELED: "cart.checkout.canceled";
30
+ };
31
+ type CartEventName = (typeof CartEvents)[keyof typeof CartEvents];
32
+ interface CartEventPayloadBase {
33
+ organizationId: string;
34
+ actorRef: string;
35
+ correlationId: string;
36
+ emittedAt: string;
37
+ }
38
+ interface CartEventMap {
39
+ [CartEvents.DRAFT_CREATED]: CartEventPayloadBase & {
40
+ draftId: string;
41
+ };
42
+ [CartEvents.DRAFT_ITEM_ADDED]: CartEventPayloadBase & {
43
+ draftId: string;
44
+ lineId: string;
45
+ kind: string;
46
+ quantity: number;
47
+ };
48
+ [CartEvents.DRAFT_ITEM_UPDATED]: CartEventPayloadBase & {
49
+ draftId: string;
50
+ lineId: string;
51
+ quantity: number;
52
+ };
53
+ [CartEvents.DRAFT_ITEM_REMOVED]: CartEventPayloadBase & {
54
+ draftId: string;
55
+ lineId: string;
56
+ };
57
+ [CartEvents.DRAFT_PROMO_APPLIED]: CartEventPayloadBase & {
58
+ draftId: string;
59
+ code: string;
60
+ accepted: boolean;
61
+ };
62
+ [CartEvents.DRAFT_MERGED]: CartEventPayloadBase & {
63
+ sourceDraftId: string;
64
+ targetDraftId: string;
65
+ mergedLineCount: number;
66
+ };
67
+ [CartEvents.DRAFT_ABANDONED]: CartEventPayloadBase & {
68
+ draftId: string;
69
+ abandonedAt: string;
70
+ lineCount: number;
71
+ grandTotal: Money;
72
+ };
73
+ [CartEvents.DRAFT_EXPIRED]: CartEventPayloadBase & {
74
+ draftId: string;
75
+ };
76
+ [CartEvents.CHECKOUT_CREATED]: CartEventPayloadBase & {
77
+ checkoutId: string;
78
+ draftId: string;
79
+ grandTotal: Money;
80
+ };
81
+ [CartEvents.CHECKOUT_COMMITTED]: CartEventPayloadBase & {
82
+ checkoutId: string;
83
+ draftId: string;
84
+ externalRef: string | null;
85
+ grandTotal: Money;
86
+ };
87
+ [CartEvents.CHECKOUT_CANCELED]: CartEventPayloadBase & {
88
+ checkoutId: string;
89
+ reason: string;
90
+ };
91
+ }
92
+ type CartEventPayload<E extends CartEventName> = CartEventMap[E];
93
+ //#endregion
94
+ //#region src/domain/events/domain-event.d.ts
95
+ /**
96
+ * Cart-specific aggregate types. Used in event meta so subscribers can
97
+ * route by the mutated aggregate without parsing the event name.
98
+ */
99
+ type CartAggregateType = 'draft' | 'checkout' | 'reservation' | 'line_item';
100
+ interface CartEventAggregate {
101
+ type: CartAggregateType;
102
+ id: string;
103
+ }
104
+ /**
105
+ * Cart event meta = primitives' `EventMeta` plus cart-specific extensions.
106
+ *
107
+ * - `version?` — payload schema version; bump when the payload shape changes.
108
+ * - `causationId?` — id of the upstream event that caused this one
109
+ * (distributed tracing).
110
+ * - `aggregate?` — the aggregate this event mutates, as `(type, id)`.
111
+ */
112
+ interface CartEventMeta extends EventMeta {
113
+ version?: number;
114
+ causationId?: string;
115
+ aggregate?: CartEventAggregate;
116
+ }
117
+ /**
118
+ * Cart event = primitives' `DomainEvent<T>` with the richer `CartEventMeta`.
119
+ * Structurally a subtype of `DomainEvent<T>` — any transport accepting
120
+ * `DomainEvent` accepts `CartDomainEvent` too.
121
+ */
122
+ interface CartDomainEvent<TPayload = unknown> extends DomainEvent<TPayload> {
123
+ meta: CartEventMeta;
124
+ }
125
+ /**
126
+ * Construct a {@link CartDomainEvent} with auto-filled id + timestamp
127
+ * (delegated to primitives' {@link createPrimitiveEvent createEvent}) plus
128
+ * cart-specific `version` / `aggregate` / `causationId`. Callers typically go
129
+ * through `defineCartEvent()` rather than calling this directly.
130
+ */
131
+ declare function createCartEvent<T>(type: string, payload: T, meta?: Partial<CartEventMeta>, version?: number): CartDomainEvent<T>;
132
+ //#endregion
133
+ //#region src/domain/events/outbox-store.d.ts
134
+ /**
135
+ * Backwards-compatible alias. Prefer `OutboxWriteOptions` from
136
+ * `@classytic/primitives/outbox` in new code.
137
+ */
138
+ type OutboxStoreWriteOptions = OutboxWriteOptions;
139
+ /**
140
+ * In-memory OutboxStore for tests and local dev. Not for production.
141
+ *
142
+ * Implements the required trio (`save` / `getPending` / `acknowledge`) plus
143
+ * optional `purge`. The lease-based methods (`claimPending` / `fail` /
144
+ * `getDeadLettered`) are intentionally omitted — single-process tests don't
145
+ * need them, and cart's publisher only calls `save()`.
146
+ */
147
+ declare class MemoryOutboxStore implements OutboxStore {
148
+ private events;
149
+ save(event: DomainEvent, _options?: OutboxWriteOptions): Promise<void>;
150
+ getPending(limit: number): Promise<DomainEvent[]>;
151
+ acknowledge(eventId: string, _options?: OutboxAcknowledgeOptions): Promise<void>;
152
+ purge(olderThanMs: number): Promise<number>;
153
+ }
154
+ //#endregion
155
+ export { CartEvents as S, CartEventMeta as _, OutboxErrorInfo as a, CartEventName as b, OutboxFailureDecision as c, OutboxStore$1 as d, OutboxStoreWriteOptions as f, CartEventAggregate as g, CartDomainEvent as h, OutboxClaimOptions as i, OutboxFailurePolicy as l, CartAggregateType as m, MemoryOutboxStore as n, OutboxFailOptions as o, OutboxWriteOptions$1 as p, OutboxAcknowledgeOptions$1 as r, OutboxFailureContext as s, InvalidOutboxEventError as t, OutboxOwnershipError as u, createCartEvent as v, CartEventPayload as x, CartEventMap as y };
@@ -0,0 +1,135 @@
1
+ import { t as CheckoutState } from "./cart-status.vo-2iisZGhb.mjs";
2
+ import { x as OperationContext } from "./shipping.bridge-DD4A_O0O.mjs";
3
+ import { t as KindBridges } from "./line-kind-Bndo6IS8.mjs";
4
+ import { i as ClockPort, t as IdPort } from "./id.port-DPXLrqb2.mjs";
5
+
6
+ //#region src/domain/saga/saga-steps.d.ts
7
+ /**
8
+ * Checkout flow steps. Deliberately minimal — cart's job is to freeze a
9
+ * snapshot and transition its state. Everything external (payment, order
10
+ * creation, fulfillment) is the host's problem.
11
+ *
12
+ * This enum exists purely for logging / metrics / ordered state-transition
13
+ * dispatch. Cart does not run a "saga engine" — the `CheckoutRepository`
14
+ * domain methods run these steps in plain sequential code and let
15
+ * exceptions propagate. Compensation is one operation (`cancel`) rather
16
+ * than a reverse walk of a multi-step chain.
17
+ */
18
+ declare const CheckoutStep: {
19
+ /** Step 1 — load draft + validate status + version. */readonly LoadDraft: 1; /** Step 2 — re-run kind.revalidate() on stale lines. */
20
+ readonly Revalidate: 2; /** Step 3 — re-run pricing pipeline, compare expectedPricingHash. */
21
+ readonly Reprice: 3; /** Step 4 — create the checkout document with the frozen snapshot. */
22
+ readonly CreateCheckout: 4; /** Step 5 — commit (host-called after its own external work succeeds). */
23
+ readonly Commit: 5;
24
+ };
25
+ type CheckoutStepNumber = (typeof CheckoutStep)[keyof typeof CheckoutStep];
26
+ declare const SagaStep: {
27
+ /** Step 1 — load draft + validate status + version. */readonly LoadDraft: 1; /** Step 2 — re-run kind.revalidate() on stale lines. */
28
+ readonly Revalidate: 2; /** Step 3 — re-run pricing pipeline, compare expectedPricingHash. */
29
+ readonly Reprice: 3; /** Step 4 — create the checkout document with the frozen snapshot. */
30
+ readonly CreateCheckout: 4; /** Step 5 — commit (host-called after its own external work succeeds). */
31
+ readonly Commit: 5;
32
+ };
33
+ type SagaStepNumber = CheckoutStepNumber;
34
+ //#endregion
35
+ //#region src/domain/saga/saga-state.d.ts
36
+ /**
37
+ * Transient checkout flow state stored on the Checkout document.
38
+ *
39
+ * In the simplified later model, the flow is short (load → revalidate →
40
+ * reprice → create → commit/cancel) and each method is an atomic service
41
+ * call. There is no long-running saga, so this state is almost
42
+ * decorative — it records the last successful step for diagnostics.
43
+ *
44
+ * later brings back a richer state machine when reservation + probe recovery
45
+ * land as separate opt-in primitives, but even then it will not grow
46
+ * back into the 11-step orchestration the previous design carried.
47
+ */
48
+ interface CheckoutFlowState {
49
+ /** Last successful step — set by the CheckoutRepository method that ran it. */
50
+ lastStep: CheckoutStepNumber;
51
+ /** ISO timestamp of the step that last ran. */
52
+ updatedAt: string;
53
+ /** ISO timestamp of the first step (checkout creation). */
54
+ startedAt: string;
55
+ }
56
+ type SagaState = CheckoutFlowState;
57
+ //#endregion
58
+ //#region src/domain/saga/state-transitions.d.ts
59
+ /**
60
+ * Frozen state transitions for Checkout.state. Three states, two
61
+ * terminal outcomes — finalized (success) or canceled (failure / manual
62
+ * cancel / abandonment).
63
+ *
64
+ * Enforced by `CheckoutRepository.updateWithVersion` callers: the
65
+ * service methods pass the expected current state in the filter so
66
+ * racing writers see exactly one winner.
67
+ */
68
+ declare const CHECKOUT_STATE_TRANSITIONS: Record<CheckoutState, readonly CheckoutState[]>;
69
+ /** Predicate — is `to` a legal transition from `from`? */
70
+ declare function isLegalCheckoutTransition(from: CheckoutState, to: CheckoutState): boolean;
71
+ //#endregion
72
+ //#region src/domain/saga/saga-step-fn.d.ts
73
+ /**
74
+ * SagaStepFn — one saga step as an opaque activity.
75
+ *
76
+ * This is a **primitive**, not a runtime. Cart exposes step functions so
77
+ * hosts can compose them with whatever executor they already run:
78
+ *
79
+ * - Single-process apps call cart's in-process checkout service
80
+ * , which simply runs the step functions in order.
81
+ * - Distributed apps wrap each step as a job on their existing queue
82
+ * (BullMQ, SQS, Kafka) — cart has no opinion on transport.
83
+ * - Temporal / Restate / Inngest hosts register each step as an
84
+ * activity and write the workflow coordination themselves.
85
+ * - Agentic hosts expose each step as a tool their LLM agent can invoke
86
+ * via MCP.
87
+ *
88
+ * Cart does not ship a `SagaOrchestrator` interface, a Temporal integration
89
+ * layer, or a BullMQ adapter. Those are app-level concerns. Cart ships the
90
+ * step functions themselves — pure, serialisable-input, serialisable-output
91
+ * activities that any executor can run.
92
+ *
93
+ * Each step is idempotent under `ctx.idempotencyKey`. On retry with the
94
+ * same key, cart's composite idempotency store (see plan §2) replays the
95
+ * exact previous result.
96
+ */
97
+ interface SagaStepFn<TInput, TOutput> {
98
+ /** Step number in the fixed 11-step sequence. */
99
+ readonly step: SagaStepNumber;
100
+ /** Stable name for logging / metrics / activity registration. */
101
+ readonly name: string;
102
+ /**
103
+ * Recovery classification. `requires-probe` steps call
104
+ * `bridge.status(ref)` before retry on restart. See plan §3 — Saga
105
+ * Recovery Contract.
106
+ */
107
+ readonly recovery: 'retry-safe-blind' | 'requires-probe';
108
+ /**
109
+ * Run the step forward. Takes a serializable input and returns a
110
+ * serializable output. Idempotent under `ctx.idempotencyKey`.
111
+ */
112
+ execute(input: TInput, deps: SagaDependencies, ctx: OperationContext): Promise<TOutput>;
113
+ /**
114
+ * Compensate a previously successful execution. Called by whatever
115
+ * executor the host uses when a later step fails. Must be idempotent —
116
+ * already-compensated outputs should be no-ops.
117
+ */
118
+ compensate?(output: TOutput, deps: SagaDependencies, ctx: OperationContext): Promise<void>;
119
+ }
120
+ /**
121
+ * The dependency bag each step receives. Populated by the engine at
122
+ * construction time; hosts running steps outside the engine construct
123
+ * their own equivalent.
124
+ *
125
+ * Concrete step input / output types land in later alongside the default
126
+ * implementations. For P0, this interface documents that steps see
127
+ * bridges, clock, and id — nothing else.
128
+ */
129
+ interface SagaDependencies {
130
+ readonly bridges: KindBridges;
131
+ readonly clock: ClockPort;
132
+ readonly id: IdPort;
133
+ }
134
+ //#endregion
135
+ export { CheckoutFlowState as a, CheckoutStepNumber as c, isLegalCheckoutTransition as i, SagaStep as l, SagaStepFn as n, SagaState as o, CHECKOUT_STATE_TRANSITIONS as r, CheckoutStep as s, SagaDependencies as t, SagaStepNumber as u };
@@ -0,0 +1,27 @@
1
+ //#region src/domain/saga/saga-steps.ts
2
+ /**
3
+ * Checkout flow steps. Deliberately minimal — cart's job is to freeze a
4
+ * snapshot and transition its state. Everything external (payment, order
5
+ * creation, fulfillment) is the host's problem.
6
+ *
7
+ * This enum exists purely for logging / metrics / ordered state-transition
8
+ * dispatch. Cart does not run a "saga engine" — the `CheckoutRepository`
9
+ * domain methods run these steps in plain sequential code and let
10
+ * exceptions propagate. Compensation is one operation (`cancel`) rather
11
+ * than a reverse walk of a multi-step chain.
12
+ */
13
+ const CheckoutStep = {
14
+ /** Step 1 — load draft + validate status + version. */
15
+ LoadDraft: 1,
16
+ /** Step 2 — re-run kind.revalidate() on stale lines. */
17
+ Revalidate: 2,
18
+ /** Step 3 — re-run pricing pipeline, compare expectedPricingHash. */
19
+ Reprice: 3,
20
+ /** Step 4 — create the checkout document with the frozen snapshot. */
21
+ CreateCheckout: 4,
22
+ /** Step 5 — commit (host-called after its own external work succeeds). */
23
+ Commit: 5
24
+ };
25
+ const SagaStep = CheckoutStep;
26
+ //#endregion
27
+ export { SagaStep as n, CheckoutStep as t };