feat(trpc-server): add trpcFetchHandler preserving router context inference#1848
Open
hkirste wants to merge 3 commits intohonojs:mainfrom
Open
feat(trpc-server): add trpcFetchHandler preserving router context inference#1848hkirste wants to merge 3 commits intohonojs:mainfrom
hkirste wants to merge 3 commits intohonojs:mainfrom
Conversation
…erence Add trpcFetchHandler, a wrapper around fetchRequestHandler that keeps the router's context type through to createContext. createContext is required for routers with a typed context and optional otherwise, via the same CreateContextCallback conditional type fetchRequestHandler itself uses. trpcServer is unchanged: same runtime behaviour (auto-env merge, endpoint auto-detect from routePath) and the same callable shape. The body-method Proxy workaround for Hono's bodyCache is factored into a shared resolveRequest helper used by both exports. Type contracts for the new export live in src/trpc-fetch-handler.test-d.ts and are picked up by vitest's typecheck mode, matching the convention used in packages/zod-openapi.
🦋 Changeset detectedLatest commit: 7f04b90 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1848 +/- ##
==========================================
+ Coverage 91.72% 91.92% +0.19%
==========================================
Files 113 113
Lines 3805 3812 +7
Branches 964 963 -1
==========================================
+ Hits 3490 3504 +14
+ Misses 283 277 -6
+ Partials 32 31 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
The POST request exercises the body-method Proxy branch in resolveRequest, which is only reached for non-GET/HEAD methods.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
trpcServerpins its options toAnyRouterand itscreateContextreturn type toRecord<string, unknown>, so the router's typed context is erased at the boundary. Minimal repro:At runtime
ctx.userIdinmeisundefined. TS has no way to flag it becausecreateContext's return type isRecord<string, unknown>, and the handler itself doesn't carryTRouteras a generic.This PR adds a second export,
trpcFetchHandler, a thin wrapper aroundfetchRequestHandlerthat keeps the router's context type all the way through tocreateContext. Required/optional semantics are inherited from tRPC's ownCreateContextCallback<inferRouterContext<TRouter>, ...>, socreateContextis required when the router uses a typed context and optional otherwise — same rule asfetchRequestHandler.A few things to flag:
trpcServeris unchanged. Same runtime behaviour (auto-merge ofc.env, endpoint auto-detect fromroutePath), same callable shape, no@deprecated. Whether to eventually deprecate it is a maintainer call I don't want to pre-empt here.trpcFetchHandlerdeliberately does not auto-mergec.env. Callers who wantc.envin their tRPC context pass it through their owncreateContext, and that is exactly what lets the router's context type flow through. Auto-mergingenvis the reasontrpcServer's return type had to beRecord<string, unknown>in the first place.Proxyworkaround forHonoRequest#bodyCache(needed when an upstream middleware has already consumedc.req.json()and the raw stream is locked) is factored into a sharedresolveRequesthelper and used by both exports. No behavioural change fortrpcServer.src/trpc-fetch-handler.test-d.tsusingexpectTypeOf, picked up by vitest's typecheck mode — same convention aspackages/zod-openapi. I also added one runtime smoke test insrc/trpc-fetch-handler.test.tsthat threads a typed context through to a procedure.trpcServer, so no existing caller should need to do anything.The author should do the following, if applicable
yarn changesetat the top of this repo and push the changeset