Add groupAsync

This commit is contained in:
Tobe O 2019-11-28 12:46:57 -05:00
parent 745d75da67
commit ddb7c6f1ee
4 changed files with 31 additions and 5 deletions

View file

@ -1,3 +1,6 @@
# 3.1.0
* Add `Router.groupAsync`
# 3.0.6
* Remove static default values for `middleware`.

View file

@ -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) => '...');
});
}
```

View file

@ -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);

View file

@ -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: