shelf_routing_generator 0.1.0 copy "shelf_routing_generator: ^0.1.0" to clipboard
shelf_routing_generator: ^0.1.0 copied to clipboard

Generator for routing class methods with support for path parameters, query parameters, body and headers.

example/example.dart

// ignore_for_file: unreachable_from_main

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

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

class User {
  final int id;
  final String name;

  const User({
    required this.id,
    required this.name,
  });

  factory User.fromJson(Map<String, dynamic> map) => User(id: map['id'], name: map['name']);
  Map<String, dynamic> toJson() => {'id': id, 'name': name};
}

@Routable(prefix: '/users')
class UserController {
  // Create router using the generate function defined in 'example.g.dart'.
  static Router get router => _$userControllerRouter;

  final DatabaseConnection connection;

  UserController(this.connection);

  @Route.get('/')
  Future<List<dynamic>> listUsers(Request request, {String? query}) async {
    return ['user1'];
  }

  @Route.get('/<userId>')
  Future<Response> fetchUser(Request request, int userId) async {
    if (userId == 1) {
      return Response.ok('user1');
    }
    return Response.notFound('no such user');
  }

  @Route.post('/')
  Future<JsonResponse<User>> createUser(Request request, User user) async {
    if (user.name.isEmpty) {
      return JsonResponse.badRequest(body: 'Missing name field');
    }
    return JsonResponse.ok(user);
  }
}

// Create router using the generate function defined in 'example.g.dart'.
@GenerateRouterFor([UserController])
Router get apiRouter => _$apiRouter;

class DatabaseConnection {
  static Future<DatabaseConnection> connect(String _) => throw UnimplementedError();
}
0
likes
140
pub points
0%
popularity

Publisher

unverified uploader

Generator for routing class methods with support for path parameters, query parameters, body and headers.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

analyzer, build, collection, dart_style, glob, path, recase, shelf, shelf_router, shelf_routing, source_gen

More

Packages that depend on shelf_routing_generator