Packages narrative
packages / cart / core

cart / core

Functions

NameDescription
runCartAdapterContractVitest-bound contract suite that every
analyticsBuilt-in middleware that calls the host-supplied
composeKoa-style middleware composer: each layer wraps the next in registration order. With
createCartBuilds a cart kernel bound to one adapter. The kernel is the host-facing surface: it routes typed
createEventBusIn-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
idempotencyBuilt-in middleware that deduplicates mutations sharing the same
loggerBuilt-in middleware that logs mutation lifecycle events via
memoryIdempotencyStoreIn-memory
retryBuilt-in middleware that retries transport / provider failures with linear backoff. Errors matched by
tracingBuilt-in middleware that wraps each mutation in an OTel-shaped span. Span name is
createMockCartAdapterBuilds an in-memory cart adapter for host tests and contract self-tests. State lives in a

Classes

NameDescription
CartCapabilityUnsupportedErrorThrown by the kernel when a mutation requires a capability the active adapter doesn't advertise (e.g. gift cards on a provider without them).
CartErrorBase class for every cart-core domain error. Consumers match on
CartNotFoundErrorThrown by adapters when a cart id is resolvable in the request but no cart exists upstream — distinct from a transport failure.
CartProviderErrorThrown for transport / upstream-API failures (network, 5xx, malformed payloads). Retry middleware treats this as retryable.
CartUserErrorThrown 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

NameDescription
RunCartAdapterContractOptsOptions accepted by
CartAdapterContract 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.
CartEventBusIn-process pub/sub channel through which the kernel broadcasts cart lifecycle events. Returned by
CartKernelHost-facing surface returned by
CreateCartOptsConfiguration bag passed to
IdempotencyStoreBacking store contract for the
ILoggerMinimal structured-logging interface threaded through
ITracerMinimal tracing interface accepted by the
CreateMockCartAdapterOptsOptions for

Types

NameDescription
AdapterCtxImmutable 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.
AnalyticsEmitCallback injected into the
BuyerIdentityOptional contact details attached to an active cart to identify the buyer. Pass to
CartFull cart state returned by the kernel after every read or mutation. The
CartActionFailureReasonMachine-readable failure code surfaced on the
CartActionResultDiscriminated result returned by a
CartCapabilitiesFeature flags advertised by every
CartEventDiscriminated union of every lifecycle event the cart kernel can emit. Consumers subscribe via
CartEventHandlerCallback signature for a subscriber to a specific
CartEventTypeUnion of the string literal type discriminants for every
CartExtExtension slot that lets adapter authors attach arbitrary provider-specific data to
CartLineA single line item in a
CartLineMerchandiseSnapshot 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.
CartMiddlewareHigher-order function that wraps a
CartMutationDiscriminated union of every operation the kernel can route to an adapter. Pass to
CartUserErrorEntryA single field-scoped validation error returned by the upstream provider. Consumers display these to users;
CurrencyCodeISO 4217 alphabetic currency code (e.g.
CustomMutationHandlerHandler registered on a
KVGeneric key-value pair used for cart attributes and line item attributes. Adapters map these to provider-specific metadata fields.
LocaleTupleLanguage-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.
MoneyDecimal-string money amount paired with its currency code, matching the shape returned by most provider APIs. Pass to
MoneyCentsInteger-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
MutationEnvelopeWraps a
MutationFnCore mutation primitive: an async function that accepts a
NewCartLineMinimal input needed to add a variant to a cart. Passed to
ProductSnapshotDenormalized product data captured at add-to-cart time and carried on the
SubmitMutationServer-action signature that transport layers (Next.js, Remix, tRPC) implement to accept a

Variables

NameDescription
consoleLoggerDefault
moneyNamespace of pure arithmetic helpers for

On this page