---
url: https://node-ts.github.io/bus/api/bus-core/interfaces/WorkflowContext.md
---
[API reference](../../index.md) / [@node-ts/bus-core](../index.md) / WorkflowContext

# Interface: WorkflowContext\<TWorkflowState, TMessageAttributes>

Defined in: [packages/bus-core/src/workflow/workflow-context.ts:18](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/workflow/workflow-context.ts#L18)

Passed to every handler of a workflow declared with `defineWorkflow`. It's the `HandlerContext` of the message
being handled, plus the message's attributes and the functions that end the workflow.

Messages sent or published from it carry the workflow id in their sticky attributes, so replies are routed back
to the same workflow instance.

It's an interface so a workflow handler can be unit tested by calling it with a plain object, such as one from
`workflowContext()`.

## Example

```ts
const ctx = workflowContext<OrderState>({ send: async command => { sent.push(command) } })
```

## Extends

* [`HandlerContext`](HandlerContext.md)

## Type Parameters

| Type Parameter | Default type |
| ------ | ------ |
| `TWorkflowState` *extends* [`WorkflowState`](../classes/WorkflowState.md) | - |
| `TMessageAttributes` *extends* [`MessageAttributes`](../../bus-messages/interfaces/MessageAttributes.md) | [`MessageAttributes`](../../bus-messages/interfaces/MessageAttributes.md) |

## Properties

| Property | Modifier | Type | Description | Inherited from | Defined in |
| ------ | ------ | ------ | ------ | ------ | ------ |
| <a id="property-attributes"></a> `attributes` | `readonly` | `TMessageAttributes` | The attributes of the message being handled | - | [packages/bus-core/src/workflow/workflow-context.ts:25](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/workflow/workflow-context.ts#L25) |
| <a id="property-correlationid"></a> `correlationId` | `readonly` | `string` | `undefined` | The correlation id of the message being handled, which is also put on every message sent or published from this context. `undefined` only when a message from outside the bus arrived without one. | [`HandlerContext`](HandlerContext.md).[`correlationId`](HandlerContext.md#property-correlationid) | [packages/bus-core/src/handler/handler-context.ts:35](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/handler/handler-context.ts#L35) |

## Methods

### complete()

```ts
complete(workflowState?): WorkflowStateChange<TWorkflowState>;
```

Defined in: [packages/bus-core/src/workflow/workflow-context.ts:35](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/workflow/workflow-context.ts#L35)

Ends the workflow. Return its result from the handler. The workflow instance is no longer activated by later
messages.

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `workflowState?` | `Partial`<`TWorkflowState`> | Final changes to the workflow state to save with it |

#### Returns

[`WorkflowStateChange`](../type-aliases/WorkflowStateChange.md)<`TWorkflowState`>

The changes to return from the handler

#### Example

```ts
.when(CardCharged, (_message, _state, ctx) => ctx.complete({ charged: true }))
```

***

### discard()

```ts
discard(): WorkflowStateChange<TWorkflowState>;
```

Defined in: [packages/bus-core/src/workflow/workflow-context.ts:48](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/workflow/workflow-context.ts#L48)

Drops the changes of this handler, so nothing is saved. Returned from a `startedBy` handler, it stops the
workflow from starting. Return its result from the handler.

#### Returns

[`WorkflowStateChange`](../type-aliases/WorkflowStateChange.md)<`TWorkflowState`>

The result to return from the handler

#### Example

```ts
.startedBy(DocumentUploaded, (message, _state, ctx) =>
  message.path.startsWith('/documents') ? { path: message.path } : ctx.discard()
)
```

***

### failMessage()

```ts
failMessage(): Promise<void>;
```

Defined in: [packages/bus-core/src/handler/handler-context.ts:41](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/handler/handler-context.ts#L41)

Routes the message being handled straight to the dead letter queue, without further retries. The handler
should return after calling this.

#### Returns

`Promise`<`void`>

#### Inherited from

[`HandlerContext`](HandlerContext.md).[`failMessage`](HandlerContext.md#failmessage)

***

### publish()

```ts
publish<TEvent>(event, messageAttributes?): Promise<void>;
```

Defined in: [packages/bus-core/src/handler/bus-sender.ts:37](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/handler/bus-sender.ts#L37)

Publishes an event to the transport. Inside a handler, the event is buffered and only published once the
handler resolves, and is dropped if the handler fails.

#### Type Parameters

| Type Parameter |
| ------ |
| `TEvent` *extends* [`Event`](../../bus-messages/classes/Event.md) |

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `event` | `TEvent` | The event to publish |
| `messageAttributes?` | `Partial`<[`MessageAttributes`](../../bus-messages/interfaces/MessageAttributes.md)<[`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md), [`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md)>> | Attributes to attach to the outgoing message. The `correlationId` and `stickyAttributes` of the message being handled are added when published from a handler. |

#### Returns

`Promise`<`void`>

#### Inherited from

[`HandlerContext`](HandlerContext.md).[`publish`](HandlerContext.md#publish)

***

### returnMessage()

```ts
returnMessage(): Promise<void>;
```

Defined in: [packages/bus-core/src/handler/handler-context.ts:47](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/handler/handler-context.ts#L47)

Returns the message being handled to the queue so that it's retried, without failing the handler. When the
message came from a `Receiver`, it's reported to the receiver host as failed so the host doesn't delete it.

#### Returns

`Promise`<`void`>

#### Inherited from

[`HandlerContext`](HandlerContext.md).[`returnMessage`](HandlerContext.md#returnmessage)

***

### send()

```ts
send<TCommand>(command, messageAttributes?): Promise<void>;
```

Defined in: [packages/bus-core/src/handler/bus-sender.ts:25](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/handler/bus-sender.ts#L25)

Sends a command to the transport. Inside a handler, the command is buffered and only sent once the handler
resolves, and is dropped if the handler fails.

#### Type Parameters

| Type Parameter |
| ------ |
| `TCommand` *extends* [`Command`](../../bus-messages/classes/Command.md) |

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `command` | `TCommand` | The command to send |
| `messageAttributes?` | `Partial`<[`MessageAttributes`](../../bus-messages/interfaces/MessageAttributes.md)<[`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md), [`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md)>> | Attributes to attach to the outgoing message. The `correlationId` and `stickyAttributes` of the message being handled are added when sent from a handler. |

#### Returns

`Promise`<`void`>

#### Inherited from

[`HandlerContext`](HandlerContext.md).[`send`](HandlerContext.md#send)
