diff --git a/README.md b/README.md index 991942ff..c26c18c6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # angel_framework -[![pub 1.0.0-dev.42](https://img.shields.io/badge/pub-1.0.0--dev.42-red.svg)](https://pub.dartlang.org/packages/angel_framework) +[![pub 1.0.0-dev.43](https://img.shields.io/badge/pub-1.0.0--dev.43-red.svg)](https://pub.dartlang.org/packages/angel_framework) [![build status](https://travis-ci.org/angel-dart/framework.svg)](https://travis-ci.org/angel-dart/framework) Core libraries for the Angel Framework. diff --git a/lib/src/http/routable.dart b/lib/src/http/routable.dart index 7206d878..493099b1 100644 --- a/lib/src/http/routable.dart +++ b/lib/src/http/routable.dart @@ -19,6 +19,26 @@ typedef Future RequestMiddleware(RequestContext req, ResponseContext res); /// A function that receives an incoming [RequestContext] and responds to it. typedef Future RequestHandler(RequestContext req, ResponseContext res); +/// Sequentially runs a list of [handlers] of middleware, and breaks if any does not +/// return `true`. Works well with [Router].chain. +RequestMiddleware waterfall(List handlers) { + for (var handler in handlers) { + if (handler is! RequestMiddleware && handler is! RequestHandler) + throw new ArgumentError( + '`waterfall` only accepts middleware and handlers. $handler is not a valid option.'); + } + + return (req, res) async { + for (var handler in handlers) { + var result = await handler(req, res); + + if (result != true) return result; + } + + return true; + }; +} + /// A routable server that can handle dynamic requests. class Routable extends Router { final Map _controllers = {}; diff --git a/pubspec.yaml b/pubspec.yaml index 1bb182a8..6da03e3f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_framework -version: 1.0.0-dev.42 +version: 1.0.0-dev.43 description: Core libraries for the Angel framework. author: Tobe O homepage: https://github.com/angel-dart/angel_framework