rest_route 0.0.5 copy "rest_route: ^0.0.5" to clipboard
rest_route: ^0.0.5 copied to clipboard

Manage API routes in Flutter efficiently, ensuring type safety, cleaner code, and easier maintenance for RESTful APIs

example/main.dart

// Define all your routes in one place
import 'package:rest_route/rest_route.dart';

class FakeDio {
  Future<void> get(String path) async {}
  Future<void> post(String path) async {}
}

abstract class ApiRoutes {
  static final users = UserRoute();
}

// Multi-level nested route example
class UserRoute extends RestRoute<UserRoute> with RestfulMixin {
  UserRoute([String routePath = '']) : super('users', routePath);

  late final posts = PostsRoute(this);
  late final settings = SimpleNestedRoute(this, 'settings');

  @override
  UserRoute copyWith(String newPath) => UserRoute(newPath);
}

class PostsRoute extends NestedRoute<UserRoute, PostsRoute> with RestfulMixin {
  PostsRoute(this.parent, [String routePath = ''])
      : super(parent, 'posts', routePath);

  final UserRoute parent;

  late final comments = CommentsRoute(this);

  @override
  PostsRoute copyWith(String newPath) => PostsRoute(parent, newPath);
}

class CommentsRoute extends NestedRoute<PostsRoute, CommentsRoute>
    with RestfulMixin {
  CommentsRoute(this.parent, [String routePath = ''])
      : super(parent, 'comments', routePath);

  final PostsRoute parent;

  String get featured => join('featured');
  String get report => join('report');

  @override
  CommentsRoute copyWith(String newPath) => CommentsRoute(parent, newPath);
}

// Using in your code
void apiCalls() async {
  final dio = FakeDio(); // Your HTTP client

  // User endpoints with RestfulMixin
  await dio.get(ApiRoutes.users.list); // "users"
  await dio.post(ApiRoutes.users.create); // "users"
  await dio.get(ApiRoutes.users.get(123)); // "users/123"

  // Nested routes
  await dio.get(ApiRoutes.users(123).posts.list); // "users/123/posts"
  await dio.get(ApiRoutes.users(123)
      .posts(5)
      .comments
      .featured); // "users/123/posts/5/comments/featured"

  // Using SimpleNestedRoute
  await dio.get(ApiRoutes.users(123).settings.path); // "users/123/settings"

  // With query parameters
  await dio.get(ApiRoutes.users.withQueryParams(
      {"role": "admin", "page": 1})); // "users?role=admin&page=1"
}
0
likes
160
points
24
downloads

Publisher

unverified uploader

Weekly Downloads

Manage API routes in Flutter efficiently, ensuring type safety, cleaner code, and easier maintenance for RESTful APIs

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on rest_route