feat(server): inferRPCMethodFromRouter util#607
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
WalkthroughThis change introduces a new utility function, Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant inferRPCMethodFromRouter
participant Router
Client->>inferRPCMethodFromRouter: Call with router, options, path
inferRPCMethodFromRouter->>Router: Traverse to procedure at path (unwrap lazy routers)
Router-->>inferRPCMethodFromRouter: Return procedure with routing metadata
inferRPCMethodFromRouter->>inferRPCMethodFromRouter: Extract and normalize HTTP method
inferRPCMethodFromRouter-->>Client: Return inferred HTTP method
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:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. 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/hey-api
@orpc/nest
@orpc/openapi
@orpc/openapi-client
@orpc/react
@orpc/react-query
@orpc/server
@orpc/shared
@orpc/solid-query
@orpc/standard-server
@orpc/standard-server-aws-lambda
@orpc/standard-server-fetch
@orpc/standard-server-node
@orpc/standard-server-peer
@orpc/svelte-query
@orpc/tanstack-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 (2)
packages/server/src/link-utils.ts (2)
12-12: Consider using a more descriptive parameter name.The underscore parameter name
_indicates the options parameter is intentionally unused. Consider using a more descriptive name like_optionsto make the intent clearer.- return async (_, path) => { + return async (_options, path) => {
24-24: Document the HEAD to GET conversion rationale.The conversion of HEAD to GET method should be documented to explain why this transformation is necessary in the RPC context.
- return method === 'HEAD' ? 'GET' : method + // Convert HEAD to GET as HEAD requests are not typically used in RPC contexts + return method === 'HEAD' ? 'GET' : method
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
packages/client/src/utils.ts(1 hunks)packages/server/src/adapters/standard/utils.ts(1 hunks)packages/server/src/index.ts(1 hunks)packages/server/src/link-utils.test-d.ts(1 hunks)packages/server/src/link-utils.test.ts(1 hunks)packages/server/src/link-utils.ts(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/server/src/link-utils.test-d.ts (2)
packages/client/src/adapters/fetch/rpc-link.ts (1)
RPCLink(16-22)packages/server/src/link-utils.ts (1)
inferRPCMethodFromRouter(11-26)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: lint
- GitHub Check: publish-commit
🔇 Additional comments (7)
packages/server/src/index.ts (1)
10-10: LGTM! Export addition follows established patterns.The new export for
link-utilsis correctly positioned and follows the existing export pattern in the file.packages/server/src/adapters/standard/utils.ts (1)
11-11:Details
✅ Verification successful
Verify removal of optional chaining is safe.
The optional chaining operator was removed from
options.context, assumingoptionsis always defined. While the function signature indicatesoptionsis required, please verify that all callers pass a definedoptionsobject to prevent potential runtime errors.
🏁 Script executed:
#!/bin/bash # Description: Verify all calls to resolveFriendlyStandardHandleOptions pass defined options # Expected: All function calls should have non-null/undefined options parameter echo "Searching for calls to resolveFriendlyStandardHandleOptions..." rg -A 3 -B 3 "resolveFriendlyStandardHandleOptions\(" --type tsLength of output: 5728
🏁 Script executed:
#!/bin/bash # Display the contents of utils.ts for context sed -n '1,200p' packages/server/src/adapters/standard/utils.tsLength of output: 614
🏁 Script executed:
#!/bin/bash # Locate the definition and behavior of resolveMaybeOptionalOptions rg -A5 -B5 "resolveMaybeOptionalOptions" --type tsLength of output: 23097
🏁 Script executed:
#!/bin/bash # Show Context type definition sed -n '1,200p' packages/server/src/context.ts # Show StandardHandleOptions in the standard handler sed -n '1,200p' packages/server/src/adapters/standard/handler.tsLength of output: 5699
Removal of optional chaining on
options.contextis safe.All call sites invoke
resolveFriendlyStandardHandleOptionswith a defined object—either the provided options or{}viaresolveMaybeOptionalOptions—and the function signature enforces a non-optionaloptionsparameter. No runtime undefined access is possible.packages/server/src/link-utils.test-d.ts (1)
1-11: LGTM! Well-structured TypeScript declaration test.The test properly validates the integration between
inferRPCMethodFromRouterandRPCLink, ensuring type compatibility. Using the shared test router maintains consistency across tests.packages/server/src/link-utils.test.ts (1)
1-25: LGTM! Comprehensive test coverage with excellent edge case handling.The test suite effectively covers:
- Various HTTP methods (HEAD→GET conversion, GET, POST, DELETE)
- Nested lazy-loaded routes
- Error handling for invalid paths with descriptive error messages
- Default method behavior
The test structure is clean and the assertions are specific and meaningful.
packages/client/src/utils.ts (1)
48-48:Details
✅ Verification successful
Verify that options parameter is always defined.
The removal of optional chaining (
?.) assumes that theoptionsparameter is nevernullorundefined. This could introduce runtime errors if the assumption is incorrect.Please verify that all call sites guarantee
optionsis defined, or consider reverting to optional chaining for defensive programming:
🏁 Script executed:
#!/bin/bash # Search for all calls to resolveFriendlyClientOptions to verify options is always defined ast-grep --pattern 'resolveFriendlyClientOptions($_)'Length of output: 547
🏁 Script executed:
#!/bin/bash echo "=== utils.ts: resolveFriendlyClientOptions definition ===" rg -n "function resolveFriendlyClientOptions" -A5 -B5 packages/client/src/utils.ts echo echo "=== client.ts: call site & signature around options ===" rg -n "link.call" -A5 -B5 packages/client/src/client.ts rg -n "export .* (async )?function .*options" -A3 -B3 packages/client/src/client.tsLength of output: 1350
Safe removal of optional chaining
resolveFriendlyClientOptionsalways receives a definedoptionsobject—its signature requires it, the client call site defaultsoptions = {}, and tests only ever pass objects—so accessingoptions.contextwithout?.is safe. No changes required.packages/server/src/link-utils.ts (2)
11-26: LGTM! Well-structured utility with good error handling.The function is well-implemented with proper error handling and clear logic flow. The HEAD to GET conversion is appropriate for RPC contexts.
22-22:Details
✅ Verification successful
Verify the fallbackContractConfig usage.
Ensure that the
fallbackContractConfigfunction correctly handles the case whereprocedure['~orpc'].route.methodmight be undefined or invalid.
🏁 Script executed:
#!/bin/bash # Search for fallbackContractConfig implementation to understand its behavior ast-grep --pattern 'function fallbackContractConfig($_, $_) { $$$ }'Length of output: 70
🏁 Script executed:
#!/usr/bin/env bash # Locate the definition or references of fallbackContractConfig across the repo rg -n "fallbackContractConfig" --glob "*.ts"Length of output: 4240
No changes needed for fallbackContractConfig usage
ThefallbackContractConfigimplementation inpackages/contract/src/config.tsalready handlesundefinedvalues (e.g. returns"POST"for"defaultMethod") and is covered by unit tests. Invalid inputs are constrained by theContractConfigtype at compile time, so no further runtime checks are required here.
Summary by CodeRabbit
New Features
Bug Fixes
Tests
Chores