Repository navigation
[RFC] Add middleware next()-fn - #718
Conversation
|
This pull request is being automatically deployed with Vercel (learn more). www – ./www🔍 Inspect: https://vercel.com/trpc/www/AtQbSNUSywLgoWDFdpcKwntvcNDb todomvc – ./examples/next-prisma-todomvc🔍 Inspect: https://vercel.com/trpc/todomvc/HMBnCECymQsCGSSadi1ysdRG4SgD |
next()-fnnext()-fn
Co-authored-by: Peter Pistorius <[email protected]>
|
I won't have time to review the code properly in the next days, sorry. But I have a question about the purpose of the PR: Currently a middleware has 2 states: success (fn returns) and failure (fn throws). With this PR a middleware now gets a third state: whenever neither next() will be called nor an error will be thrown. I assume an unspecific error ("Missing next() call") will be thrown then? I am not sure if this improves the user experience of tRPC or just makes it more complicated. You still cannot reuse express middlewares for example, because in our middlewares you don't have access to req and res. I am sure there is some kind of benefit in this, which I am currently missing. ;) |
|
@KATT thanks for adding me as a collaborator! I do have some untested ideas about how to implement this, as well as how to ensure that each middleware calls @simonedelmann I requested this because it can enable a bunch of use cases:
With the right design, each of these could be implemented as a separate middleware function, and any combination of them would work together. But yes, these wouldn't be 1-1 with express middleware. IMO that's ok because express middleware are kinda badly designed, using callbacks not promises. Koa has some pretty good docs on why. This design seems closer to koa. |
|
Thanks @mmkal ! Totally convinced ;) |
My implementation was that middlewares conform to
💯
💯
This is not the role of the type of middleware implementation I thought and rather something I'd use error reporting for.
💯
This should not be done in middlewares as far as I can tell and there's already support for this with data transformers. A request can contain N requests to the same procedure which would all be passed through middlewares. What you are looking for here is probably some over-arching middleware support on the request handler, so responses can be intersected before getting passed through
💯 This is also why I was okay with having the existing middleware implementation not having a |
|
@mmkal (when) do you think you will have a stab at this? |
|
@KATT I've set aside some time on Wednesday. If you're ok to wait until then? If not totally your call of course! |
|
@KATT I pushed a change. I squashed into a single commit (8a028fb) so it's easy to revert if you don't like the implementation. I can also break this out into a separate branch if you'd like to do a side-by-side comparison. The key change is: middlewares now have to pass on (return) the result from their We don't really need as much bookkeeping, like try/catch wrappers or to check for calling router
.middleware(async ({ next }) => {
const first = await next()
const second = await next()
// which one should be returned??
})I also added a test using I made middleware I updated the docs that I found that used Re error reporting, a pattern I've seen used before (admittedly for a probably overcomplicated architecture) was to throw a "recoverable" error - you could imagine a middleware that could do something like: router
.middleware(async ({ ctx, path, type, next }) => {
try {
return next()
} catch (e) {
if (e instanceof RecoverableError) {
// maybe service a failed, but we can fall back on service b which is slower/less ideal in some way
myErrorLogger.log(e, {ctx, path, type})
return e.recover()
}
throw e
}
})This should now be doable, although I think it's reasonable to let users figure out for themselves since everything interesting is really happening in the |
|
@KATT just to check - are you waiting on any more feedback from me on this? We're happily on the Also re the conflicts, in case this is helpful, I use these commands sometimes to publish branch-specific canary releases from lerna without editing the actual package.json files/creating a commit. The Sample |
|
I'm happy with this PR. Why it's not merged is that I'm spending some time finding other things that requires some breaking changes here and there too and doing them as well. See the v9 milestone. Also, if you've noticed any other structural stuff that you think we should change, this is a good opportunity to do so ☺ |
# Conflicts: # examples/.wip/next-chat/package.json # examples/express-server/package.json # examples/next-prisma-starter-websockets/package.json # examples/next-prisma-starter/package.json # examples/next-prisma-todomvc/package.json # examples/standalone-server/package.json # lerna.json # packages/client/package.json # packages/next/package.json # packages/react/package.json # packages/server/package.json
Middlewares will now have to call a
next()function that you have to call.Example
@mmkal @delaneyj - you've both championed this. I really don't like my implementation of the recursive async/await spaghetti, I wrote it in like 5 minutes, but the tests pass and it seems to work. You're both added as contributors so you can update the branch directly, would be great if one of you could clean up my implementation.
Todos
Closes #564 #586 #564