Skip to content

Latest commit

 

History

History

README.md

@amqp-contract/client

Type-safe AMQP client for publishing messages using amqp-contract with explicit error handling via Result types.

CI npm version npm downloads TypeScript License: MIT

📖 Full documentation →

Installation

pnpm add @amqp-contract/client

Usage

import { TypedAmqpClient } from "@amqp-contract/client";
import { contract } from "./contract";

// Create client from contract (automatically connects and waits for connection)
const client = (
  await TypedAmqpClient.create({
    contract,
    urls: ["amqp://localhost"],
  })
).unwrap();

// Publish message with explicit error handling
const result = await client.publish("orderCreated", {
  orderId: "ORD-123",
  amount: 99.99,
});
result.match({
  ok: () => console.log("Published successfully"),
  err: (error) => console.error("Publish failed:", error),
  defect: (cause) => {
    throw cause;
  },
});

// Clean up
await client.close();

Error Handling

The client uses Result types from unthrown for explicit error handling. Runtime errors are part of the type signature:

publish(): Result<boolean, TechnicalError | MessageValidationError>

Error Types:

  • TechnicalError - Runtime failures (channel buffer full, network issues, etc.)
  • MessageValidationError - Message fails schema validation

Programming Errors (client not initialized, invalid publisher name) throw exceptions since they indicate bugs caught by TypeScript at compile-time.

API

For complete API documentation, see the Client API Reference.

Documentation

📖 Read the full documentation →

License

MIT