shelf_router_generator 0.7.2+2

  • 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.

dependencies:
  shelf: ^0.7.5
  shelf_router: ^0.7.0+1
dev_dependencies:
  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;
  UserService(this.connection);

  @Route.get('/users/')
  Future<Response> listUsers(Request request) async {
    return Response.ok('["user1"]');
  }

  @Route.get('/users/<userId>')
  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+2 #

  • Relax dependency constraint on analyzer.

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 README.md with references to package build_runner.

v0.7.0 #

  • Initial release

example/main.dart

// 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
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// 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.
  @Route.get('/say-hi/<name>')
  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.
  @Route.get('/user/<userId|[0-9]+>')
  Response _user(Request request, String userId) {
    return Response.ok('User has the user-number: $userId');
  }

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

  // Other routers can be mounted...
  @Route.mount('/api/')
  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.
  @Route.all('/<ignored|.*>')
  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 :)
  @Route.get('/messages')
  @Route.get('/messages/')
  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.
  @Route.all('/<ignored|.*>')
  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:


dependencies:
  shelf_router_generator: ^0.7.2+2

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';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
48
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
74
Learn more about scoring.

We analyzed this package on Jul 2, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.9+1

Analysis suggestions

Package not compatible with SDK flutter

Because it is not compatible with any of the supported runtimes: flutter-native, flutter-web

Package not compatible with runtime flutter-native of android

Because of the import of dart:mirrors via the import chain package:shelf_router_generator/builder.dart->package:shelf_router_generator/src/shelf_router_generator.dart->package:source_gen/source_gen.dart->package:source_gen/src/type_checker.dart->dart:mirrors

Package not compatible with runtime flutter-native of ios

Because of the import of dart:mirrors via the import chain package:shelf_router_generator/builder.dart->package:shelf_router_generator/src/shelf_router_generator.dart->package:source_gen/source_gen.dart->package:source_gen/src/type_checker.dart->dart:mirrors

Package not compatible with runtime flutter-native of linux

Because of the import of dart:mirrors via the import chain package:shelf_router_generator/builder.dart->package:shelf_router_generator/src/shelf_router_generator.dart->package:source_gen/source_gen.dart->package:source_gen/src/type_checker.dart->dart:mirrors

Package not compatible with runtime flutter-native of macos

Because of the import of dart:mirrors via the import chain package:shelf_router_generator/builder.dart->package:shelf_router_generator/src/shelf_router_generator.dart->package:source_gen/source_gen.dart->package:source_gen/src/type_checker.dart->dart:mirrors

Package not compatible with runtime flutter-native of windows

Because of the import of dart:mirrors via the import chain package:shelf_router_generator/builder.dart->package:shelf_router_generator/src/shelf_router_generator.dart->package:source_gen/source_gen.dart->package:source_gen/src/type_checker.dart->dart:mirrors

Package not compatible with runtime flutter-web of web

Because of the import of dart:io via the import chain package:shelf_router_generator/builder.dart->package:shelf_router_generator/src/shelf_router_generator.dart->package:source_gen/source_gen.dart->package:source_gen/src/utils.dart->package:build/build.dart->package:build/src/generate/run_post_process_builder.dart->package:build/src/builder/post_process_builder.dart->package:build/src/builder/builder.dart->package:build/src/builder/build_step.dart->package:build/src/asset/reader.dart->package:glob/glob.dart->package:glob/src/list_tree.dart->package:glob/src/io.dart->package:glob/src/io_export.dart->dart:io

Package not compatible with runtime native-aot

Because of the import of dart:mirrors via the import chain package:shelf_router_generator/builder.dart->package:shelf_router_generator/src/shelf_router_generator.dart->package:source_gen/source_gen.dart->package:source_gen/src/type_checker.dart->dart:mirrors

Package not compatible with runtime web

Because of the import of dart:io via the import chain package:shelf_router_generator/builder.dart->package:shelf_router_generator/src/shelf_router_generator.dart->package:source_gen/source_gen.dart->package:source_gen/src/utils.dart->package:build/build.dart->package:build/src/generate/run_post_process_builder.dart->package:build/src/builder/post_process_builder.dart->package:build/src/builder/builder.dart->package:build/src/builder/build_step.dart->package:build/src/asset/reader.dart->package:glob/glob.dart->package:glob/src/list_tree.dart->package:glob/src/io.dart->package:glob/src/io_export.dart->dart:io

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
analyzer >=0.35.0 <0.40.0 0.39.11
build ^1.0.0 1.3.0
build_config >=0.3.1 <=0.5.0 0.4.2
code_builder ^3.2.0 3.3.0
http_methods ^1.0.0 1.0.0
meta ^1.1.7 1.1.8 1.2.0
shelf ^0.7.3 0.7.7
shelf_router ^0.7.0 0.7.2
source_gen ^0.9.1 0.9.5
Transitive dependencies
_fe_analyzer_shared 5.0.0
args 1.6.0
async 2.4.1
built_collection 4.3.2
built_value 7.1.0
charcode 1.1.3
checked_yaml 1.0.2
collection 1.14.13
convert 2.1.1
crypto 2.1.5
csslib 0.16.1
dart_style 1.3.6
fixnum 0.10.11
glob 1.2.0
html 0.14.0+3
http_parser 3.1.4
js 0.6.2
json_annotation 3.0.1
logging 0.11.4
matcher 0.12.8
node_interop 1.1.1
node_io 1.1.1
package_config 1.9.3
path 1.7.0
pub_semver 1.4.4
pubspec_parse 0.1.5
quiver 2.1.3
source_span 1.7.0
stack_trace 1.9.5
stream_channel 2.0.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.2.0
watcher 0.9.7+15
yaml 2.2.1
Dev dependencies
build_runner ^1.4.0
build_verify ^1.1.1
http ^0.12.0+1
pedantic ^1.4.0 1.9.0 1.9.1
test ^1.5.3