diff --git a/README.md b/README.md index 2d31d818..c89fe153 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 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) A powerful, isomorphic routing library for Dart. diff --git a/lib/src/router.dart b/lib/src/router.dart index f8145891..861cf885 100644 --- a/lib/src/router.dart +++ b/lib/src/router.dart @@ -16,6 +16,7 @@ final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)'); /// An abstraction over complex [Route] trees. Use this instead of the raw API. :) class Router extends Extensible { + final List<_ChainedRouter> _chained = []; final List _middleware = []; final Map _mounted = {}; final List _routes = []; @@ -31,7 +32,13 @@ class Router extends Extensible { /// Additional filters to be run on designated requests. Map requestMiddleware = {}; - List get routes => new List.unmodifiable(_routes); + List get routes { + var result = []..addAll(_routes); + + for (var piped in _chained) result.addAll(piped.routes); + + return new List.unmodifiable(result); + } /// Provide a `root` to make this Router revolve around a pre-defined route. /// Not recommended. @@ -57,13 +64,22 @@ class Router extends Extensible { return route.._path = _pathify(path); } + _ChainedRouter _addChained(_ChainedRouter piped) { + // mount('/', piped); + _chained.add(piped); + return piped; + } + /// Prepends the given middleware to any routes created /// by the resulting router. - /// + /// /// [middleware] can be either an `Iterable`, or a single object. /// /// 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. Router clone() { @@ -142,7 +158,6 @@ class Router extends Extensible { String namespace: null}) { final router = new Router().._middleware.addAll(middleware); callback(router..debug = debug); - return mount(path, router, namespace: namespace).._name = name; } @@ -420,10 +435,20 @@ class _ChainedRouter extends Router { @override Route addRoute(String method, Pattern path, handler, {List middleware: const []}) { - return _root.addRoute(method, path, handler, + return super.addRoute(method, path, handler, 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 SymlinkRoute mount(Pattern path, Router router, {bool hooked: true, String namespace: null}) { @@ -439,6 +464,6 @@ class _ChainedRouter extends Router { piped._handlers.addAll([] ..addAll(_handlers) ..addAll(middleware is Iterable ? middleware : [middleware])); - return piped; + return _addChained(piped); } } diff --git a/pubspec.yaml b/pubspec.yaml index 1cbe3317..59684695 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: angel_route description: A powerful, isomorphic routing library for Dart. -version: 1.0.0 +version: 1.0.1 author: Tobe O homepage: https://github.com/angel-dart/angel_route dev_dependencies: diff --git a/test/chain_nest_test.dart b/test/chain_nest_test.dart new file mode 100644 index 00000000..754dc18b --- /dev/null +++ b/test/chain_nest_test.dart @@ -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'])); + }); +}