Core utilities for AMQP setup and management in amqp-contract
npm install @amqp-contract/coreCore 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.
``bash`
npm install @amqp-contract/coreor
pnpm add @amqp-contract/coreor
yarn add @amqp-contract/core
The core package exports an AmqpClient class that handles the creation of all AMQP resources defined in a contract.
`typescript
import { AmqpClient } from "@amqp-contract/core";
import {
defineContract,
defineEventPublisher,
defineEventConsumer,
defineExchange,
defineQueue,
defineMessage,
} from "@amqp-contract/contract";
import { z } from "zod";
// Define resources
const ordersExchange = defineExchange("orders", "topic", { durable: true });
const orderProcessingQueue = defineQueue("order-processing", { durable: true });
const orderMessage = defineMessage(z.object({ orderId: z.string() }));
const orderCreatedEvent = defineEventPublisher(ordersExchange, orderMessage, {
routingKey: "order.created",
});
// Define your contract
const contract = defineContract({
publishers: {
orderCreated: orderCreatedEvent,
},
consumers: {
processOrder: defineEventConsumer(orderCreatedEvent, orderProcessingQueue),
},
});
// Setup AMQP resources
const amqpClient = new AmqpClient(contract, {
urls: ["amqp://localhost"],
});
// Clean up
await amqpClient.close();
`
For advanced channel configuration options (custom setup, prefetch, publisher confirms), see the Channel Configuration Guide.
The core package exports a Logger interface that can be used to implement custom logging for AMQP operations:
`typescript
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
});
``
For complete API documentation, see the @amqp-contract/core API Reference.
š Read the full documentation ā
MIT