Stone.js API
    Preparing search index...

    Class Router<IncomingEventType, OutgoingResponseType>

    Represents a configurable router for managing HTTP routes and handling incoming events.

    Type Parameters

    • IncomingEventType extends IIncomingEvent = IIncomingEvent

      Type of incoming events.

    • OutgoingResponseType = unknown

      Type of outgoing responses.

    Index
    • Dumps all routes as an array of JSON objects.

      Returns Promise<Record<string, unknown>[]>

      An array of JSON objects representing the routes.

    • One parameter of the route this event resolves to, from any layer.

      The sugar over getBoundRoute for the common case: a group middleware reading the orgCode its guard needs, a locale middleware reading :lang. Named alongside findRoute, because find* is the family that takes an event and may match, where getParam reads the already-dispatched current route.

      A parameter of a route that does not exist is simply absent, so a miss answers the fallback rather than throwing: deciding that a request is a 404 is the router's job at dispatch, not the caller's at peek.

      Type Parameters

      • TReturn = unknown

      Parameters

      • event: IncomingEventType

        The incoming event.

      • name: string

        The parameter to read.

      • Optionalfallback: TReturn

        What to answer when the route has no such parameter, or no route matches.

      Returns Promise<TReturn>

      The parameter's value, or the fallback.

    • Generates a URL based on a named route and the provided options.

      Parameters

      • options: GenerateOptions

        Options for generating the URL, including the route name, parameters, and query.

      Returns string

      The generated URL as a string.

      If no route is found with the specified name.

    • The route this event resolves to, bound, from any layer.

      Before routing (a kernel or group middleware), no route exists on the event yet, and even on the router layer the parameters are only bound after the route middleware have run. Reading a parameter therefore took a three-line dance everywhere: find the route, bind it, read it. This is that dance, done once and remembered per event.

      It is a peek, not a dispatch: nothing is emitted, currentRoute is untouched, and the router's own resolution later proceeds exactly as if nobody had looked. The event's already-resolved route is reused when there is one, so asking after routing costs one map lookup.

      Parameters

      Returns Promise<Route<IncomingEventType, OutgoingResponseType>>

      The bound route.

      When no route matches the event.

    • Retrieves the name of the currently active route.

      Returns string

      The name of the current route, or undefined if no route is active.

    • Retrieves a specific parameter from the current route.

      Type Parameters

      • TReturn = unknown

        The expected return type of the parameter.

      Parameters

      • name: string

        The name of the parameter to retrieve.

      • Optionalfallback: TReturn

        An optional fallback value to return if the parameter is not found.

      Returns TReturn

      The value of the parameter, or the fallback value if the parameter is not found.

    • Retrieves the parameters of the current route.

      Returns RouteParams

      An object containing the parameters of the current route, or undefined if no route is active.

    • Checks if the router contains a route with the given name(s).

      Parameters

      • name: string | string[]

        A route name or an array of route names to check.

      Returns boolean

      true if at least one of the specified routes exists, false otherwise.

    • Checks if the currently active route matches the specified name.

      Parameters

      • name: string

        The name to compare with the current route's name.

      Returns boolean

      true if the current route's name matches the specified name, false otherwise.

    • Navigates to a specific route.

      Resolving the destination is platform-independent (a route name becomes a path here); performing the effect is not, so it is delegated to the configured RouterNavigator. The default one drives the browser's History API, which is why this throws outside a browser unless another navigator is configured under stone.router.navigator.

      Parameters

      • pathOrOptions: string | NavigateOptions

        The path or navigation options, including route name and parameters.

      • Optionalreplace: boolean

        Whether to replace the current history entry instead of adding a new one.

      Returns void

      If no navigator is configured and this runs outside a browser.

    • Removes the current group definition, ending the grouping context.

      Returns this

      The router instance for chaining.

    • Unsubscribes from an event emitted by the router's event emitter.

      Parameters

      • eventName: string

        The name of the event to stop listening for.

      • listener: FunctionalEventListener

        The listener function to remove.

      Returns this

      The current Router instance.

    • Subscribes to an event emitted by the router's event emitter.

      Parameters

      • eventName: string

        The name of the event to listen for.

      • listener: FunctionalEventListener

        The listener function to execute when the event is emitted.

      Returns this

      The current Router instance.

    post

    • Dispatches an event to a specific route by its name.

      Parameters

      • event: IncomingEventType

        The incoming event to process.

      • name: string

        The name of the route to execute.

      Returns Promise<OutgoingResponseType>

      A promise resolving to the outgoing response after executing the specified route.

      If no route is found with the given name.