Stone.jsDocs
Paradigm

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.

app/Application.tsts
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

app/Handlers.tsts
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:

app/config.tsts
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.

app/Application.tsts
@KeyRouting({
  extractor: (event) => {
    const raw = event.get('metadata', {})
    return { key: raw.type, payload: raw.data }
  },
  strict: true
})

#Who uses it

callerTypeDescription
@stone-js/event-buslistenerIncoming bus events (@OnBusEvent) route through the light router on any cloud adapter.
@stone-js/realtimegatewaysWebSocket adapters feed socket events to the kernel; @On* gateway methods are keyed handlers.
CLI commandscommandsA command name is a key; the console adapter dispatches through the same router.

Stone.js

Your app exists in every runtime. Until you run it.

An open-source project by Stone Foundation
Created by Mr. Stone (Evens Pierre)