fix(combine/every): make every
middleware work with short-circuiting middlewares
#3441
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When using
every
middleware to compose middlewares that short-circuits the chain (i.e. return aResponse
object, like e.g.zod-validator
, but I had also issues with e.g.cors
with OPTIONS requests) and, as a result, don't callnext()
, it was throwing an errorError: Context is not finalized. Did you forget to return a Response object or await next()?
. This fixes the issue and makes it possible to use it with (probably?) any middleware.As a side note, I attempted to simplify the
compose
's middleware signature (and I did) as it could easily accept an array of functions/handlers and simplify the code. But I think the signature is so complicated because of the usage indispatch
method of theHono
class and that would require mapping to an array of handlers which would probably slightly worsen the overall performance (not sure how to measure that but presumably 0Array.prototype.map
s is better than 1) and that method seems to be rather critical. If you think it's worth considering I'll submit another PR, but it's probably not worth it.The author should do the following, if applicable
bun run format:fix && bun run lint:fix
to format the code