npm.io
1.0.0 • Published 2d ago

@amqp-contract/core

Licence
MIT
Version
1.0.0
Deps
4
Size
223 kB
Vulns
0
Weekly
3.9K

@amqp-contract/core

Core utilities for AMQP setup and management in amqp-contract.

CI npm version npm downloads TypeScript License: MIT

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.

Full documentation →

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.

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");
const orderProcessingQueue = defineQueue("order-processing");
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.

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
  })
).unwrap();

API

For complete API documentation, see the @amqp-contract/core API Reference.

Documentation

Read the full documentation →

License

MIT

Keywords