Order Processing Example
A complete e-commerce order processing workflow example with Result/Future pattern.
Overview
This example demonstrates:
- Separated contract package: Contract is in its own package that can be shared
- Result/Future pattern: Explicit error handling with type-safe errors
- Order validation
- Payment processing
- Inventory management
- Email notifications
- Clean Architecture structure
Workflow Flow
Project Structure
The example consists of two packages:
samples/
├── order-processing-contract/ # Contract package (shared)
│ ├── src/
│ │ ├── contract.ts # Contract definition
│ │ ├── schemas.ts # Domain schemas
│ │ └── index.ts # Package exports
│ └── package.json
│
└── order-processing-worker/ # Worker/Client implementation
├── src/
│ ├── application/
│ │ ├── activities.ts # Activity implementations (Result/Future)
│ │ ├── workflows.ts # Workflow implementations
│ │ ├── worker.ts # Worker setup
│ │ └── client.ts # Client example
│ ├── domain/ # Business logic
│ └── infrastructure/ # External adapters
└── package.json # Imports contract packageKey Concepts
Contract Package
The contract is separated into its own package (order-processing-contract) which:
- Can be imported by the worker to implement workflows/activities
- Can be imported by clients (even in other applications) to consume the workflow
- Provides full TypeScript type safety across all boundaries
- Can be versioned and published independently
Result/Future Pattern
This example uses @temporal-contract/boxed for explicit error handling:
- Activities return
Future<Result<T, ActivityError>>instead of throwing - Client methods return
Future<Result<T, TypedClientError>> - Errors are part of the type signature
- Enables railway-oriented programming
Worker Application
The worker application imports the contract package and implements:
- Activities that match the contract signatures with Result/Future pattern
- Workflows that use the contract's type definitions
- Worker setup that registers the implementations
Source Code
View the complete source code:
Running the Example
Prerequisites
- Start Temporal server:bash
temporal server start-dev
Run the Example
bash
# Build the contract package first
cd samples/order-processing-contract
pnpm build
# Run the worker
cd ../order-processing-worker
pnpm dev # Terminal 1 - Start worker
# In another terminal, run the client
cd ../order-processing-client
pnpm dev # Terminal 2 - Run clientBenefits of This Architecture
- Contract Reusability: The contract can be imported by multiple applications
- Type Safety: Full TypeScript support with Result/Future types
- Explicit Error Handling: Errors are part of the type system
- Independent Deployment: Client and worker can be in different repositories
- Clear Separation: Contract definition is separate from implementation
See Examples Overview and Result Pattern Guide for more details.