Skip to content

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

Class: BusConfiguration ​

Defined in: packages/bus-core/src/service-bus/bus-configuration.ts:62

Constructors ​

Constructor ​

ts
new BusConfiguration(): BusConfiguration;

Returns ​

BusConfiguration

Methods ​

asSendOnly() ​

ts
asSendOnly(): this;

Defined in: packages/bus-core/src/service-bus/bus-configuration.ts:162

Configure the bus to only send messages and not receive them. No queues or subscriptions will be created for this service.

Returns ​

this

Throws ​

BusAlreadyInitialized if called after the bus has been built


build() ​

ts
build(): BusInstance;

Defined in: packages/bus-core/src/service-bus/bus-configuration.ts:91

Constructs an instance of a bus from the configuration

Returns ​

BusInstance

Throws ​

BusAlreadyInitialized if the bus has already been built

Throws ​

ContainerNotRegistered if a class handler's constructor takes arguments and no container is registered

Throws ​

TransportAlreadyInUse if the transport is already used by another bus

Throws ​

MessageTypesConflict if the message types passed to withMessageTypes() define a $name or type differently

Throws ​

MessageTypeReferenceNotFound if message types passed to withMessageTypes() refer to a type they don't define


withConcurrency() ​

ts
withConcurrency(concurrency): this;

Defined in: packages/bus-core/src/service-bus/bus-configuration.ts:356

Sets the message handling concurrency beyond the default value of 1, which will increase the number of messages handled in parallel.

Parameters ​

ParameterType
concurrencynumber

Returns ​

this

Default ​

ts
1

Throws ​

BusAlreadyInitialized if called after the bus has been built


withContainer() ​

ts
withContainer(container): this;

Defined in: packages/bus-core/src/service-bus/bus-configuration.ts:377

Use a local dependency injection/IoC container to resolve handlers and workflows.

Parameters ​

ParameterTypeDescription
containerContainerAdapterAn adapter to an existing DI container to fetch class instances from

Returns ​

this

Throws ​

BusAlreadyInitialized if called after the bus has been built


withCustomHandler() ​

ts
withCustomHandler<MessageType>(messageHandler, customResolver): this;

Defined in: packages/bus-core/src/service-bus/bus-configuration.ts:226

Registers a custom handler that receives messages from external systems, or messages that don't implement the Message interface from @node-ts/bus-messages

Type Parameters ​

Type Parameter
MessageType

Parameters ​

ParameterTypeDescription
messageHandlerHandlerDefinition<MessageType>A handler that receives the custom message
customResolverCustomResolver<MessageType>A discriminator that determines if an incoming message should be mapped to this handler.

Returns ​

this

Throws ​

BusAlreadyInitialized if called after the bus has been built


withHandler() ​

Call Signature ​

ts
withHandler(...classHandler): this;

Defined in: packages/bus-core/src/service-bus/bus-configuration.ts:180

Register a handler for a specific message type. When Bus is initialized it will configure the transport to subscribe to this type of message and upon receipt will forward the message through to the provided message handler

Parameters ​
ParameterType
...classHandlerClassConstructor<Handler<MessageBase, MessageAttributes<MessageAttributeMap, MessageAttributeMap>>>[]
Returns ​

this

Throws ​

BusAlreadyInitialized if called after the bus has been built

Call Signature ​

ts
withHandler<MessageType, TMessageAttributes>(...functionHandler): this;

Defined in: packages/bus-core/src/service-bus/bus-configuration.ts:181

Register a handler for a specific message type. When Bus is initialized it will configure the transport to subscribe to this type of message and upon receipt will forward the message through to the provided message handler

Type Parameters ​
Type ParameterDefault type
MessageType extends Message-
TMessageAttributes extends MessageAttributes<MessageAttributeMap, MessageAttributeMap>MessageAttributes<MessageAttributeMap, MessageAttributeMap>
Parameters ​
ParameterType
...functionHandlerobject[]
Returns ​

this

Throws ​

BusAlreadyInitialized if called after the bus has been built


withInterruptSignals() ​

ts
withInterruptSignals(signals): this;

Defined in: packages/bus-core/src/service-bus/bus-configuration.ts:432

Sets the process signals that gracefully stop the bus, replacing the defaults. Pass an empty array to listen for none, so a host such as NestJS or AWS Lambda can own shutdown and call bus.stop() or bus.dispose() itself. Send-only buses never listen for signals.

Parameters ​

ParameterTypeDescription
signalsSignals[]The signals that stop the bus

Returns ​

this

Default ​

ts
['SIGINT', 'SIGTERM']

Throws ​

BusAlreadyInitialized if called after the bus has been built

Examples ​

ts
// Let the host handle SIGINT and SIGTERM
Bus.configure().withInterruptSignals([])
ts
// Also stop on SIGUSR2
Bus.configure().withInterruptSignals(['SIGINT', 'SIGTERM', 'SIGUSR2'])

withLogger() ​

ts
withLogger(loggerFactory): this;

Defined in: packages/bus-core/src/service-bus/bus-configuration.ts:282

Configures Bus to use a different logging provider than the default console logger

Parameters ​

ParameterType
loggerFactoryLoggerFactory

Returns ​

this

Throws ​

BusAlreadyInitialized if called after the bus has been built


withMessageReadMiddleware() ​

ts
withMessageReadMiddleware<TransportMessageType>(messageReadMiddleware): this;

Defined in: packages/bus-core/src/service-bus/bus-configuration.ts:392

Register optional middlewares that will run for each message that is polled from the transport Note these middlewares only run when polling successfully pulls a message off the Transports queue After all the user defined middlewares have registered.

Type Parameters ​

Type ParameterDefault type
TransportMessageTypeunknown

Parameters ​

ParameterType
messageReadMiddlewareMiddleware<TransportMessage<TransportMessageType>>

Returns ​

this

Throws ​

BusAlreadyInitialized if called after the bus has been built


withMessageTypes() ​

ts
withMessageTypes(...messageTypes): this;

Defined in: packages/bus-core/src/service-bus/bus-configuration.ts:327

Gives the bus the message types generated by bus generate-message-types (from @node-ts/bus-cli), which say how to restore the Dates, Maps, Sets, bigints and classes of each message and workflow state it receives. Pass the messageTypes export of every generated file whose messages the bus handles. Calling this again adds to the message types already passed.

A bus that receives messages must have message types for every message it handles and every workflow state it persists, or initialize() throws MessageTypesMissing. Send-only buses don't need them.

Parameters ​

ParameterTypeDescription
...messageTypesMessageTypes[]the message types of one or more generated files

Returns ​

this

Throws ​

BusAlreadyInitialized if called after the bus has been built

Example ​

ts
import { messageTypes as orderMessageTypes } from '@my-org/order-messages'
import { messageTypes } from './message-types.generated'

const bus = Bus.configure()
  .withMessageTypes(orderMessageTypes, messageTypes)
  .withHandler(orderPlacedHandler)
  .build()

withPersistence() ​

ts
withPersistence(persistence): this;

Defined in: packages/bus-core/src/service-bus/bus-configuration.ts:341

Configures Bus to use a different persistence provider than the default InMemoryPersistence provider. This is used to persist workflow data and is unused if not using workflows.

Parameters ​

ParameterType
persistencePersistence

Returns ​

this

Throws ​

BusAlreadyInitialized if called after the bus has been built


withReceiver() ​

ts
withReceiver(receiver): this;

Defined in: packages/bus-core/src/service-bus/bus-configuration.ts:453

Register a receiving mechanism that will be used to receive messages and deliver them to the dispatcher.

Usually the bus will connect to a transport and receive messages directly. However a different receiver plugin can be used that will become responsible for this instead.

Once the bus is configured, messages can be received by passing the received message into bus.receive().

Parameters ​

ParameterTypeDescription
receiver| Receiver<unknown, TransportMessage<unknown>, unknown> | undefinedThe receiver mechanism to use, or undefined to use the default behaviour

Returns ​

this

Throws ​

BusAlreadyInitialized if called after the bus has been built


withRetryStrategy() ​

ts
withRetryStrategy(retryStrategy): this;

Defined in: packages/bus-core/src/service-bus/bus-configuration.ts:409

Configure @node-ts/bus to use a different retry strategy that determines delays between retrying failed messages.

Parameters ​

ParameterType
retryStrategyRetryStrategy

Returns ​

this

Default ​

ts
DefaultRetryStrategy

Throws ​

BusAlreadyInitialized if called after the bus has been built


withSerializer() ​

ts
withSerializer(serializer): this;

Defined in: packages/bus-core/src/service-bus/bus-configuration.ts:299

Configures Bus to use a different serialization provider. The provider is responsible for transforming messages to/from a serialized representation, as well as ensuring all object properties are a strong type. The bus passes its message types (see withMessageTypes()) to the serializer each time it restores an object, so one serializer can be shared by several buses.

Parameters ​

ParameterType
serializerSerializer

Returns ​

this

Default ​

ts
JsonSerializer

Throws ​

BusAlreadyInitialized if called after the bus has been built


withTransport() ​

ts
withTransport(transportConfiguration): this;

Defined in: packages/bus-core/src/service-bus/bus-configuration.ts:269

Configures Bus to use a different transport than the default MemoryQueue. A transport instance holds one queue and one connection, so each bus needs its own instance.

Parameters ​

ParameterType
transportConfigurationTransport

Returns ​

this

Throws ​

BusAlreadyInitialized if called after the bus has been built


withWorkflow() ​

ts
withWorkflow(...workflow): this;

Defined in: packages/bus-core/src/service-bus/bus-configuration.ts:248

Register a workflow definition so that all of the messages it depends on will be subscribed to and forwarded to the handlers inside the workflow

Parameters ​

ParameterTypeDescription
...workflow( | FunctionWorkflow<WorkflowState> | ClassConstructor<Workflow<WorkflowState>>)[]Classes that extend Workflow, or workflows declared with defineWorkflow

Returns ​

this

Throws ​

BusAlreadyInitialized if called after the bus has been built

Throws ​

WorkflowNotRecognized if a workflow is neither a class that extends Workflow nor declared with defineWorkflow

Example ​

ts
Bus.configure().withWorkflow(OrderWorkflow, defineWorkflow(ShippingState).startedBy(OrderPaid, ...))

Released under the MIT License.