Add groupAsync
This commit is contained in:
parent
745d75da67
commit
ddb7c6f1ee
4 changed files with 31 additions and 5 deletions
|
@ -1,3 +1,6 @@
|
|||
# 3.1.0
|
||||
* Add `Router.groupAsync`
|
||||
|
||||
# 3.0.6
|
||||
* Remove static default values for `middleware`.
|
||||
|
||||
|
|
|
@ -64,6 +64,12 @@ main() {
|
|||
(r) => r.id == reviewId);
|
||||
});
|
||||
}, middleware: [put, middleware, here]);
|
||||
|
||||
// Grouping can also take async callbacks.
|
||||
await router.groupAsync('/hello', (router) async {
|
||||
var name = await getNameFromFileSystem();
|
||||
router.get(name, (req, res) => '...');
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -1,18 +1,15 @@
|
|||
library angel_route.src.router;
|
||||
|
||||
import 'dart:async';
|
||||
import 'package:combinator/combinator.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:string_scanner/string_scanner.dart';
|
||||
|
||||
import '../string_util.dart';
|
||||
import 'routing_exception.dart';
|
||||
|
||||
part 'grammar.dart';
|
||||
|
||||
part 'route.dart';
|
||||
|
||||
part 'routing_result.dart';
|
||||
|
||||
part 'symlink_route.dart';
|
||||
|
||||
//final RegExp _param = RegExp(r':([A-Za-z0-9_]+)(\((.+)\))?');
|
||||
|
@ -175,6 +172,16 @@ class Router<T> {
|
|||
return mount(path, router)..name = name;
|
||||
}
|
||||
|
||||
/// Asynchronous equivalent of [group].
|
||||
Future<SymlinkRoute<T>> groupAsync(
|
||||
String path, FutureOr<void> callback(Router<T> router),
|
||||
{Iterable<T> middleware, String name}) async {
|
||||
middleware ??= <T>[];
|
||||
final router = Router<T>().._middleware.addAll(middleware);
|
||||
await callback(router);
|
||||
return mount(path, router)..name = name;
|
||||
}
|
||||
|
||||
/// Generates a URI string based on the given input.
|
||||
/// Handy when you have named routes.
|
||||
///
|
||||
|
@ -425,6 +432,16 @@ class _ChainedRouter<T> extends Router<T> {
|
|||
return mount(path, router)..name = name;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<SymlinkRoute<T>> groupAsync(
|
||||
String path, FutureOr<void> callback(Router<T> router),
|
||||
{Iterable<T> middleware, String name}) async {
|
||||
final router = _ChainedRouter<T>(
|
||||
_root, []..addAll(_handlers)..addAll(middleware ?? []));
|
||||
await callback(router);
|
||||
return mount(path, router)..name = name;
|
||||
}
|
||||
|
||||
@override
|
||||
SymlinkRoute<T> mount(String path, Router<T> router) {
|
||||
final route = super.mount(path, router);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
name: angel_route
|
||||
description: A powerful, isomorphic routing library for Dart. It is mainly used in the Angel framework, but can be used in Flutter and on the Web.
|
||||
version: 3.0.6
|
||||
version: 3.1.0
|
||||
author: Tobe O <thosakwe@gmail.com>
|
||||
homepage: https://github.com/angel-dart/angel_route
|
||||
environment:
|
||||
|
|
Loading…
Reference in a new issue