-
Notifications
You must be signed in to change notification settings - Fork 65
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
fix(#161): use() accepts array #162
base: main
Are you sure you want to change the base?
Conversation
|
Codecov Report
@@ Coverage Diff @@
## master #162 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 1 1
Lines 86 87 +1
=========================================
+ Hits 86 87 +1
Continue to review full report at Codecov.
|
Could you let me know some use cases around this? Is there any libraries that require this pattern? Otherwise, I think this can simply perform a ["/foo", "/bar"].map((base) => nc.use(base, fn)) |
The use case is for an internal library written for express to be used with next. I'd like to use next without modifying that library and without using a next custom server approach, and I was looking to this library to achieve that, as I read "drop-in replacement". |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your response. I left some comments.
I would be more than happy to merge this. There is just a few nits! |
b42c537
to
2806c87
Compare
@hoangvvo Ok sorry for the long long delay, but I've reworked this a bit. Should be much much simpler. Only handling one case here. This one:
Which a lot of middlewares expect. |
Thank you! So in this, the array middleware pattern only works if it is the first argument. Is it how it works in const mArr = [m1,m2,m3];
handler.use("/foo", mArr); |
@hoangvvo if you look at the express code, they support a lot of crazy things. All sorts of positions and levels of arrays. If you look at their types, it's similar, but not quite as open ended as their JavaScript. As an example, in their JavaScript, they use a recursive while loop lookup, just to find the first string argument. This is the reason I kept it simple. Because the most common use case in the express world, is the one I'm adding support for. The most common use case for middleware is to be added to the app without a route, though yes, they support arrays everywhere middleware is taken, at all levels. If you are interested in supporting exactly what express does, I can look into that, but it would most likely be just copying a lot of their code. What are you thinking? |
I saw your previous implementation support this pattern of having array in multiple places, but this implementation is obviously so much cleaner. I am okay with just supporting only the first argument. However, if we go with the other one, another hacky solution is to use Array.flat to just flatten out the array middlewares and process as expected. |
That's what express does, but along with some additional processing. Just want to get to know your thoughts here. |
If we would add support to this, I think it's better to support all positions. |
This should allow
use()
to behave more like express.use