dart-neats icon indicating copy to clipboard operation
dart-neats copied to clipboard

Update Route.mount to accept handler

Open CodeDoctorDE opened this issue 4 years ago • 9 comments
trafficstars

To add a middleware on the router, you need a pipeline which is an type of handler. But currently you can only use routers for a mount: The shelf_router.Route.mount annotation can only be used on a getter that returns shelf_router.Router (https://github.com/google/dart-neats/blob/6c1d4685b1598d22e829109276bf4fb7d585535a/shelf_router_generator/lib/src/shelf_router_generator.dart#L298).

It would be awesome if https://github.com/google/dart-neats/blob/6c1d4685b1598d22e829109276bf4fb7d585535a/shelf_router_generator/lib/src/shelf_router_generator.dart#L32 accepts Handler, like the method in shelf_router: https://github.com/google/dart-neats/blob/6c1d4685b1598d22e829109276bf4fb7d585535a/shelf_router/lib/src/router.dart#L149

CodeDoctorDE avatar Jul 07 '21 18:07 CodeDoctorDE

Should I create a pull request?

CodeDoctorDE avatar Jul 07 '21 18:07 CodeDoctorDE

okay, i think it's too much for me, isAssignableFrom doesn't work with function typedefs

CodeDoctorDE avatar Jul 07 '21 19:07 CodeDoctorDE

@jonasfj What do you think?

CodeDoctorDE avatar Jul 11 '21 16:07 CodeDoctorDE

Let's first determine if we want this in shelf_router, then make a solution in shelf_router_generator.

How is this different from using Router.all? And is not likely that we're going to want something more like router.middleware(...)?

jonasfj avatar Jul 15 '21 10:07 jonasfj

The difference between Router.all and Router.mount is, that Router.mount removes the path of the router before. For example, you have the path /auth/sign-in, the Router.all gives the full path instead of only /sign-in at Router.mount. I saw the sourecode and shelf_router already supports a normal handler: https://github.com/google/dart-neats/blob/6c1d4685b1598d22e829109276bf4fb7d585535a/shelf_router/lib/src/router.dart#L149

CodeDoctorDE avatar Jul 16 '21 08:07 CodeDoctorDE

I can see the point that allowing the shelf_router.Route.mount to wrap a Handler wouldn't be so bad :smile:

Especially, when we already have the support in shelf_router, it wouldn't be a breaking change, it just means tweaking the code-generator to accept both Router and Handler. I think I'm down with that!

I think we should do this, since it only affects the code-generator. @CodeDoctorDE, if you want to take a stab at I'm happy to reviews.


Whether or not this is the best way to add middleware is a good question, we can discuss that in https://github.com/google/dart-neats/issues/32

jonasfj avatar Jul 16 '21 12:07 jonasfj

It would be awesome. You only need to wrap the router in a pipeline and have the route.mount annotation. I think I cannot contribute because I haven't much experience by making code generators. I'm stuck at the point where I want to check if the return type is a handler

CodeDoctorDE avatar Jul 16 '21 12:07 CodeDoctorDE

Any updates?

CodeDoctorDE avatar Nov 20 '21 18:11 CodeDoctorDE

No update, this is labeled as an enhancement. I don't think it's critical, if someone does I'm willing to help with code reviews.

But as far as I'm aware nobody is working on this.

jonasfj avatar Nov 20 '21 20:11 jonasfj