API Overview
The amqp-contract library consists of core packages and framework integrations:
Core Packages
@amqp-contract/contract
The core package for defining AMQP contracts.
typescript
import { defineContract, defineExchange, defineQueue } from '@amqp-contract/contract';Key exports:
defineContract- Define a complete AMQP contractdefineExchange- Define an exchangedefineQueue- Define a queuedefineBinding- Define a bindingdefinePublisher- Define a publisher with schemadefineConsumer- Define a consumer with schema
@amqp-contract/client
Type-safe client for publishing messages.
typescript
import { TypedAmqpClient } from '@amqp-contract/client';Key exports:
TypedAmqpClient.create- Create a type-safe AMQP clientTypedAmqpClient- The client class type
@amqp-contract/worker
Type-safe worker for consuming messages.
typescript
import { TypedAmqpWorker } from '@amqp-contract/worker';Key exports:
TypedAmqpWorker.create- Create a type-safe AMQP workerTypedAmqpWorker- The worker class type
@amqp-contract/asyncapi
Generate AsyncAPI 3.0 specifications from contracts.
typescript
import { generateAsyncAPI } from '@amqp-contract/asyncapi';Key exports:
generateAsyncAPI- Generate AsyncAPI specification- Various TypeScript types for AsyncAPI documents
NestJS Integration
@amqp-contract/client-nestjs
NestJS integration for the type-safe AMQP client.
typescript
import { AmqpClientModule, AmqpClientService } from '@amqp-contract/client-nestjs';Key exports:
AmqpClientModule- NestJS dynamic module for clientAmqpClientService- Injectable service for publishing messages
Features:
- ✅ Automatic lifecycle management
- ✅ Dependency injection
- ✅ forRoot/forRootAsync configuration
- ✅ Graceful shutdown
@amqp-contract/worker-nestjs
NestJS integration for the type-safe AMQP worker.
typescript
import { AmqpWorkerModule, AmqpWorkerService } from '@amqp-contract/worker-nestjs';Key exports:
AmqpWorkerModule- NestJS dynamic module for workerAmqpWorkerService- Injectable service managing the worker
Features:
- ✅ Automatic lifecycle management
- ✅ Dependency injection in handlers
- ✅ forRoot/forRootAsync configuration
- ✅ Graceful shutdown
Type Inference
All packages leverage TypeScript's type inference:
typescript
import { defineContract, definePublisher } from '@amqp-contract/contract';
import { TypedAmqpClient } from '@amqp-contract/client';
import { TypedAmqpWorker } from '@amqp-contract/worker';
import { connect } from 'amqplib';
import { z } from 'zod';
// Types are inferred from the contract
const contract = defineContract({
publishers: {
orderCreated: definePublisher('orders', z.object({
orderId: z.string(),
})),
},
});
// Connect to RabbitMQ
const connection = await connect('amqp://localhost');
// Client knows about 'orderCreated' and its schema
const client = await TypedAmqpClient.create({ contract, connection });
await client.publish('orderCreated', { orderId: 'ORD-123' });
// Worker handler is fully typed
const worker = await TypedAmqpWorker.create({
contract,
handlers: {
processOrder: async (message) => {
message.orderId; // string
},
},
connection,
});Common Types
ContractDefinition
The base type for all contracts:
typescript
interface ContractDefinition {
exchanges?: Record<string, ExchangeDefinition>;
queues?: Record<string, QueueDefinition>;
bindings?: Record<string, BindingDefinition>;
publishers?: Record<string, PublisherDefinition>;
consumers?: Record<string, ConsumerDefinition>;
}Schema Types
amqp-contract uses Standard Schema, supporting:
- Zod
- Valibot
- ArkType
- Any Standard Schema-compatible library
Next Steps
For Framework-Agnostic Usage
- Explore the @amqp-contract/contract API
- Learn about the @amqp-contract/client API
- Understand the @amqp-contract/worker API
- Check out @amqp-contract/asyncapi generation
For NestJS Applications
- Get started with NestJS Client Usage
- Learn about NestJS Worker Usage
- Explore @amqp-contract/client-nestjs API
- Understand @amqp-contract/worker-nestjs API