Type-safe AMQP client for publishing messages using amqp-contract with explicit error handling via Result types.
pnpm add @amqp-contract/clientimport { 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();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.
For complete API documentation, see the Client API Reference.
📖 Read the full documentation →
MIT