-
Notifications
You must be signed in to change notification settings - Fork 125
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
Add support for route middleware #87
Comments
This is completely possible. Just to confirm the format: api.post(
'/user',
authenticate(),
authorize({ role: 'admin' }),
validate({ body: { /*...schema...*/ } }),
async (req, res) => {
/* business logic */
return {}
}
) What would the signature be of these factory functions? Middleware in Lambda API requires the acceptance of three parameters, |
I was just about to add a similar request because I wanted some of my middleware to only operate on a particular I was thinking that the However it looks like this request would also satisfy my request? |
and my 2cents would be exactly as you describe - I would expect my middleware function to have to have the same signature whether I include it like the OP suggested or if I include it using the |
Not sure we would want to allow If that is how it is envisioned, then it should be fairly simple to implement. |
Yes, that's exactly what I had in mind. Middleware is middleware, whether
it's used in use() or in METHOD().
I see in the current implementation middleware processing and route
handling are implemented independently. The simplest solution would be to
add route middleware processing into route handling. But then you end up
with middleware being processed in two places: one for use() middleware and
one for route middleware. Do you think it is worth refactoring towards a
more unified approach, where use() middleware is also processed as part of
route handling?
…On Thu, Dec 20, 2018 at 8:48 AM Jeremy Daly ***@***.***> wrote:
Not sure we would want to allow next() to be passed into route methods,
but if route specific middleware uses the same signature as use(), then
it's entirely possible to read the last parameter as the handler and then
any proceeding functions as middleware. The middleware would be passed the
REQUEST, RESPONSE and next() parameters and would control the execution
flow just like normal middleware.
If that is how it is envisioned, then it should be fairly simple to
implement.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#87 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABwi8_d5F1wezl0HzY60ShMEDelKOoGdks5u679EgaJpZM4Za4zn>
.
|
We could move middleware processing into the route, but mappings are all preprocessed anyway. So, it might be better to add method filtering to middleware, and then just map it correctly when processing route declarations. That way middleware processing doesn't need to change AND you get the benefit of adding I'm thinking this is just a convenience method, and really doesn't change the underlying processing too much. |
You're right, that's probably the simplest.
It just seems a little inelegant to me having routing logic implemented
twice: once to lookup the applicable middleware for an incoming request,
and once to lookup the handler. Also, with the style of declaring
middleware in route declarations, the list of registered middleware
functions could get quite large - and it gets iterated over on every
request. This seems a shame given that you already have the *_routes* tree
data structure for efficient handler lookup.
The routing logic could be unified by including middleware in the
*_routes* data
structure (instead of having a separate *_middleware* list). Then you'd
get efficient lookup of both middleware and handler.
If you do want to leave the existing middleware implementation as it is, it
would be possible to use *_routes* for route middleware only. Or, as you
suggest, add method filtering to the existing implementation.
Let me know what you think.
…On Thu, Dec 20, 2018 at 10:45 AM Jeremy Daly ***@***.***> wrote:
We *could* move middleware processing into the route, but mappings are
all preprocessed anyway. So, it might be better to add method filtering to
middleware, and then just map it correctly when processing route
declarations. That way middleware processing doesn't need to change AND you
get the benefit of adding use to restrict methods if you wanted to do it
that way.
I'm thinking this is just a convenience method, and really doesn't change
the underlying processing too much.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#87 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABwi83A-MoNO9-9ZIVwiIsjUhu_ZMuxWks5u69q9gaJpZM4Za4zn>
.
|
Hmm, I see what you mean. Could be useful to use the nested routes tree to store middleware mappings. I'd need to think about how that would work for global middleware though as we'd have to handle wildcard routes too. I wouldn't want global middleware to be tied to each route though, seems as though we could do that higher up the tree. |
Agreed. Global middleware should be attached to the root node (i.e. path
/*). Other wildcard middleware is attached to the appropriate node as
wildcard middleware (not route specific).
When the tree is walked for an incoming request, applicable wildcard
middleware (including global) is executed as you go. If a matching route
is found, its middleware (global then route specific) is executed before
the handler.
…On Thu, Dec 20, 2018 at 12:22 PM Jeremy Daly ***@***.***> wrote:
Hmm, I see what you mean. Could be useful to use the nested routes tree to
store middleware mappings. I'd need to think about how that would work for
global middleware though as we'd have to handle wildcard routes too. I
wouldn't want global middleware to be tied to each route though, seems as
though we could do that higher up the tree.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#87 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABwi88BeoC9kKXagtRbjFAXn7ijRr3Loks5u6_GJgaJpZM4Za4zn>
.
|
I'm thinking about changing the entire execution stack so that even the 'handler route' is just like another piece of middleware, similar to how Express creates their middleware stacks. They essentially have the same signatures (minus the Need to think about maintaining processing order though, especially for wildcard and other global routes. |
|
Thanks! |
Allow middleware to be added per route in order to support this style of route declaration:
...where
authenticate
,authorize
andvalidate
are middleware (factory) functions.The text was updated successfully, but these errors were encountered: