-
Notifications
You must be signed in to change notification settings - Fork 152
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
Comments
Hi @rubenferreira97 👋 Handler middleware(Handler handler) {
return (context) {
final request = context.request;
// Do something with the request
return handler(context);
};
} |
@felangel Thanks for you answer! Sorry if I am missing something but first I guess you meant
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);
};
} |
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);
};
} |
@niklasbartsch In that example I can't access the variable value. |
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
:Index
/[test]/index.dart
:The text was updated successfully, but these errors were encountered: