Routing
Key routing (light)
Not every event has a URL. A cloud bus message, a WebSocket frame, a CLI command are keyed by a name, not a path. @KeyRouting() is the event-routing sibling of @Routing(): it installs a tiny kernel event handler that routes by a key instead of matching a route. Same pattern as the full router (adapter → kernel → handler), a fraction of the size, and tree-shaken away when you do not use it.
#Why a second router
The universal @Routing() carries paths, parameters, constraints, groups and URL generation, everything an HTTP surface needs. An event consumer needs none of that: it maps order.shipped to a handler. Forcing the full router onto a queue worker or a bus consumer is weight with no benefit. @KeyRouting() is that mapping and nothing else, a minimal key-to-handler registry that ships inside the router package.
#Enable it
Stack it on any adapter that runs the kernel. Each incoming event is routed by a key read from a configurable property.
import { StoneApp } from '@stone-js/core'
import { AwsLambda } from '@stone-js/aws-lambda-adapter'
import { KeyRouting } from '@stone-js/router'
@KeyRouting({ source: 'detail-type' }) // which incoming property carries the key
@AwsLambda()
@StoneApp({ name: 'consumer' })
export class Application {}#Handlers
import { KeyHandler, OnKey } from '@stone-js/router'
@KeyHandler()
export class Handlers {
@OnKey('order.shipped') async onShipped (payload) { /* … */ }
@OnKey('order.cancelled') async onCancelled (payload) { /* … */ }
}Or define routes imperatively:
import { defineConfig } from '@stone-js/core'
import { defineKeyRouting, defineKeyRoute } from '@stone-js/router'
export const AppConfig = defineConfig(defineKeyRouting({
source: 'detail-type',
definitions: [ defineKeyRoute('order.shipped', OnShipped, { isClass: true }) ]
}))#The routing key is configurable
source names the incoming-event property holding the key (defaults to detail-type, where cloud buses put the event name). Pass a full extractor for any other shape, and strict: true to throw on an unmatched key instead of a no-op.
@KeyRouting({
extractor: (event) => {
const raw = event.get('metadata', {})
return { key: raw.type, payload: raw.data }
},
strict: true
})#Who uses it
| caller | Type | Description |
|---|---|---|
| @stone-js/event-bus | listener | Incoming bus events (@OnBusEvent) route through the light router on any cloud adapter. |
| @stone-js/realtime | gateways | WebSocket adapters feed socket events to the kernel; @On* gateway methods are keyed handlers. |
| CLI commands | commands | A command name is a key; the console adapter dispatches through the same router. |