Conversation
Mean you don't need manually check prefix, oRPC do that for you
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
WalkthroughThis pull request refactors the request handling logic across various integration documents and server/client packages. The changes eliminate the creation of temporary URL objects and redundant pathname checks in favor of directly invoking the Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Server
participant Handler
Client->>Server: Send HTTP request
Server->>Handler: Call handler.handle(request)
Handler-->>Server: Return { matched, response }
alt matched is true
Server-->>Client: Return response
else matched is false
Server-->>Client: Return 404 Not Found
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/contract
@orpc/openapi
@orpc/openapi-client
@orpc/react-query
@orpc/server
@orpc/solid-query
@orpc/shared
@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/server/src/adapters/standard/handler.ts (1)
81-91: Improved request prefix handling with early return pattern.The new implementation correctly checks if a prefix exists and whether the URL pathname starts with it before proceeding, returning early if there's no match. This aligns with the PR objective of automating prefix checking within handlers, making the code more robust and reducing the burden on developers.
The pathname construction is now more explicit, either replacing the prefix in the URL pathname or using the pathname as is, which improves clarity.
Consider adding a brief comment explaining the purpose of this prefix check and the expected behavior when the prefix doesn't match. Also, the regex pattern for removing leading and trailing slashes could be extracted to a named constant for better readability.
+ // Check if the request URL starts with the specified prefix if (prefix && !url.pathname.startsWith(prefix)) { return { matched: false, response: undefined } } + // Construct the pathname by removing the prefix if it exists const pathname = prefix ? url.pathname.replace(prefix, '') : url.pathname + // Format the pathname by removing leading and trailing slashes + const PATH_NORMALIZER = /^\/|\/$/g const match = await this.matcher.match(method, `/${pathname.replace(/^\/|\/$/g, '')}`)
📜 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) (1)
path(41-53)
🔇 Additional comments (18)
packages/client/src/adapters/standard/rpc-link-codec.ts (2)
5-5: Import statement updated to remove trim function.The
trimfunction has been removed from the import statement as it's no longer needed after the URL handling refactoring.
82-82: URL trimming approach improved using regex.The code now uses a regular expression to remove trailing slashes instead of the
trimutility function. The regexreplace(/\/$/, '')efficiently removes only a trailing slash if present.apps/content/docs/integrations/fetch-server.md (2)
23-26: Handler now handles prefix checking internally.The handler.handle method now accepts a
prefixparameter which handles the URL prefix checking internally, eliminating the need for manual URL parsing and prefix validation.
28-30: Simplified response handling using matched flag.The code now uses the
matchedboolean returned by the handler to determine if the request matches the RPC endpoint, simplifying the control flow.packages/shared/src/index.ts (1)
11-11: Removed trim function from exports.The
trimfunction has been removed from the exports since it's no longer used after the URL handling refactoring in the client adapter. This helps keep the API surface clean.apps/content/docs/integrations/node.md (3)
28-31: HTTP server now uses handler's prefix checking.Similar to other integrations, the Node HTTP server now relies on the handler's internal prefix checking mechanism rather than manually checking the URL.
33-35: Simplified response flow using matched flag.The code now uses the
matchedboolean to determine if the request was handled, improving readability and consistency with other integrations.
65-72: HTTP2 server implementation updated consistently.The HTTP2 server implementation has been updated with the same pattern as the HTTP server, ensuring consistency across different server types. The prefix checking is now handled by the handler internally.
apps/content/docs/integrations/cloudflare-workers.md (1)
24-31: Implementation looks good and follows the PR objective.The code has been refactored to simplify the request handling logic by leveraging the handler's built-in prefix checking. This eliminates the need for manual URL parsing and pathname validation, resulting in cleaner, more maintainable code.
apps/content/docs/integrations/deno.md (1)
23-29: Implementation correctly handles request routing.The code has been simplified to use the handler's automatic prefix checking capability. This approach is consistent with the changes in other integration files and aligns with the PR objective of moving prefix validation into handlers.
packages/server/src/adapters/standard/handler.test.ts (1)
355-371: Good test addition for prefix validation.This test case verifies a critical aspect of the new feature - ensuring that the handler properly checks the URL prefix before attempting to match a route. The test correctly validates that when an invalid prefix is provided, the handler returns
{ matched: false, response: undefined }.apps/content/docs/openapi/openapi-handler.md (1)
80-86: Implementation is consistent with other handler changes.The OpenAPI handler code has been refactored to use the same pattern as other integration examples, leveraging the handler's built-in prefix checking. This ensures a consistent approach across all handlers and documentation.
apps/content/docs/integrations/bun.md (1)
24-27: LGTM! Code refactoring improves handler usageThe integration now directly uses the handler's built-in prefix matching capability instead of manually checking URL paths. This simplifies the code and aligns with the PR objective of automating prefix checks in handlers.
Also applies to: 29-31
apps/content/docs/rpc-handler.md (1)
58-61: LGTM! Simplified request handling logicThe refactoring properly leverages the handler's built-in prefix matching capability, eliminating the need for manual URL parsing and prefix validation. This results in cleaner, more maintainable code.
Also applies to: 63-65
apps/content/docs/openapi/scalar.md (1)
33-35: LGTM! Streamlined OpenAPIHandler integrationThe refactoring simplifies the request handling flow by utilizing the handler's built-in prefix matching capability, improving code clarity and maintainability.
Also applies to: 37-39
playgrounds/contract-first/src/main.ts (2)
41-44: LGTM! Improved API request handlingThe refactoring properly utilizes the OpenAPIHandler's built-in prefix matching capability, resulting in cleaner and more maintainable code.
Also applies to: 46-48
50-53: LGTM! Improved RPC request handlingThe refactoring properly utilizes the RPCHandler's built-in prefix matching capability, resulting in cleaner and more maintainable code. This approach consistently applies the same pattern used for the OpenAPIHandler above.
Also applies to: 55-57
packages/server/src/adapters/standard/handler.ts (1)
10-10: Import statement updated to remove unused 'trim' function.The import statement now only includes the
interceptfunction from@orpc/shared, removing the previously importedtrimfunction which is no longer used after the refactoring.
|
Conflict |
Mean you don't need manually check prefix, oRPC do that for you
Before:
After:
Summary by CodeRabbit
Refactor
Tests