shelf_router_macro 0.0.0-dev.7 copy "shelf_router_macro: ^0.0.0-dev.7" to clipboard
shelf_router_macro: ^0.0.0-dev.7 copied to clipboard

Experimental Dart package that is a thin wrapper for shelf_router using macros

shelf_router_macro #

build pub package License: MIT

🚧 Experimental support for shelf_router using macros.

🌟 Features #

  • ✨ Route declarations
  • ✨ Route parameters
  • ✨ Async routes
  • ✨ Lightweight - no additional dependencies beyond shelf_router
  • 🖊️ In Progress Intuitive - custom return types
  • 🖊️ In Progress Minimalistic - no need to specify Request/Response, just return response body

🧑‍💻 Examples #

import 'package:data_class_macro/data_class_macro.dart';

@Controller()
class GreetingController {
  @Get('/wave')
  Future<String> wave() async {
    await Future.delayed(const Duration(seconds: 1));
    return '_o/';
  }
}
copied to clipboard
import 'package:json/json.dart';
import 'package:data_class_macro/data_class_macro.dart';

@JsonCodable()
class Pong {
  Pong({required this.uid});

  final String uid;
}

@Controller()
class PingController {
  @Get('/ping/<uid>')
  Future<Pong> ping(String uid) async {
    return Pong(uid: uid);
  }
}
copied to clipboard

🚀 Quick Start #

Important

This package requires Dart SDK >= 3.5.0-164.0.dev

  1. Download Dart from dev or master channel

    $ flutter channel master
    
    copied to clipboard
  2. Add package:shelf_router_macro to your pubspec.yaml

    $ dart pub add shelf_router_macro
    
    copied to clipboard
  3. Enable experimental macros in analysis_options.yaml

    analyzer:
      enable-experiment:
        - macros
    
    copied to clipboard
  4. Use annotations provided by this library (see above example).

  5. Run it

    $ dart --enable-experiment=macros run lib/main.dart
    
    copied to clipboard

🙌 Hands-on guide #

Defining routes: #

All routes should be declared as part of a class annotated with @Controller():

@Controller()
class SomeController {
  @Get('/')
  String health() => 'ok';
}
copied to clipboard

Customizing response: #

Return shelf's Response:

@Get('/')
String health() => Response.ok(
    'ok', 
    headers: { 'X-Test': 'test' },
  );
copied to clipboard

Accessing request details: #

Include shelf's Request in method signature:

@Get('/')
String test(Request r) => 'Headers: ${r.headers}';
copied to clipboard

===

TODO: add remaining examples.I've yet to decide on the format, I have to think about it some more.

1
likes
150
points
32
downloads

Publisher

verified publishereeqk.codes

Weekly Downloads

2024.09.26 - 2025.04.10

Experimental Dart package that is a thin wrapper for shelf_router using macros

Repository (GitHub)

Topics

#server #shelf #macros

Documentation

API reference

License

MIT (license)

Dependencies

collection, macros, meta, shelf, shelf_router

More

Packages that depend on shelf_router_macro