Skip to content

Shipping Domain

The shipping domain provides shipment lifecycle management, carrier integration, and rate calculation. It tracks shipments from creation through delivery and supports multiple carriers.

Terminal window
npx @backcap/cli add shipping

Represents an order shipment with a state machine: created → dispatched → in_transit → delivered (or canceled).

import { Shipment } from "./domains/shipping/domain/entities/shipment.entity";
const shipment = Shipment.create({
id: crypto.randomUUID(),
orderId: "order-42",
carrierId: "fedex",
status: "created",
originAddress: { street: "1 Main St", city: "Paris", country: "FR", postalCode: "75001" },
destinationAddress: { street: "5 Oak Ave", city: "Lyon", country: "FR", postalCode: "69001" },
});
const dispatched = shipment.unwrap().dispatch("TRACK-123456");

Shipping carrier definitions with supported zones and service types.

import { Carrier } from "./domains/shipping/domain/entities/carrier.entity";
const carrier = Carrier.create({
id: "fedex",
name: "FedEx",
supportedZones: ["EU", "US", "APAC"],
});
  • ShipmentStatuscreated, dispatched, in_transit, delivered, canceled
  • ShippingZone — geographic shipping region
  • TrackingNumber — validated carrier tracking identifier
Use CaseDescription
CreateShipmentInitialize a shipment for an order
DispatchShipmentAssign tracking number, move to dispatched
MarkInTransitUpdate status to in-transit
DeliverShipmentMark as delivered
CancelShipmentCancel with optional reason
GetShipmentRetrieve shipment details
ListShipmentsList shipments with filters
GetRatesGet shipping rates from carrier
EstimateDeliveryCalculate estimated delivery date range
  • IShipmentRepositoryfindById, findByOrderId, save
  • ICarrierRepositoryfindById, findAll
  • IShippingProvidergetRates, estimateDelivery
import { createShippingService } from "./domains/shipping/contracts";
const shipping = createShippingService({
shipmentRepository: myShipmentRepo,
carrierRepository: myCarrierRepo,
shippingProvider: myShippingProvider,
});
const rates = await shipping.getRates({
carrierId: "fedex",
origin: { country: "FR", postalCode: "75001" },
destination: { country: "FR", postalCode: "69001" },
weight: 2.5,
});
EventPayload
ShipmentCreatedshipmentId, orderId, carrierId
ShipmentDispatchedshipmentId, trackingNumber
ShipmentInTransitshipmentId
ShipmentDeliveredshipmentId, deliveredAt
ShipmentCanceledshipmentId, reason