@amqp-contract/core
Core utilities for AMQP setup and management in amqp-contract.
This package provides centralized functionality for establishing AMQP topology (exchanges, queues, and bindings) from contract definitions, and defines the Logger interface used across amqp-contract packages.
Installation
npm install @amqp-contract/core
# or
pnpm add @amqp-contract/core
# or
yarn add @amqp-contract/core
Usage
AmqpClient
The core package exports an AmqpClient class that handles the creation of all AMQP resources defined in a contract.
// contract.ts
import {
defineContract,
defineEventPublisher,
defineEventConsumer,
defineExchange,
defineQueue,
defineQueueBinding,
defineMessage,
} from "@amqp-contract/contract";
import { z } from "zod";
// Define resources
const ordersExchange = defineExchange("orders");
const ordersDlx = defineExchange("orders-dlx");
const orderProcessingQueue = defineQueue("order-processing", {
deadLetter: { exchange: ordersDlx },
});
// A dead-letter exchange with nothing bound to it discards every rejected
// message, so declare the DLQ and bind it. `orders-dlx` is topic, so `#`
// catches whatever routing key the message arrived with.
const orderDlq = defineQueue("order-processing-dlq");
const orderMessage = defineMessage(z.object({ orderId: z.string() }));
const orderCreatedEvent = defineEventPublisher(ordersExchange, orderMessage, {
routingKey: "order.created",
});
// Define your contract
export const contract = defineContract({
publishers: {
orderCreated: orderCreatedEvent,
},
consumers: {
processOrder: defineEventConsumer(orderCreatedEvent, orderProcessingQueue),
},
queues: { orderDlq },
bindings: { orderDlqBinding: defineQueueBinding(orderDlq, ordersDlx, { routingKey: "#" }) },
});
Hand that contract to an AmqpClient, which asserts its topology on connect:
import { AmqpClient } from "@amqp-contract/core";
import { contract } from "./contract.js";
// Setup AMQP resources
const amqpClient = new AmqpClient(contract, {
urls: ["amqp://localhost"],
});
// Clean up — close() returns an AsyncResult with an empty error channel
await amqpClient.close().get();
For advanced channel configuration options (custom setup, prefetch, publisher confirms), see the Channel Configuration Guide.
Logger Interface
The core package exports a Logger interface that can be used to implement custom logging for AMQP operations:
import type { Logger } from "@amqp-contract/core";
const logger: Logger = {
debug: (message, context) => console.debug(message, context),
info: (message, context) => console.info(message, context),
warn: (message, context) => console.warn(message, context),
error: (message, context) => console.error(message, context),
};
// Pass the logger to client or worker
import { TypedAmqpClient } from "@amqp-contract/client";
const client = await TypedAmqpClient.create({
contract,
urls: ["amqp://localhost"],
logger, // Optional: logs published messages
}).getOrThrow();
API
For complete API documentation, see the @amqp-contract/core API Reference.
Documentation
📖 Read the full documentation →
License
MIT