🚧 Experimental support for shelf_router using macros.
- ✨ 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
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/';
}
}
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);
}
}
Important
This package requires Dart SDK >= 3.5.0-164.0.dev
-
Download Dart from
dev
ormaster
channel- Dart SDK archive
- dvm: Dart Version Manager
- Alternatively, you can simply switch to flutter master channel:
$ flutter channel master
-
Add
package:shelf_router_macro
to yourpubspec.yaml
$ dart pub add shelf_router_macro
-
Enable experimental macros in
analysis_options.yaml
analyzer: enable-experiment: - macros
-
Use annotations provided by this library (see above example).
-
Run it
$ dart --enable-experiment=macros run lib/main.dart
All routes should be declared as part of a class annotated with @Controller()
:
@Controller()
class SomeController {
@Get('/')
String health() => 'ok';
}
Return shelf's Response:
@Get('/')
String health() => Response.ok(
'ok',
headers: { 'X-Test': 'test' },
);
Include shelf's Request in method signature:
@Get('/')
String test(Request r) => 'Headers: ${r.headers}';
===
TODO: add remaining examples.I've yet to decide on the format, I have to think about it some more.