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

feat: Allow routing parameters in middleware #773

Open
rubenferreira97 opened this issue Jul 18, 2023 · 4 comments · May be fixed by #1262
Open

feat: Allow routing parameters in middleware #773

rubenferreira97 opened this issue Jul 18, 2023 · 4 comments · May be fixed by #1262

Comments

@rubenferreira97
Copy link

rubenferreira97 commented Jul 18, 2023

Description
Currently is not possible to access routing parameters on middlewares. Would be nice in cases where we have nested routing parameters and have different logic dependent on that parameter (e.g. /[userId]/tasks/[taskId], middleware on /[userId]/_middleware.dart that checks if we have permissions for that [userId) .

My current workaround relies on private implementations 😕:

Middleware /[test]/_middleware.dart:

import 'package:dart_frog/dart_frog.dart';
import 'package:dart_frog/src/_internal.dart' show toShelfHandler;

Handler middleware(Handler handler /* String test */) { // `String test` does not work
  return fromShelfHandler((request) async {
    print((request.context['shelf_router/params'] as Map<String, String>?)?['test']);
    return toShelfHandler(handler)(request);
  });
}

Index /[test]/index.dart:

Future<Response> onRequest(RequestContext context, String test) async {
  return Response(body: '$test');
}
@felangel
Copy link
Contributor

felangel commented Jul 18, 2023

Hi @rubenferreira97 👋
This is already possible:

Handler middleware(Handler handler) {
  return (context) {
    final request = context.request;
    // Do something with the request
    return handler(context);
  };
}

@felangel felangel self-assigned this Jul 18, 2023
@rubenferreira97
Copy link
Author

rubenferreira97 commented Jul 18, 2023

@felangel Thanks for you answer!

Sorry if I am missing something but first I guess you meant context.request instead of request.context, second neither request or context expose routing params as far as I know.

Middleware /[test]/_middleware.dart:

Handler middleware(Handler handler) {
  return (context) {
    final request = context.request;
    print(request['test']); // does not work
    print(context['test']); // does not work
    // Do something with the request
    return handler(context);
  };
}

In a ideal scenario I would like to access the variable in a typesafe manner via the middleware params, however I don't know if this is possible. I imagine middlewares are different from requests.

Handler middleware(Handler handler, String test) {
  return (context /*, test */) { // or here?
    print(test);
    return handler(context);
  };
}

@niklasbartsch
Copy link

You can do the following:

Handler middleware(Handler handler) {
  return (context) {
        final uri = context.request.uri;

        if (uri.pathSegments.contains('test')) {
          return Response(body: 'OK');
        }
        return handler(context);
      };
}

@rubenferreira97
Copy link
Author

@niklasbartsch In that example I can't access the variable value.

@sun-jiao sun-jiao linked a pull request Jan 28, 2024 that will close this issue
7 tasks
@tomarra tomarra removed the question label Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Needs Triage
Development

Successfully merging a pull request may close this issue.

4 participants