Router class abstract

Defines the core contract for JetLeaf’s routing system.

The Router interface provides a flexible and extensible way to register routes, group paths, and build a structured RouterSpec for use in JetLeaf’s request–response dispatch pipeline.

Routers can define handlers using either:

  • route() — for handlers that accept only (ServerHttpRequest)
  • routeX() — for handlers that accept (ServerHttpRequest, ServerHttpResponse)

They can also combine other routers via and or group related routes under a shared path prefix via group.

Example

final router = RouterBuilder()
  ..get('/hello', (req) => 'Hello!')
  ..post('/save', (req) async => {'status': 'ok'})
  ..group('/api', RouterBuilder()
      ..get('/users', (req) => ['Alice', 'Bob'])
      ..post('/users', (req) => {'created': true})
  );

final spec = router.build();

for (final route in spec.routes) {
  print('${route.method} ${route.path}');
}

Key Concepts

  • Composable Design: You can chain routers with and.
  • Grouping: Use group to define nested route hierarchies.
  • Two Handler Modes: Choose between req or req, res signatures.
  • Build Step: Converts the in-memory structure into an immutable RouterSpec used by JetLeaf’s dispatcher.

Extension

Custom router implementations may implement this interface directly to provide their own builder-style APIs, while still producing a compatible RouterSpec.

Implementers

Constructors

Router([bool ignoreContextPath = true])
Defines the core contract for JetLeaf’s routing system.

Properties

hashCode int
The hash code for this object.
no setterinherited
ignoreContextPath bool
When true, excludes the application context path from the route mapping.
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

and(Router other) Router
Combines this router with another router.
build({String? contextPath}) RouterSpec
Builds and returns a complete RouterSpec containing all defined and nested routes.
child(Route route, RequestRouterFunction handler) Router
Registers a child router whose route is defined using a handler with (ServerHttpRequest) only.
childX(Route route, XRouterFunction handler) Router
Registers a child router whose route handler accepts both (ServerHttpRequest, ServerHttpResponse).
group(String pathPrefix, Router router) Router
Groups routes under a common path prefix.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
route(Route route, RequestRouterFunction handler) Router
Registers a route whose handler accepts only a ServerHttpRequest.
routeX(Route route, XRouterFunction handler) Router
Registers a route whose handler accepts both a ServerHttpRequest and a ServerHttpResponse.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited