@amqp-contract/core

中文文档
3.0.0发布于 2026/11/12MITTypeScriptESM + CJS构建来源已验证近 30 天下载 1.2万

Core utilities for AMQP setup and management in amqp-contract

原始使用文档
文档目录7 节
  1. Installation
  2. Usage
  3. AmqpClient
  4. Logger Interface
  5. API
  6. Documentation
  7. License

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

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

版本与发布记录

@amqp-contract/core 共发布 51 个版本,首次发布于 2025/12/23;过去 12 个月发布了 51 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.0.0latest2026/11/12今天
3.0.0-beta.13beta2026/10/2717 天前
3.0.0-beta.122026/10/2320 天前
3.0.0-beta.112026/10/061 个月前
3.0.0-beta.102026/09/301 个月前
3.0.0-beta.92026/09/281 个月前

关于 @amqp-contract/core 的常见问题

@amqp-contract/core 是什么?

Core utilities for AMQP setup and management in amqp-contract

如何安装 @amqp-contract/core?

在项目目录运行 npm install @amqp-contract/core,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:unthrown(^5.3.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@amqp-contract/core 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@amqp-contract/core 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@amqp-contract/core 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@amqp-contract/core 可以商用吗?

@amqp-contract/core 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@amqp-contract/core 的下载量有多少?

根据 npm 官方下载统计,@amqp-contract/core 在 2026/10/06 – 2026/11/04 这 30 天内被下载 12,441 次,日均约 415 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@amqp-contract/core 还在维护吗?

最新版本 3.0.0 发布于 2026/11/12(今天);至今共发布 51 个版本,其中过去 12 个月发布了 51 个;首次发布于 2025/12/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。