Skip to content

feat(server): inferRPCMethodFromRouter util#607

Merged
dinwwwh merged 1 commit intomainfrom
feat/server/inferRPCMethodFromRouter
Jun 6, 2025
Merged

feat(server): inferRPCMethodFromRouter util#607
dinwwwh merged 1 commit intomainfrom
feat/server/inferRPCMethodFromRouter

Conversation

@dinwwwh
Copy link
Copy Markdown
Member

@dinwwwh dinwwwh commented Jun 6, 2025

Summary by CodeRabbit

  • New Features

    • Introduced a utility to infer the appropriate HTTP method for RPC requests based on router configuration.
  • Bug Fixes

    • Improved reliability by assuming options are always defined when accessing context properties.
  • Tests

    • Added comprehensive tests to verify correct HTTP method inference and error handling for invalid routes.
  • Chores

    • Updated exports to include new link utility functions.

@vercel
Copy link
Copy Markdown

vercel bot commented Jun 6, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
orpc ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 6, 2025 8:37am

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Jun 6, 2025

Walkthrough

This change introduces a new utility function, inferRPCMethodFromRouter, to determine the HTTP method for RPC requests based on router configuration. It adds comprehensive tests for this function and updates exports. Additionally, optional chaining is removed from context access in utility functions, assuming options are always defined.

Changes

File(s) Change Summary
packages/client/src/utils.ts
packages/server/src/adapters/standard/utils.ts
Removed optional chaining when accessing options.context in utility functions.
packages/server/src/index.ts Added export for all entities from link-utils.
packages/server/src/link-utils.ts Introduced inferRPCMethodFromRouter to infer HTTP methods from router configuration.
packages/server/src/link-utils.test.ts
packages/server/src/link-utils.test-d.ts
Added unit and type tests for inferRPCMethodFromRouter, covering various route and error scenarios.

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
Loading

Possibly related PRs

  • unnoq/orpc#219: Adds a utility to recursively unwrap lazy routers, similar to the unwrapping logic used in the new inferRPCMethodFromRouter.
  • unnoq/orpc#578: Introduces inferRPCMethodFromContractRouter for inferring HTTP methods from contract routers, related to the new function for standard routers.

Poem

A router’s path, a method to find,
With lazy routes no longer blind.
Now inference is crystal clear,
HTTP verbs appear right here!
Exports expand, tests abound—
Hopping forward, code is sound.
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need 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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@codecov
Copy link
Copy Markdown

codecov bot commented Jun 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Jun 6, 2025

More templates

@orpc/arktype

npm i https://pkg.pr.new/@orpc/arktype@607

@orpc/client

npm i https://pkg.pr.new/@orpc/client@607

@orpc/contract

npm i https://pkg.pr.new/@orpc/contract@607

@orpc/hey-api

npm i https://pkg.pr.new/@orpc/hey-api@607

@orpc/nest

npm i https://pkg.pr.new/@orpc/nest@607

@orpc/openapi

npm i https://pkg.pr.new/@orpc/openapi@607

@orpc/openapi-client

npm i https://pkg.pr.new/@orpc/openapi-client@607

@orpc/react

npm i https://pkg.pr.new/@orpc/react@607

@orpc/react-query

npm i https://pkg.pr.new/@orpc/react-query@607

@orpc/server

npm i https://pkg.pr.new/@orpc/server@607

@orpc/shared

npm i https://pkg.pr.new/@orpc/shared@607

@orpc/solid-query

npm i https://pkg.pr.new/@orpc/solid-query@607

@orpc/standard-server

npm i https://pkg.pr.new/@orpc/standard-server@607

@orpc/standard-server-aws-lambda

npm i https://pkg.pr.new/@orpc/standard-server-aws-lambda@607

@orpc/standard-server-fetch

npm i https://pkg.pr.new/@orpc/standard-server-fetch@607

@orpc/standard-server-node

npm i https://pkg.pr.new/@orpc/standard-server-node@607

@orpc/standard-server-peer

npm i https://pkg.pr.new/@orpc/standard-server-peer@607

@orpc/svelte-query

npm i https://pkg.pr.new/@orpc/svelte-query@607

@orpc/tanstack-query

npm i https://pkg.pr.new/@orpc/tanstack-query@607

@orpc/valibot

npm i https://pkg.pr.new/@orpc/valibot@607

@orpc/vue-colada

npm i https://pkg.pr.new/@orpc/vue-colada@607

@orpc/vue-query

npm i https://pkg.pr.new/@orpc/vue-query@607

@orpc/zod

npm i https://pkg.pr.new/@orpc/zod@607

commit: 3a8e066

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 _options to 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

📥 Commits

Reviewing files that changed from the base of the PR and between cd10cfb and 3a8e066.

📒 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-utils is 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, assuming options is always defined. While the function signature indicates options is required, please verify that all callers pass a defined options object 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 ts

Length 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.ts

Length of output: 614


🏁 Script executed:

#!/bin/bash
# Locate the definition and behavior of resolveMaybeOptionalOptions
rg -A5 -B5 "resolveMaybeOptionalOptions" --type ts

Length 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.ts

Length of output: 5699


Removal of optional chaining on options.context is safe.

All call sites invoke resolveFriendlyStandardHandleOptions with a defined object—either the provided options or {} via resolveMaybeOptionalOptions—and the function signature enforces a non-optional options parameter. 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 inferRPCMethodFromRouter and RPCLink, 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 the options parameter is never null or undefined. This could introduce runtime errors if the assumption is incorrect.

Please verify that all call sites guarantee options is 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.ts

Length of output: 1350


Safe removal of optional chaining
resolveFriendlyClientOptions always receives a defined options object—its signature requires it, the client call site defaults options = {} , and tests only ever pass objects—so accessing options.context without ?. 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 fallbackContractConfig function correctly handles the case where procedure['~orpc'].route.method might 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
The fallbackContractConfig implementation in packages/contract/src/config.ts already handles undefined values (e.g. returns "POST" for "defaultMethod") and is covered by unit tests. Invalid inputs are constrained by the ContractConfig type at compile time, so no further runtime checks are required here.

@dinwwwh dinwwwh merged commit 7dcd9e1 into main Jun 6, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant