feat(server): check prefix inside handlers#295
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
WalkthroughThis pull request refactors the request handling logic across multiple integrations by removing the creation of intermediate URL objects and the associated pathname checks. Instead, the code now directly calls the handler’s method and inspects the returned Changes
Sequence Diagram(s)sequenceDiagram
participant C as Client
participant S as Server (Fetch Function)
participant H as Handler
C->>S: Send request
S->>H: Call handler.handle(request)
H-->>S: Return {matched, response}
alt matched is true
S->>C: Return response
else
S->>C: Return 404 response
end
Possibly related PRs
Poem
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov ReportAll modified and coverable lines are covered by tests ✅ 📢 Thoughts on this report? Let us know! |
More templates
@orpc/arktype
@orpc/client
@orpc/openapi
@orpc/contract
@orpc/openapi-client
@orpc/react-query
@orpc/server
@orpc/shared
@orpc/solid-query
@orpc/standard-server
@orpc/standard-server-fetch
@orpc/standard-server-node
@orpc/svelte-query
@orpc/valibot
@orpc/vue-colada
@orpc/vue-query
@orpc/zod
commit: |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/client/src/adapters/standard/rpc-link-codec.ts (1)
82-82: Regex replacement fortrimworks, but consider handling multiple slashesThe regular expression replacement for the
trimfunction works well for removing a single trailing slash. However, the current regex only handles a single trailing slash, whiletrimmight have handled multiple consecutive slashes.Consider using
/\/+$/to handle multiple consecutive trailing slashes:-const url = new URL(`${baseUrl.toString().replace(/\/$/, '')}/${path.map(encodeURIComponent).join('/')}`) +const url = new URL(`${baseUrl.toString().replace(/\/+$/, '')}/${path.map(encodeURIComponent).join('/')}`)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (13)
apps/content/docs/integrations/bun.md(1 hunks)apps/content/docs/integrations/cloudflare-workers.md(1 hunks)apps/content/docs/integrations/deno.md(1 hunks)apps/content/docs/integrations/fetch-server.md(1 hunks)apps/content/docs/integrations/node.md(2 hunks)apps/content/docs/openapi/openapi-handler.md(1 hunks)apps/content/docs/openapi/scalar.md(1 hunks)apps/content/docs/rpc-handler.md(1 hunks)packages/client/src/adapters/standard/rpc-link-codec.ts(2 hunks)packages/server/src/adapters/standard/handler.test.ts(1 hunks)packages/server/src/adapters/standard/handler.ts(2 hunks)packages/shared/src/index.ts(1 hunks)playgrounds/contract-first/src/main.ts(1 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
packages/client/src/adapters/standard/rpc-link-codec.ts (1)
packages/client/src/adapters/standard/link.ts (1)
path(44-56)
🔇 Additional comments (16)
packages/server/src/adapters/standard/handler.test.ts (1)
344-360: Good test addition for prefix checking!This new test case properly verifies that the handler checks the URL prefix before attempting to match routes. It ensures the handler returns
{ matched: false, response: undefined }when the URL doesn't match the specified prefix.apps/content/docs/integrations/node.md (2)
27-35: Simplified request handling looks good!The code now directly calls
handler.handlewith the prefix specified as an option, removing the need for manual prefix checking. This aligns with the PR objective of automating prefix checking inside handlers.
65-72: Consistent implementation for HTTP/2 - LGTM!The HTTP/2 implementation follows the same pattern as the HTTP implementation, correctly passing the prefix as an option to the handler.
packages/shared/src/index.ts (1)
13-13: LGTM: Removing unusedtrimfunction exportRemoving the
trimfunction from exports makes sense since it's no longer used in the codebase, as evidenced by the changes inrpc-link-codec.ts.packages/client/src/adapters/standard/rpc-link-codec.ts (1)
5-5: LGTM: Import updated to match exportsCorrectly updated import to match the change in exported functions from
@orpc/shared.apps/content/docs/openapi/openapi-handler.md (1)
80-87: Simplified request handling with automatic prefix checking.The code now delegates the prefix checking to the handler instead of manually checking the URL path. This improves code clarity and reduces the amount of boilerplate required for each integration.
apps/content/docs/rpc-handler.md (1)
58-65: Improved handler logic with automatic prefix checking.The implementation now correctly leverages the handler's built-in prefix checking capability by passing '/rpc' as a parameter, simplifying the code flow and making the example more maintainable.
apps/content/docs/openapi/scalar.md (1)
33-39: Streamlined request handling with prefix injection.The code now leverages the handler's built-in prefix checking by passing '/api' as a parameter rather than performing manual URL pathname checks. This creates a more consistent pattern across the documentation.
apps/content/docs/integrations/cloudflare-workers.md (1)
24-31: Enhanced Cloudflare Workers integration with automatic prefix checking.The implementation has been streamlined by leveraging the handler's built-in prefix checking capability instead of manual URL parsing, making the integration example more concise and easier to follow.
playgrounds/contract-first/src/main.ts (2)
41-48: Improved request handling for OpenAPI endpoints.The code now directly handles the OpenAPI requests with built-in prefix checking using '/api' as the parameter, eliminating the need for manual URL parsing and making the control flow cleaner.
50-57: Enhanced RPC request handling with automatic prefix checking.Similar to the OpenAPI handler implementation, the RPC request handling now leverages the built-in prefix checking mechanism, creating a consistent pattern across both handlers.
apps/content/docs/integrations/fetch-server.md (1)
22-30: Great refactoring of request handling logic!The implementation is now cleaner and more maintainable. By having the handler automatically check for the prefix, you've reduced boilerplate code and made the control flow more explicit through the
matchedflag. This is a nice DX improvement that aligns well with the PR's objectives.apps/content/docs/integrations/bun.md (1)
23-30: Consistent implementation with other integrationsThe changes properly align with the same pattern applied to other integrations, providing a consistent developer experience. The simplified request handling with automatic prefix checking reduces code verbosity and makes the control flow clearer.
apps/content/docs/integrations/deno.md (1)
22-30: Well-implemented consistent patternThe changes maintain the same clean pattern used in other integrations, delegating prefix checking to the handler rather than manually parsing URLs. This consistency across integrations (Fetch, Bun, Deno) provides a unified developer experience and simplifies the framework's usage.
packages/server/src/adapters/standard/handler.ts (2)
9-9: Simplified importsGood cleanup by removing the unused
trimimport. This keeps the imports clean and reduces unnecessary dependencies.
88-96: Well-implemented prefix handling logicThe implementation of prefix checking is clean and efficient:
- The early return for non-matching prefixes is a good performance optimization
- The pathname construction logic is clear and explicit
- Path normalization ensures consistent handling regardless of leading/trailing slashes
This is the core change that enables the simplified integration code seen in the documentation files. The pattern of returning
{ matched, response }provides a clear API for consumers.
Mean you don't need manually check prefix, oRPC do that for you
Before:
After:
Summary by CodeRabbit