Skip to content
burrowmartPublic

About

Shared OpenAPI specifications, TypeScript DTOs, RabbitMQ message contracts, and typed REST clients for the `@demo` microservice platform.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

@demo/contracts

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.


What lives here

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

Installation (git dependency)

// package.json of a consuming service
{
  "dependencies": {
    "@demo/contracts": "git+https://github.com/<org>/contracts.git#main"
  }
}

After install, run npm run build in this repo once to produce the dist/ folder that consuming services reference.


Subpath exports

// 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';

Using the typed clients

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.


Regenerating clients

Run this whenever you update an OpenAPI YAML:

npm run generate:clients

This 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.


Build & test

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 specs

RabbitMQ message contracts

Envelope

Every 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;
}

Saga flow (order-placement)

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 CANCELLED
  • payment-failed → release-inventory → mark CANCELLED

Choreographed events (after terminal status)

Event Consumer
order-confirmed notification-service
order-cancelled notification-service

About

Shared OpenAPI specifications, TypeScript DTOs, RabbitMQ message contracts, and typed REST clients for the `@demo` microservice platform.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages