WIP: Experimental and ugly middleware support #267
Closed
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.
Motivation
Middlewares are a very flexible way of composing behaviour on web servers. Currently, mirage lacks support for it and it would be especially cool for plugins and shared behaviour to add it.
Description
This PR adds middleware support to
miragejs
. API is still experimental (and quite ugly). It doesn't even support "all the middleware features" that stuff likeexpress
offers. But here it goes:I agree that this is far from the perfect API, it should be something like the
app.use
fromexpress
and I'll try to go in that direction.Regarding the middleware code itself, the API is the following (I also don't like that the API changes - the return value - for
pre
andpost
middlewares)Questions/Problems
Currently, it's being hard to have a coherent API between
post
andpre
mw APIs due to the handler's signature. It would be cool to migrate to something more similar toexpressjs
(req, res, next
). And that would also make middlewares API simpler.At the moment the fact that we also don't have methods like
res.send
, as there is onexpress
, it is not possible to have apre
middleware directly answering the request. The best is can do for now is to enrich/mutate it.Notes
This PR is obviously a draft, the APIs still have a lot to improve (as said before) but I'd like to have you guys opinion on what makes sense and what not, did this draft so we have something to discuss on top of.
I get that this might not even be possible/worth doing right now, due to the problems listed before and the priorities. Just had this idea in mind and wanted to put it out, who knows if something interesting comes out of this.
Now that I think of this, this would probably make more sense as an event emitter/subscriber than as a middleware 馃
Would love to hear your opinion