1.0.1
This commit is contained in:
parent
6814229106
commit
1b22c8758a
4 changed files with 52 additions and 8 deletions
|
@ -1,6 +1,6 @@
|
||||||
# angel_route
|
# angel_route
|
||||||
|
|
||||||
![version 1.0.0](https://img.shields.io/badge/version-1.0.0-brightgreen.svg)
|
![version 1.0.1](https://img.shields.io/badge/version-1.0.1-brightgreen.svg)
|
||||||
![build status](https://travis-ci.org/angel-dart/route.svg)
|
![build status](https://travis-ci.org/angel-dart/route.svg)
|
||||||
|
|
||||||
A powerful, isomorphic routing library for Dart.
|
A powerful, isomorphic routing library for Dart.
|
||||||
|
|
|
@ -16,6 +16,7 @@ final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)');
|
||||||
|
|
||||||
/// An abstraction over complex [Route] trees. Use this instead of the raw API. :)
|
/// An abstraction over complex [Route] trees. Use this instead of the raw API. :)
|
||||||
class Router extends Extensible {
|
class Router extends Extensible {
|
||||||
|
final List<_ChainedRouter> _chained = [];
|
||||||
final List _middleware = [];
|
final List _middleware = [];
|
||||||
final Map<Pattern, Router> _mounted = {};
|
final Map<Pattern, Router> _mounted = {};
|
||||||
final List<Route> _routes = [];
|
final List<Route> _routes = [];
|
||||||
|
@ -31,7 +32,13 @@ class Router extends Extensible {
|
||||||
/// Additional filters to be run on designated requests.
|
/// Additional filters to be run on designated requests.
|
||||||
Map<String, dynamic> requestMiddleware = {};
|
Map<String, dynamic> requestMiddleware = {};
|
||||||
|
|
||||||
List<Route> get routes => new List<Route>.unmodifiable(_routes);
|
List<Route> get routes {
|
||||||
|
var result = []..addAll(_routes);
|
||||||
|
|
||||||
|
for (var piped in _chained) result.addAll(piped.routes);
|
||||||
|
|
||||||
|
return new List<Route>.unmodifiable(result);
|
||||||
|
}
|
||||||
|
|
||||||
/// Provide a `root` to make this Router revolve around a pre-defined route.
|
/// Provide a `root` to make this Router revolve around a pre-defined route.
|
||||||
/// Not recommended.
|
/// Not recommended.
|
||||||
|
@ -57,13 +64,22 @@ class Router extends Extensible {
|
||||||
return route.._path = _pathify(path);
|
return route.._path = _pathify(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_ChainedRouter _addChained(_ChainedRouter piped) {
|
||||||
|
// mount('/', piped);
|
||||||
|
_chained.add(piped);
|
||||||
|
return piped;
|
||||||
|
}
|
||||||
|
|
||||||
/// Prepends the given middleware to any routes created
|
/// Prepends the given middleware to any routes created
|
||||||
/// by the resulting router.
|
/// by the resulting router.
|
||||||
///
|
///
|
||||||
/// [middleware] can be either an `Iterable`, or a single object.
|
/// [middleware] can be either an `Iterable`, or a single object.
|
||||||
///
|
///
|
||||||
/// The resulting router can be chained, too.
|
/// The resulting router can be chained, too.
|
||||||
_ChainedRouter chain(middleware) => new _ChainedRouter(this, middleware);
|
_ChainedRouter chain(middleware) {
|
||||||
|
var piped = new _ChainedRouter(this, middleware);
|
||||||
|
return _addChained(piped);
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns a [Router] with a duplicated version of this tree.
|
/// Returns a [Router] with a duplicated version of this tree.
|
||||||
Router clone() {
|
Router clone() {
|
||||||
|
@ -142,7 +158,6 @@ class Router extends Extensible {
|
||||||
String namespace: null}) {
|
String namespace: null}) {
|
||||||
final router = new Router().._middleware.addAll(middleware);
|
final router = new Router().._middleware.addAll(middleware);
|
||||||
callback(router..debug = debug);
|
callback(router..debug = debug);
|
||||||
|
|
||||||
return mount(path, router, namespace: namespace).._name = name;
|
return mount(path, router, namespace: namespace).._name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -420,10 +435,20 @@ class _ChainedRouter extends Router {
|
||||||
@override
|
@override
|
||||||
Route addRoute(String method, Pattern path, handler,
|
Route addRoute(String method, Pattern path, handler,
|
||||||
{List middleware: const []}) {
|
{List middleware: const []}) {
|
||||||
return _root.addRoute(method, path, handler,
|
return super.addRoute(method, path, handler,
|
||||||
middleware: []..addAll(_handlers)..addAll(middleware ?? []));
|
middleware: []..addAll(_handlers)..addAll(middleware ?? []));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SymlinkRoute group(Pattern path, void callback(Router router),
|
||||||
|
{Iterable middleware: const [],
|
||||||
|
String name: null,
|
||||||
|
String namespace: null}) {
|
||||||
|
final router =
|
||||||
|
new _ChainedRouter(_root, []..addAll(_handlers)..addAll(middleware));
|
||||||
|
callback(router..debug = debug);
|
||||||
|
return mount(path, router, namespace: namespace).._name = name;
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
SymlinkRoute mount(Pattern path, Router router,
|
SymlinkRoute mount(Pattern path, Router router,
|
||||||
{bool hooked: true, String namespace: null}) {
|
{bool hooked: true, String namespace: null}) {
|
||||||
|
@ -439,6 +464,6 @@ class _ChainedRouter extends Router {
|
||||||
piped._handlers.addAll([]
|
piped._handlers.addAll([]
|
||||||
..addAll(_handlers)
|
..addAll(_handlers)
|
||||||
..addAll(middleware is Iterable ? middleware : [middleware]));
|
..addAll(middleware is Iterable ? middleware : [middleware]));
|
||||||
return piped;
|
return _addChained(piped);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
name: angel_route
|
name: angel_route
|
||||||
description: A powerful, isomorphic routing library for Dart.
|
description: A powerful, isomorphic routing library for Dart.
|
||||||
version: 1.0.0
|
version: 1.0.1
|
||||||
author: Tobe O <thosakwe@gmail.com>
|
author: Tobe O <thosakwe@gmail.com>
|
||||||
homepage: https://github.com/angel-dart/angel_route
|
homepage: https://github.com/angel-dart/angel_route
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
|
|
19
test/chain_nest_test.dart
Normal file
19
test/chain_nest_test.dart
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
import 'package:angel_route/angel_route.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
main() {
|
||||||
|
var router = new Router()
|
||||||
|
..chain('a').group('/b', (router) {
|
||||||
|
router.chain('c').chain('d').group('/e', (router) {
|
||||||
|
router.get('f', 'g');
|
||||||
|
});
|
||||||
|
})
|
||||||
|
..dumpTree();
|
||||||
|
|
||||||
|
test('nested route groups with chain', () {
|
||||||
|
var r = router.resolveAbsolute('/b/e/f')?.route;
|
||||||
|
expect(r, isNotNull);
|
||||||
|
expect(r.handlers, hasLength(4));
|
||||||
|
expect(r.handlers, equals(['a', 'c', 'd', 'g']));
|
||||||
|
});
|
||||||
|
}
|
Loading…
Reference in a new issue