Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: support middleware in shelf_router_generator #388

Open
aci8 opened this issue Oct 13, 2023 · 0 comments
Open

Proposal: support middleware in shelf_router_generator #388

aci8 opened this issue Oct 13, 2023 · 0 comments

Comments

@aci8
Copy link

aci8 commented Oct 13, 2023

Motivation

I would propose to add middleware support in the shelf_router_generator. Currently, to use the router_generator with middleware, you can write something like this:

@Route.get('/user/<userId>')
Future<Response> getUser(Request request, String userId) async {
    return Pipeline()
        .addMiddleware(logging)
        .addMiddleware(authorization)
        .addHandler((request) async {
            // do something with request and(!) userId variables
        })(request);
  }

This creates a lot of repetitive and boilerplate code for a routine (and quite frequent) action (the use of middleware). This code could be generated automatically.

Possible solution design

I see 2 possible paths:

  1. Add a Middleware Annotation (similar to the one described here Add middleware for handlers #338)
    In this case, the above code would look something like this:

    @Middleware(logging)
    @Middleware(authorization)
    @Route.get('/user/<userId>')
    Future<Response> getUser(Request request, String userId) async {
      // do something with request and userId variables
    }
  1. Add an annotation to the Route constructor parameter List<Middleware> middlewares
    In this case, code would look something like this:

    @Route.get('/user/<userId>' middlewares: [logging, authorization])
    Future<Response> getUser(Request request, String userId) async {
      // do something with request and userId variables
    }

The generated code could look something like this in both cases (such a construct is necessary to preserve the passing of the PATH parameters to the client handler):

Router _$UserServiceRouter(UserService service) {
  final router = Router();
  router.add(
    'GET',
    r'/user/<userId>',
    (Request request, String userId) => 
      Pipeline()
        .addMiddleware(logging)
        .addMiddleware(authorization)
        .addHandler((request) => service.getUser(request, userId))(request),
    // or just: (logging(authorization((request) => service.getUser(request, userId))))(request)
  );
  return router;
}

I believe the second solution (with the middlewares parameter) is more successful and I could attempt to make the necessary changes and create a pull request myself.

Is this a good idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant