shelf_router_generator 0.7.2+1

  • Readme
  • Changelog
  • Example
  • Installing
  • 74

Shelf Router Generator #

Shelf makes it easy to build web applications in Dart by composing request handlers. The shelf_router package offers a request router for Shelf. this package enables generating a shelf_route.Router from annotations in code.

Disclaimer: This is not an officially supported Google product.

This package should be a development dependency along with package build_runner, and used with package shelf and package shelf_router as dependencies.

  shelf: ^0.7.5
  shelf_router: ^0.7.0+1
  shelf_router_generator: ^0.7.0+1
  build_runner: ^1.3.1

Once your code have been annotated as illustrated in the example below the generated part can be created with pub run build_runner build.

Example #

import 'package:shelf/shelf.dart';
import 'package:shelf_router/shelf_router.dart';

part 'userservice.g.dart'; // generated with 'pub run build_runner build'

class UserService {
  final DatabaseConnection connection;

  Future<Response> listUsers(Request request) async {
    return Response.ok('["user1"]');

  Future<Response> fetchUser(Request request, String userId) async {
    if (userId == 'user1') {
      return Response.ok('user1');
    return Response.notFound('no such user');

  // Create router using the generate function defined in 'userservice.g.dart'.
  Router get router => _$UserServiceRouter(this);

void main() async {
  // You can setup context, database connections, cache connections, email
  // services, before you create an instance of your service.
  var connection = await DatabaseConnection.connect('localhost:1234');

  // Create an instance of your service, usine one of the constructors you've
  // defined.
  var service = UserService(connection);
  // Service request using the router, note the router can also be mounted.
  var router = service.router;
  var server = await io.serve(router.handler, 'localhost', 8080);

v0.7.2+1 #

  • Relax dependency constraint on analyzer.

v0.7.2 #

  • Use literalString('...', raw: true) from package:code_builder to ensure that generated strings are properly escaped (fixing #10).

v0.7.1 #

  • Bumped dependencies.

v0.7.0+1 #

  • Updated with references to package build_runner.

v0.7.0 #

  • Initial release


// Copyright 2019 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

import 'dart:async' show Future;
import 'package:shelf_router/shelf_router.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as shelf_io;

// Generated code will be written to 'main.g.dart'
part 'main.g.dart';

class Service {
  // A handler is annotated with @Route.<verb>('<route>'), the '<route>' may
  // embed URL-parameters, and these may be taken as parameters by the handler.
  // But either all URL-parameters or none of the URL parameters must be taken
  // as parameters by the handler.
  Response _hi(Request request, String name) {
    return Response.ok('hi $name');

  // Embedded URL parameters may also be associated with a regular-expression
  // that the pattern must match.
  Response _user(Request request, String userId) {
    return Response.ok('User has the user-number: $userId');

  // Handlers can be asynchronous (returning `FutureOr` is also allowed).
  Future<Response> _wave(Request request) async {
    await Future.delayed(Duration(milliseconds: 100));
    return Response.ok('_o/');

  // Other routers can be mounted...
  Router get _api => Api().router;

  // You can catch all verbs and use a URL-parameter with a regular expression
  // that matches everything to catch app.
  Response _404(Request request) => Response.notFound('Page not found');

  // The generated function _$ServiceRouter can be used to get a [Handler]
  // for this object. This can be used with [shelf_io.serve].
  Handler get handler => _$ServiceRouter(this).handler;

class Api {
  // A handler can have more that one route :)
  Future<Response> _messages(Request request) async {
    return Response.ok('[]');

  // This nested catch-all, will only catch /api/.* when mounted above.
  // Notice that ordering if annotated handlers and mounts is significant.
  Response _404(Request request) => Response.notFound('null');

  // The generated function _$ApiRouter can be used to expose a [Router] for
  // this object.
  Router get router => _$ApiRouter(this);

// Run shelf server and host a [Service] instance on port 8080.
void main() async {
  final service = Service();
  final server = await shelf_io.serve(service.handler, 'localhost', 8080);
  print('Server running on localhost:${server.port}');

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  shelf_router_generator: ^0.7.2+1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:shelf_router_generator/builder.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Dec 7, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.12.21


Detected platforms: other

Platform components identified in package: build, io, mirrors.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (analyzer).


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
analyzer >=0.35.0 <0.39.0 0.38.5 0.39.2
build ^1.0.0 1.2.2
build_config >=0.3.1 <=0.5.0 0.4.1+1
code_builder ^3.2.0 3.2.1
http_methods ^1.0.0 1.0.0
meta ^1.1.7 1.1.8
shelf ^0.7.3 0.7.5
shelf_router ^0.7.0 0.7.1
source_gen ^0.9.1 0.9.4+6
Transitive dependencies
args 1.5.2
async 2.4.0
built_collection 4.3.0
built_value 7.0.0
charcode 1.1.2
checked_yaml 1.0.2
collection 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
dart_style 1.3.3
fixnum 0.10.11
front_end 0.1.27 0.1.29
glob 1.2.0
html 0.14.0+3
http_parser 3.1.3
js 0.6.1+1
json_annotation 3.0.0
kernel 0.3.27 0.3.29
logging 0.11.3+2
matcher 0.12.6
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.1.0
path 1.6.4
pub_semver 1.4.2
pubspec_parse 0.1.5
quiver 2.1.2+1
source_span 1.5.5
stack_trace 1.9.3
stream_channel 2.0.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+13
yaml 2.2.0
Dev dependencies
build_runner ^1.4.0
build_verify ^1.1.1
http ^0.12.0+1
pedantic ^1.4.0 1.9.0
test ^1.5.3