---
url: https://node-ts.github.io/bus/api/bus-core/functions/handlerFor.md
---
[API reference](../../index.md) / [@node-ts/bus-core](../index.md) / 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](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/handler/handler-for.ts#L36)

Declares a message handling function

### Type Parameters

| Type Parameter | Default type | Description |
| ------ | ------ | ------ |
| `TMessageType` *extends* [`Message`](../../bus-messages/classes/Message.md) | - | The message the handler handles, inferred from `messageType` |
| `TMessageAttributes` *extends* [`MessageAttributes`](../../bus-messages/interfaces/MessageAttributes.md)<[`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md), [`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md)> | [`MessageAttributes`](../../bus-messages/interfaces/MessageAttributes.md)<[`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md), [`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md)> | 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`](../type-aliases/FunctionHandler.md)<`TMessageType`, `TMessageAttributes`> | [`FunctionHandler`](../type-aliases/FunctionHandler.md)<`TMessageType`, `TMessageAttributes`> | - |

### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `messageType` | [`MessageDeclaration`](../../bus-messages/type-aliases/MessageDeclaration.md)<`TMessageType`> | Type of message that the function handles: a message class with a static `NAME`, or a definition from `defineCommand` or `defineEvent` |
| `messageHandler` | `THandler` | Function 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`](../interfaces/HandlerFor.md)<`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](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/handler/handler-for.ts#L45)

Declares a message handling function

### Type Parameters

| Type Parameter | Default type | Description |
| ------ | ------ | ------ |
| `TMessageType` *extends* [`Message`](../../bus-messages/classes/Message.md) | - | The message the handler handles, inferred from `messageType` |
| `TMessageAttributes` *extends* [`MessageAttributes`](../../bus-messages/interfaces/MessageAttributes.md)<[`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md), [`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md)> | [`MessageAttributes`](../../bus-messages/interfaces/MessageAttributes.md)<[`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md), [`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md)> | 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`](../type-aliases/HandlerDefinition.md)<`TMessageType`, `TMessageAttributes`> | [`HandlerDefinition`](../type-aliases/HandlerDefinition.md)<`TMessageType`, `TMessageAttributes`> | - |

### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `messageType` | [`MessageDeclaration`](../../bus-messages/type-aliases/MessageDeclaration.md)<`TMessageType`> | Type of message that the function handles: a message class with a static `NAME`, or a definition from `defineCommand` or `defineEvent` |
| `messageHandler` | `THandler` | Function 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`](../interfaces/HandlerFor.md)<`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)
```
