justinfagnani / route

A client + server routing library for Dart
BSD 3-Clause "New" or "Revised" License
114 stars 40 forks source link

Add methods optional argument to filter #80

Open tusj opened 10 years ago

tusj commented 10 years ago

I'd like to apply a filter on certain methods. I have an authorization filter which I'd like to only work on GET and POST methods. The solution is not to add that check within the authorization method itself, but I feel that is much uglier.

How it has to be now:

Future<bool> filterAuth(HttpRequest r) {...}
...
var router = new Router(server) 
  ..filter(new RegExp(r'/auth/.*'), (HttpRequest r) {
      if (r.method != 'OPTIONS') {
        return filterAuth(r);
      }
       return new Future.value(true);
    })
...

The cleaner solution:

var router = new Router(server)
  ..filter(new RegExp(r'/auth/.*'), methods: ['GET', 'POST'], filterAuth);