Shared OpenAPI specifications, TypeScript DTOs, RabbitMQ message contracts, and typed REST clients for the @demo microservice platform.
Every other repo in the monorepo imports from here. All exported names are a frozen public API — renaming anything is a breaking change that requires a coordinated deploy.
| Path | Contents |
|---|---|
openapi/ |
OpenAPI 3.1 YAML specs — one per domain service |
src/dto/ |
Hand-written TypeScript interfaces matching each OpenAPI schema |
src/messages/ |
MessageEnvelope<T>, saga commands/replies, choreography events, QUEUES/EXCHANGES/ROUTING_KEYS const maps |
src/clients/ |
Typed fetch clients generated from the OpenAPI specs (zero runtime deps) |
src/clients/generated/ |
Auto-generated — do not edit by hand; run npm run generate:clients |
After install, run npm run build in this repo once to produce the dist/ folder that consuming services reference.
// All DTOs
import type { User, Order, OrderStatus, Paginated } from '@demo/contracts/dto';
// All message contracts
import { createEnvelope, QUEUES, ROUTING_KEYS } from '@demo/contracts/messages';
import type { MessageEnvelope, ReserveInventoryPayload } from '@demo/contracts/messages';
// Typed HTTP clients
import { createUserServiceClient } from '@demo/contracts/clients';
// Root barrel (convenience — prefer subpaths in production code)
import type { User } from '@demo/contracts';Each client is created per-request so you can inject a correlationId header for distributed tracing:
import { createOrderServiceClient } from '@demo/contracts/clients';
const orders = createOrderServiceClient({
baseUrl: 'http://order-service:3000',
defaultHeaders: { 'x-correlation-id': req.headers['x-correlation-id'] },
});
const order = await orders.createOrder({ items: [{ catalogItemId: 'abc', quantity: 2 }] });FetchError is thrown on non-2xx responses; catch it to access status, statusText, and body.
Run this whenever you update an OpenAPI YAML:
npm run generate:clientsThis runs openapi-typescript (no Java needed) against each openapi/*.yaml and writes typed paths/components definitions into src/clients/generated/. Commit the generated files — they are the public API surface.
The prebuild npm lifecycle hook runs this automatically before tsc, so npm run build always uses fresh types.
npm install
npm run build # prebuild: generate:clients → tsc
npm test # vitest (envelope round-trip + OpenAPI structure tests)
npm run lint:openapi # redocly lint on all 6 YAML specsEvery message is wrapped in MessageEnvelope<T>:
interface MessageEnvelope<T> {
messageId: string; // UUIDv4 — dedup key for idempotent consumers
correlationId: string; // propagated cf-ray / x-correlation-id
sagaId?: string;
step?: string; // saga step name for idempotent keying
type: string; // routing key
occurredAt: string; // ISO 8601
payload: T;
}order-service ──[reserve-inventory]──▶ catalog-service
│
◀──[inventory-reserved]───────┘
◀──[inventory-rejected]───────┘ (→ CANCELLED)
order-service ──[charge]────────────▶ payment-service
│
◀──[payment-succeeded]────────┘ (→ CONFIRMED)
◀──[payment-failed]───────────┘ (→ release-inventory → CANCELLED)
Compensations on failure:
inventory-rejected→ skip payment, mark CANCELLEDpayment-failed→release-inventory→ mark CANCELLED
| Event | Consumer |
|---|---|
order-confirmed |
notification-service |
order-cancelled |
notification-service |