@amqp-contract/client-nestjs
@amqp-contract/client-nestjs
Classes
AmqpClientModule
Defined in: packages/client-nestjs/src/client.module.ts:93
NestJS module for AMQP client integration This module provides type-safe AMQP client functionality using @amqp-contract/client without relying on NestJS decorators (except for dependency injection)
Type Param
The contract definition type for type-safe publishing
Example
// Synchronous configuration
@Module({
imports: [
AmqpClientModule.forRoot({
contract: myContract,
urls: ['amqp://localhost']
})
]
})
export class AppModule {}
// Asynchronous configuration
@Module({
imports: [
AmqpClientModule.forRootAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
contract: myContract,
urls: configService.get('AMQP_URLS')
}),
inject: [ConfigService]
})
]
})
export class AppModule {}
// Using the client in a service
@Injectable()
export class OrderService {
constructor(
private readonly amqpClient: AmqpClientService<typeof myContract>
) {}
async createOrder(order: Order) {
// publish is fully typed based on the contract
await this.amqpClient.publish('orderCreated', {
orderId: order.id,
amount: order.total
}).resultToPromise();
}
}Constructors
Constructor
new AmqpClientModule(): AmqpClientModule;Returns
Methods
forRoot()
static forRoot<TContract>(options): DynamicModule;Defined in: packages/client-nestjs/src/client.module.ts:100
Register the AMQP client module with synchronous configuration
Type Parameters
| Type Parameter |
|---|
TContract extends ContractDefinition |
Parameters
| Parameter | Type | Description |
|---|---|---|
options | AmqpClientModuleOptions<TContract> | The client configuration options with contract |
Returns
DynamicModule
A dynamic module for NestJS
forRootAsync()
static forRootAsync<TContract>(options): DynamicModule;Defined in: packages/client-nestjs/src/client.module.ts:122
Register the AMQP client module with asynchronous configuration
Type Parameters
| Type Parameter |
|---|
TContract extends ContractDefinition |
Parameters
| Parameter | Type | Description |
|---|---|---|
options | AmqpClientModuleAsyncOptions<TContract> | Async configuration options with factory function |
Returns
DynamicModule
A dynamic module for NestJS
AmqpClientService
Defined in: packages/client-nestjs/src/client.service.ts:86
Type-safe AMQP client service for NestJS applications.
This service wraps TypedAmqpClient and integrates it with the NestJS lifecycle, automatically initializing the connection on module init and cleaning up resources on module destroy.
Example
// In your module
import { AmqpClientModule } from '@amqp-contract/client-nestjs';
@Module({
imports: [
AmqpClientModule.forRoot({
contract: myContract,
urls: ['amqp://localhost']
})
]
})
export class AppModule {}
// In your service
import { AmqpClientService } from '@amqp-contract/client-nestjs';
@Injectable()
export class OrderService {
constructor(
private readonly amqpClient: AmqpClientService<typeof myContract>
) {}
async createOrder(order: Order) {
const result = await this.amqpClient.publish('orderCreated', {
orderId: order.id,
amount: order.total
}).resultToPromise();
if (result.isError()) {
throw new Error('Failed to publish order event');
}
}
}Type Parameters
| Type Parameter | Description |
|---|---|
TContract extends ContractDefinition | The contract definition type |
Implements
OnModuleInitOnModuleDestroy
Constructors
Constructor
new AmqpClientService<TContract>(options): AmqpClientService<TContract>;Defined in: packages/client-nestjs/src/client.service.ts:91
Parameters
| Parameter | Type |
|---|---|
options | AmqpClientModuleOptions<TContract> |
Returns
AmqpClientService<TContract>
Methods
onModuleDestroy()
onModuleDestroy(): Promise<void>;Defined in: packages/client-nestjs/src/client.service.ts:118
Close the AMQP client when the NestJS module is destroyed.
This lifecycle hook ensures proper cleanup of resources when the NestJS application shuts down, gracefully closing the connection and cleaning up all consumers.
Returns
Promise<void>
Implementation of
OnModuleDestroy.onModuleDestroyonModuleInit()
onModuleInit(): Promise<void>;Defined in: packages/client-nestjs/src/client.service.ts:103
Initialize the AMQP client when the NestJS module starts.
This lifecycle hook automatically creates and initializes the client when the NestJS application starts up. The connection will be established in the background with automatic reconnection handling.
Returns
Promise<void>
Implementation of
OnModuleInit.onModuleInitpublish()
publish<TName>(
publisherName,
message,
options?): Future<Result<void,
| TechnicalError
| MessageValidationError>>;Defined in: packages/client-nestjs/src/client.service.ts:148
Publish a message using a contract-defined publisher.
This method provides type-safe message publishing with automatic validation and explicit error handling through the Result type.
Type Parameters
| Type Parameter |
|---|
TName extends string | number | symbol |
Parameters
| Parameter | Type | Description |
|---|---|---|
publisherName | TName | The name of the publisher from the contract |
message | ClientInferPublisherInput<TContract, TName> | The message payload (type-checked against the contract) |
options? | Publish | Optional AMQP publish options (e.g., persistence, headers) |
Returns
Future<Result<void, | TechnicalError | MessageValidationError>>
A Future that resolves to a Result indicating success or failure
Example
const result = await this.amqpClient.publish('orderCreated', {
orderId: '123',
amount: 99.99
}).resultToPromise();
if (result.isError()) {
console.error('Publish failed:', result.error);
}Interfaces
MessageValidationError
Defined in: packages/client/dist/index.d.mts:26
Error thrown when message validation fails
Extends
ClientError
Properties
TechnicalError
Defined in: packages/client/dist/index.d.mts:19
Error for technical/runtime failures that cannot be prevented by TypeScript This includes validation failures and AMQP channel issues
Extends
ClientError
Properties
Type Aliases
AmqpClientModuleAsyncOptions
type AmqpClientModuleAsyncOptions<TContract> = object;Defined in: packages/client-nestjs/src/client.module.ts:22
Options for async module configuration using factory pattern
Type Parameters
| Type Parameter |
|---|
TContract extends ContractDefinition |
Properties
| Property | Type | Description | Defined in |
|---|---|---|---|
imports? | ModuleMetadata["imports"] | Optional list of imported modules that export providers needed by the factory | packages/client-nestjs/src/client.module.ts:37 |
inject? | (string | symbol | Type<unknown>)[] | Optional dependencies to inject into the factory function. Can be a token (string/symbol) or a class reference to a provider. | packages/client-nestjs/src/client.module.ts:33 |
useFactory | (...args) => AmqpClientModuleOptionsFactory<TContract> | Factory function that returns the module options. Can use injected dependencies to create configuration. | packages/client-nestjs/src/client.module.ts:28 |
AmqpClientModuleOptions
type AmqpClientModuleOptions<TContract> = object;Defined in: packages/client-nestjs/src/client.service.ts:30
Configuration options for the AMQP client NestJS module.
Example
const options: AmqpClientModuleOptions<typeof contract> = {
contract: myContract,
urls: ['amqp://localhost'],
connectionOptions: {
heartbeatIntervalInSeconds: 30
}
};Type Parameters
| Type Parameter | Description |
|---|---|
TContract extends ContractDefinition | The contract definition type |
Properties
| Property | Type | Description | Defined in |
|---|---|---|---|
connectionOptions? | AmqpConnectionManagerOptions | Optional connection configuration (heartbeat, reconnect settings, etc.) | packages/client-nestjs/src/client.service.ts:36 |
contract | TContract | The AMQP contract definition specifying publishers and their message schemas | packages/client-nestjs/src/client.service.ts:32 |
urls | ConnectionUrl[] | AMQP broker URL(s). Multiple URLs provide failover support | packages/client-nestjs/src/client.service.ts:34 |
ClientInferPublisherInput
type ClientInferPublisherInput<TContract, TName> = PublisherInferInput<InferPublisher<TContract, TName>>;Defined in: packages/client/dist/index.d.mts:52
Infer publisher input type (message payload) for a specific publisher in a contract
Type Parameters
| Type Parameter |
|---|
TContract extends ContractDefinition |
TName extends InferPublisherNames<TContract> |
Variables
MODULE_OPTIONS_TOKEN
const MODULE_OPTIONS_TOKEN: typeof MODULE_OPTIONS_TOKEN;Defined in: packages/client-nestjs/src/client.module-definition.ts:5
Injection token for AMQP client module options Used by NestJS DI system to inject configuration into AmqpClientService