Skip to content

eEQK/shelf_router_macro

Repository files navigation

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/';
  }
}
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);
  }
}

🚀 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
  2. Add package:shelf_router_macro to your pubspec.yaml

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

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

  5. Run it

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

🙌 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';
}

Customizing response:

Return shelf's Response:

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

Accessing request details:

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.

About

Experimental support for shelf_router using macros

Resources

License

Stars

Watchers

Forks

Packages

No packages published