Packages narrative
packages / cart / core
cart / core
Functions
| Name | Description |
|---|---|
runCartAdapterContract | Vitest-bound contract suite that every |
analytics | Built-in middleware that calls the host-supplied |
compose | Koa-style middleware composer: each layer wraps the next in registration order. With |
createCart | Builds a cart kernel bound to one adapter. The kernel is the host-facing surface: it routes typed |
createEventBus | In-process event bus used by the cart kernel to broadcast lifecycle events to UI layers (React store, analytics, tab sync). Delivery is fire-and-forget via |
idempotency | Built-in middleware that deduplicates mutations sharing the same |
logger | Built-in middleware that logs mutation lifecycle events via |
memoryIdempotencyStore | In-memory |
retry | Built-in middleware that retries transport / provider failures with linear backoff. Errors matched by |
tracing | Built-in middleware that wraps each mutation in an OTel-shaped span. Span name is |
createMockCartAdapter | Builds an in-memory cart adapter for host tests and contract self-tests. State lives in a |
Classes
| Name | Description |
|---|---|
CartCapabilityUnsupportedError | Thrown by the kernel when a mutation requires a capability the active adapter doesn't advertise (e.g. gift cards on a provider without them). |
CartError | Base class for every cart-core domain error. Consumers match on |
CartNotFoundError | Thrown by adapters when a cart id is resolvable in the request but no cart exists upstream — distinct from a transport failure. |
CartProviderError | Thrown for transport / upstream-API failures (network, 5xx, malformed payloads). Retry middleware treats this as retryable. |
CartUserError | Thrown when the upstream provider rejects a mutation due to caller-supplied input (e.g. invalid discount code, quantity exceeds stock). Retry middleware does NOT retry these. |
Interfaces
| Name | Description |
|---|---|
RunCartAdapterContractOpts | Options accepted by |
CartAdapter | Contract that every cart provider implementation satisfies. Adapters expose required CRUD operations and declare their optional capabilities so the kernel can gate advanced mutations without probing method presence. |
CartEventBus | In-process pub/sub channel through which the kernel broadcasts cart lifecycle events. Returned by |
CartKernel | Host-facing surface returned by |
CreateCartOpts | Configuration bag passed to |
IdempotencyStore | Backing store contract for the |
ILogger | Minimal structured-logging interface threaded through |
ITracer | Minimal tracing interface accepted by the |
CreateMockCartAdapterOpts | Options for |
Types
| Name | Description |
|---|---|
AdapterCtx | Immutable context threaded into every adapter call by the kernel. Carries shop identity, locale, logger, and optional per-request signals so adapters never reach for global state. |
AnalyticsEmit | Callback injected into the |
BuyerIdentity | Optional contact details attached to an active cart to identify the buyer. Pass to |
Cart | Full cart state returned by the kernel after every read or mutation. The |
CartActionFailureReason | Machine-readable failure code surfaced on the |
CartActionResult | Discriminated result returned by a |
CartCapabilities | Feature flags advertised by every |
CartEvent | Discriminated union of every lifecycle event the cart kernel can emit. Consumers subscribe via |
CartEventHandler | Callback signature for a subscriber to a specific |
CartEventType | Union of the string literal type discriminants for every |
CartExt | Extension slot that lets adapter authors attach arbitrary provider-specific data to |
CartLine | A single line item in a |
CartLineMerchandise | Snapshot of the variant-level product data attached to a cart line. Captured at add-to-cart time so the cart displays correct titles and prices even when the variant is later modified or removed in the catalog. |
CartMiddleware | Higher-order function that wraps a |
CartMutation | Discriminated union of every operation the kernel can route to an adapter. Pass to |
CartUserErrorEntry | A single field-scoped validation error returned by the upstream provider. Consumers display these to users; |
CurrencyCode | ISO 4217 alphabetic currency code (e.g. |
CustomMutationHandler | Handler registered on a |
KV | Generic key-value pair used for cart attributes and line item attributes. Adapters map these to provider-specific metadata fields. |
LocaleTuple | Language-country-currency triple that parameterizes every adapter call. Ensures currency is always co-located with locale so adapters never have to infer it from context. |
Money | Decimal-string money amount paired with its currency code, matching the shape returned by most provider APIs. Pass to |
MoneyCents | Integer-cents money representation used for all arithmetic inside the cart kernel. Avoids floating-point rounding by working in the currency's minor units. Convert back to |
MutationEnvelope | Wraps a |
MutationFn | Core mutation primitive: an async function that accepts a |
NewCartLine | Minimal input needed to add a variant to a cart. Passed to |
ProductSnapshot | Denormalized product data captured at add-to-cart time and carried on the |
SubmitMutation | Server-action signature that transport layers (Next.js, Remix, tRPC) implement to accept a |
Variables
| Name | Description |
|---|---|
consoleLogger | Default |
money | Namespace of pure arithmetic helpers for |