RouterBuilder class

Default implementation of the Router interface for JetLeaf.

RouterBuilder provides a simple and fluent builder-style API for declaring routes using method chaining:

final router = RouterBuilder()
  ..get('/hello', (req) => 'Hello!')
  ..post('/save', (req) => {'status': 'ok'});

The builder maintains internal lists of routes and child routers, which are merged and flattened when build is invoked.

Features

  • Supports (req) and (req, res) style handlers.
  • Allows grouping via group with a shared path prefix.
  • Supports composability via and.
  • Produces immutable RouterSpecs for the dispatcher.

Example

final router = RouterBuilder()
  ..group('/api', RouterBuilder()
      ..get('/ping', (req) => 'pong')
      ..post('/users', (req) => {'ok': true})
  );

final spec = router.build();
Inheritance

Constructors

RouterBuilder([String? _pathPrefix, bool ignoreContextPath = true])
Creates a new RouterBuilder, optionally with a pathPrefix.

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 pairinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

and(Router other) Router
Combines this router with another router.
override
build({String? contextPath}) RouterSpec
Resolves all routes (including children and grouped routers) into a single immutable RouterSpec.
override
child(Route route, RequestRouterFunction handler) Router
Registers a child router whose route is defined using a handler with (ServerHttpRequest) only.
override
childX(Route route, XRouterFunction handler) Router
Registers a child router whose route handler accepts both (ServerHttpRequest, ServerHttpResponse).
override
group(String pathPrefix, Router router) Router
Groups routes under a common path prefix.
override
noContextPath(bool ignoreContextPath) Router
When true, excludes the application context path from the route mapping.
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.
override
routeX(Route route, XRouterFunction handler) Router
Registers a route whose handler accepts both a ServerHttpRequest and a ServerHttpResponse.
override
toString() String
A string representation of this object.
inherited

Operators

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