Skip to content

API reference / @node-ts/bus-core / handlerFor

Function: handlerFor() ​

Call Signature ​

ts
function handlerFor<TMessageType, TMessageAttributes, THandler>(messageType, messageHandler): HandlerFor<TMessageType, THandler>;

Defined in: packages/bus-core/src/handler/handler-for.ts:36

Declares a message handling function

Type Parameters ​

Type ParameterDefault typeDescription
TMessageType extends Message-The message the handler handles, inferred from messageType
TMessageAttributes extends MessageAttributes<MessageAttributeMap, MessageAttributeMap>MessageAttributes<MessageAttributeMap, MessageAttributeMap>The attributes the handler reads, such as MessageAttributes<{ tenantId: string }>. Give it with the message type to type the handler's attributes.
THandler extends FunctionHandler<TMessageType, TMessageAttributes>FunctionHandler<TMessageType, TMessageAttributes>-

Parameters ​

ParameterTypeDescription
messageTypeMessageDeclaration<TMessageType>Type of message that the function handles: a message class with a static NAME, or a definition from defineCommand or defineEvent
messageHandlerTHandlerFunction that is executed each time the type of message is read from the bus. It's passed the message, its attributes and a HandlerContext to send and publish through the bus that received it. Its return value is ignored, and a returned promise is awaited.

Returns ​

HandlerFor<TMessageType, THandler>

The message type and the handler, typed as given so a function handler can be called directly in tests

Examples ​

ts
const placeOrderHandler = handlerFor(PlaceOrder, async (message, attributes, ctx) => {
  await ctx.publish(new OrderPlaced(message.orderId))
})
ts
const PlaceOrder = defineCommand('@my-org/orders/place-order')<{ orderId: string }>()
const placeOrderHandler = handlerFor(PlaceOrder, async placeOrder => repository.save(placeOrder))
ts
type TenantAttributes = MessageAttributes<{ tenantId: string }>
const placeOrderHandler = handlerFor<PlaceOrder, TenantAttributes>(PlaceOrder, async (message, attributes) =>
  repository.save(attributes.attributes.tenantId, message)
)
ts
const placeOrderHandler = handlerFor<PlaceOrder>(PlaceOrder, PlaceOrderClassHandler)

Call Signature ​

ts
function handlerFor<TMessageType, TMessageAttributes, THandler>(messageType, messageHandler): HandlerFor<TMessageType, THandler>;

Defined in: packages/bus-core/src/handler/handler-for.ts:45

Declares a message handling function

Type Parameters ​

Type ParameterDefault typeDescription
TMessageType extends Message-The message the handler handles, inferred from messageType
TMessageAttributes extends MessageAttributes<MessageAttributeMap, MessageAttributeMap>MessageAttributes<MessageAttributeMap, MessageAttributeMap>The attributes the handler reads, such as MessageAttributes<{ tenantId: string }>. Give it with the message type to type the handler's attributes.
THandler extends HandlerDefinition<TMessageType, TMessageAttributes>HandlerDefinition<TMessageType, TMessageAttributes>-

Parameters ​

ParameterTypeDescription
messageTypeMessageDeclaration<TMessageType>Type of message that the function handles: a message class with a static NAME, or a definition from defineCommand or defineEvent
messageHandlerTHandlerFunction that is executed each time the type of message is read from the bus. It's passed the message, its attributes and a HandlerContext to send and publish through the bus that received it. Its return value is ignored, and a returned promise is awaited.

Returns ​

HandlerFor<TMessageType, THandler>

The message type and the handler, typed as given so a function handler can be called directly in tests

Examples ​

ts
const placeOrderHandler = handlerFor(PlaceOrder, async (message, attributes, ctx) => {
  await ctx.publish(new OrderPlaced(message.orderId))
})
ts
const PlaceOrder = defineCommand('@my-org/orders/place-order')<{ orderId: string }>()
const placeOrderHandler = handlerFor(PlaceOrder, async placeOrder => repository.save(placeOrder))
ts
type TenantAttributes = MessageAttributes<{ tenantId: string }>
const placeOrderHandler = handlerFor<PlaceOrder, TenantAttributes>(PlaceOrder, async (message, attributes) =>
  repository.save(attributes.attributes.tenantId, message)
)
ts
const placeOrderHandler = handlerFor<PlaceOrder>(PlaceOrder, PlaceOrderClassHandler)

Released under the MIT License.