Handler
Overview
Section titled “Overview”Handler<T> is the shape ServiceConnect accepts when you call bus.handle(typeName, handler). It is a union of three forms: a plain async function, an object with a handle method, or a { factory } wrapper that builds a function or class per message (useful for per-consume DI scopes). All three receive the deserialised message and a ConsumeContext, and all three return Promise<void>.
Import
Section titled “Import”import type { Handler, HandlerClass, HandlerFactory, HandlerFn } from '@serviceconnect/core';Signature
Section titled “Signature”export type HandlerFn<T extends Message> = (message: T, context: ConsumeContext) => Promise<void>;
export interface HandlerClass<T extends Message> { handle(message: T, context: ConsumeContext): Promise<void>;}
export type HandlerFactory<T extends Message> = ( context: ConsumeContext,) => HandlerClass<T> | HandlerFn<T>;
export type Handler<T extends Message> = | HandlerFn<T> | HandlerClass<T> | { factory: HandlerFactory<T> };Parameters
Section titled “Parameters”HandlerFn<T>— the function form. The common case: an async function that takes the message and aConsumeContextand resolves when work is done.HandlerClass<T>— the class form. Any object with anasync handle(message, context)method satisfies the shape; no base class or decorator is required.HandlerFactory<T>— a function that receives theConsumeContextand returns either anHandlerFn<T>or aHandlerClass<T>. Use it when a DI container needs to build a fresh handler instance — and its scoped dependencies — per inbound message.Handler<T>— the union accepted bybus.handle(...). Pass any of the three forms above.
Return value and failure signalling
Section titled “Return value and failure signalling”Handlers return Promise<void>. There are no magic return values: a Promise<void> that resolves means success. To signal a transient failure, throw — the framework decides whether to retry based on the error type and the configured retry policy. Poison messages flow on to the configured error queue.
Example — function form
Section titled “Example — function form”import type { Handler, Message } from '@serviceconnect/core';
interface OrderPlaced extends Message { orderId: string;}
const onOrderPlaced: Handler<OrderPlaced> = async (msg, ctx) => { ctx.logger.info('processing', { orderId: msg.orderId });};
bus.handle<OrderPlaced>('OrderPlaced', onOrderPlaced);Example — class form
Section titled “Example — class form”import type { ConsumeContext, HandlerClass, Message } from '@serviceconnect/core';
interface OrderPlaced extends Message { orderId: string;}
class OnOrderPlaced implements HandlerClass<OrderPlaced> { async handle(msg: OrderPlaced, ctx: ConsumeContext): Promise<void> { ctx.logger.info('processing', { orderId: msg.orderId }); }}
bus.handle<OrderPlaced>('OrderPlaced', new OnOrderPlaced());Example — factory form
Section titled “Example — factory form”import type { Handler, Message } from '@serviceconnect/core';
interface OrderPlaced extends Message { orderId: string;}
const onOrderPlaced: Handler<OrderPlaced> = { factory: (ctx) => async (msg) => { const scope = container.createScope(ctx.correlationId); const service = scope.resolve('OrderService'); await service.process(msg.orderId); },};
bus.handle<OrderPlaced>('OrderPlaced', onOrderPlaced);