Skip to content

refactor: new StandardRPCLink, StandardOpenAPIHandler, StandardOpenAPILink#349

Merged
dinwwwh merged 2 commits intomainfrom
refactor/StandardOpenAPIHandler-StandardRPCLink
Apr 9, 2025
Merged

refactor: new StandardRPCLink, StandardOpenAPIHandler, StandardOpenAPILink#349
dinwwwh merged 2 commits intomainfrom
refactor/StandardOpenAPIHandler-StandardRPCLink

Conversation

@dinwwwh
Copy link
Copy Markdown
Member

@dinwwwh dinwwwh commented Apr 9, 2025

Summary by CodeRabbit

  • New Features
    • API endpoints now process requests dynamically, echoing input values for tailored responses.
  • Refactor
    • Improved and streamlined initialization logic for RPC and OpenAPI adapters and handlers, reducing complexity.
  • Tests
    • Expanded test suites across RPC and OpenAPI interfaces ensure consistent handling of both successful and error scenarios.
    • New test cases validate the behavior of the handlers with various input scenarios.

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 9, 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 Apr 9, 2025 8:21am

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 9, 2025

Walkthrough

The changes refactor RPC and OpenAPI link classes across multiple adapters. The RPCLink class now extends a newly introduced StandardRPCLink with a simplified constructor that removes serializer and codec initializations. Similarly, the OpenAPILink class now extends StandardOpenAPILink, and redundant initialization logic is removed. In addition, new classes for handling OpenAPI functionality (e.g., StandardOpenAPIHandler) have been introduced, and tests have been added or updated to validate dynamic input processing and error handling for both GET and POST requests.

Changes

File(s) Change Summary
packages/client/.../fetch/rpc-link.ts, packages/client/.../standard/rpc-link.ts, packages/client/.../standard/rpc-link.test.ts Refactored RPCLink to extend StandardRPCLink with a simplified constructor (removing serializer/codec instantiation) and added tests for dynamic GET/POST scenarios.
packages/openapi-client/.../fetch/openapi-link.ts, packages/openapi-client/.../standard/openapi-link.ts, packages/openapi-client/.../standard/openapi-link.test.ts Updated OpenAPILink to extend StandardOpenAPILink by removing redundant serializer/codec setups, with new tests covering GET, POST, and Blob handling.
packages/openapi/.../fetch/openapi-handler.ts, packages/openapi/.../standard/openapi-handler.ts, packages/openapi/.../fetch/openapi-handler.test.ts, packages/openapi/.../node/openapi-handler.test.ts, packages/openapi/.../standard/openapi-handler.test.ts Refactored OpenAPIHandler to delegate initialization to a new StandardOpenAPIHandler consolidating serializer and matcher logic; tests updated to check dynamic input processing and routing.
packages/server/.../rpc-handler.test.ts, packages/server/.../node/rpc-handler.test.ts Updated RPCHandler tests to validate dynamic request processing by replacing static responses with input-based outputs.

Sequence Diagram(s)

sequenceDiagram
    participant C as Client
    participant R as RPCLink
    participant SR as StandardRPCLink
    participant LC as LinkClient

    C->>R: Invoke RPC call
    R->>SR: Delegate request
    SR->>LC: Process with options
    LC-->>SR: Return result
    SR-->>R: Pass response
    R-->>C: Return result
Loading
sequenceDiagram
    participant C as Client
    participant OH as OpenAPIHandler
    participant SOH as StandardOpenAPIHandler
    participant RT as Router

    C->>OH: Send request (/ping?input=hello)
    OH->>SOH: Forward request
    SOH->>RT: Route & process request
    RT-->>SOH: Return dynamic output
    SOH-->>OH: Deliver processed response
    OH-->>C: Return final response
Loading

Possibly related PRs

  • unnoq/orpc#338: Refactoring of the RPCLink class with changes that update method signatures and class extensions.
  • unnoq/orpc#202: Modifications focused on the constructor logic and handling of RPCLinkOptions in the RPCLink implementation.
  • unnoq/orpc#239: Updates to the RPCLink class to extend StandardRPCLink and revise the serialization strategy.

Poem

I hop through fields of code at dawn,
Where links are lean and errors are gone.
RPC and OpenAPI now gracefully flow,
With tests and handlers set in a row.
Carrot dreams and bytes all gleam—
A rabbit’s cheer in this refactor dream!

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8bcac18 and 9aaa0dc.

📒 Files selected for processing (1)
  • packages/openapi/src/adapters/node/openapi-handler.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: publish-commit
🔇 Additional comments (2)
packages/openapi/src/adapters/node/openapi-handler.ts (2)

4-4: Good addition of the StandardOpenAPIHandler import

This import introduces the new standard handler that will simplify the constructor implementation. The type import for StandardOpenAPIHandlerOptions is also correctly included.


7-8: Excellent refactoring to simplify the constructor

The constructor has been significantly simplified by delegating the complex initialization logic to the StandardOpenAPIHandler. This change:

  1. Reduces code duplication
  2. Improves maintainability
  3. Follows the principle of separation of concerns

The type usage in the constructor parameter remains correct with NoInfer<StandardOpenAPIHandlerOptions<T> & NodeHttpHandlerOptions<T>> ensuring proper type checking.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 9, 2025

More templates

@orpc/arktype

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

@orpc/client

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

@orpc/contract

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

@orpc/openapi-client

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

@orpc/react

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

@orpc/react-query

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

@orpc/openapi

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

@orpc/server

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

@orpc/shared

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

@orpc/solid-query

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

@orpc/standard-server

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

@orpc/standard-server-fetch

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

@orpc/standard-server-node

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

@orpc/svelte-query

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

@orpc/valibot

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

@orpc/vue-colada

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

@orpc/vue-query

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

@orpc/zod

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

commit: 9aaa0dc

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 9, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

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 (1)
packages/client/src/adapters/standard/rpc-link.ts (1)

11-19: Good abstraction with StandardRPCLink.

The new StandardRPCLink class properly encapsulates the common RPC link initialization logic. This refactoring helps eliminate redundant code that was previously duplicated across different link implementations.

However, consider adding JSDoc documentation to explain the purpose of this class and its parameters for better developer experience.

+/**
+ * A standard implementation of an RPC link that handles serialization and encoding of RPC requests.
+ */
 export class StandardRPCLink<T extends ClientContext> extends StandardLink<T> {
+  /**
+   * Creates a new StandardRPCLink instance.
+   * @param linkClient - The client that handles the underlying network requests
+   * @param options - Configuration options for the RPC link
+   */
   constructor(linkClient: StandardLinkClient<T>, options: StandardRPCLinkOptions<T>) {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c72b962 and 8bcac18.

📒 Files selected for processing (13)
  • packages/client/src/adapters/fetch/rpc-link.ts (1 hunks)
  • packages/client/src/adapters/standard/rpc-link.test.ts (1 hunks)
  • packages/client/src/adapters/standard/rpc-link.ts (1 hunks)
  • packages/openapi-client/src/adapters/fetch/openapi-link.ts (1 hunks)
  • packages/openapi-client/src/adapters/standard/openapi-link.test.ts (1 hunks)
  • packages/openapi-client/src/adapters/standard/openapi-link.ts (1 hunks)
  • packages/openapi/src/adapters/fetch/openapi-handler.test.ts (1 hunks)
  • packages/openapi/src/adapters/fetch/openapi-handler.ts (1 hunks)
  • packages/openapi/src/adapters/node/openapi-handler.test.ts (1 hunks)
  • packages/openapi/src/adapters/standard/openapi-handler.test.ts (1 hunks)
  • packages/openapi/src/adapters/standard/openapi-handler.ts (1 hunks)
  • packages/server/src/adapters/fetch/rpc-handler.test.ts (1 hunks)
  • packages/server/src/adapters/node/rpc-handler.test.ts (1 hunks)
🔇 Additional comments (31)
packages/server/src/adapters/node/rpc-handler.test.ts (3)

8-8: Handler now processes dynamic input

The handler has been updated to use a more flexible pattern where it takes input and returns it as part of the output object, replacing the previous static response pattern.


14-14: Test updated to include dynamic test data

The test now sends an encoded JSON object with a "json":"value" pair, reflecting the shift to dynamic input processing.


16-16: Test expectation updated to match new output pattern

The expectation now checks for the dynamic value from the input rather than a static string, which properly validates the new handler behavior.

packages/openapi/src/adapters/fetch/openapi-handler.test.ts (3)

6-6: Route handling updated to support path and dynamic input

The handler now defines a specific path ('/ping') and processes input dynamically, consistent with the standardization efforts in this PR.


8-8: Test updated to provide input via query parameter

The test now sends a request with a query parameter ('input=hello') that will be processed by the handler, testing the dynamic input handling capability.


12-12: Test assertion updated to check dynamic response

The test now verifies that the response contains the input value ('hello') instead of a static string, properly validating the new dynamic behavior.

packages/openapi/src/adapters/standard/openapi-handler.test.ts (3)

1-8: New test suite for StandardOpenAPIHandler

This test appropriately establishes the test environment for the new StandardOpenAPIHandler class, using a similar route definition pattern as seen in the other refactored tests.


9-20: Test case for GET request handling

The test correctly validates the handler's ability to process GET requests with query parameters, providing a thorough test of the request handling flow including URL parsing and context management.


21-23: Assertion validates structured response format

The test properly verifies that the handler returns a structured response object containing the input value, demonstrating the standardized response format.

packages/server/src/adapters/fetch/rpc-handler.test.ts (3)

6-6: Handler updated to support dynamic input processing

The handler implementation now processes input and returns it as part of the output object, consistent with the pattern used in other adapter tests.


10-10: Test updated with encoded JSON input

The test now sends an encoded JSON object with test data, providing a proper test case for the dynamic input handling capability.


14-14: Test assertion updated to verify dynamic response

The expectation now correctly checks for the presence of the dynamic input value in the response, validating the end-to-end functionality of the handler.

packages/openapi/src/adapters/node/openapi-handler.test.ts (3)

8-8: Good update to handle dynamic input

The handler now processes input from the request and returns it in the response, which is more realistic and better tests the actual behavior of the handler in production use.


11-12: Good test for prefix functionality

Testing the handler with a prefix option ensures that path prefixing works correctly in production scenarios where API endpoints might be mounted under a specific path.


14-14: Proper assertion for dynamic response

The expectation now correctly validates that the input value is present in the response, aligning with the handler implementation that returns the input.

packages/client/src/adapters/fetch/rpc-link.ts (2)

4-4: Good refactoring to extend StandardRPCLink

Extending the new StandardRPCLink class instead of StandardLink creates a more specific inheritance hierarchy and better separates concerns. This follows the principle of composition over inheritance and helps with code organization.

Also applies to: 10-10


14-14: Simplified constructor with better encapsulation

The constructor is now simpler and delegates the creation of serializers and codecs to the parent class. This improves encapsulation and reduces duplication of code across different implementations.

packages/openapi/src/adapters/fetch/openapi-handler.ts (1)

5-5: Good refactoring to use StandardOpenAPIHandler

Using the new StandardOpenAPIHandler class simplifies the implementation by delegating the creation of serializers and codecs to a specialized class. This reduces code duplication and improves maintainability by centralizing the OpenAPI handling logic.

Also applies to: 9-9

packages/client/src/adapters/standard/rpc-link.test.ts (5)

7-9: Good test setup with proper cleanup

Using beforeEach to clear mocks ensures that each test runs with a clean state, which helps prevent test interdependencies and flaky tests.


11-12: Comprehensive test structure with parameterization

Using describe.each to test multiple data types and HTTP methods is an excellent approach that ensures thorough coverage while keeping the test code DRY. This will make it easier to add new data types or methods in the future.


13-44: Well-structured success case test helper

The assertSuccessCase function is well-designed with:

  1. Clear mocking of dependencies
  2. Proper setup of the StandardRPCHandler and StandardRPCLink
  3. Appropriate expectations to verify both the output and the handler invocation

This ensures thorough testing of the success path.


46-83: Thorough error case testing

The assertErrorCase function properly tests error handling by:

  1. Throwing a specific ORPCError with test data
  2. Verifying that the error is properly propagated through the link
  3. Checking that the error properties are preserved

This ensures robust error handling in the RPC link.


85-88: Comprehensive test cases for various data structures

The tests cover multiple scenarios:

  1. Flat values
  2. Nested objects
  3. Complex objects with special characters and collections (lists, maps, sets)

This thorough testing approach ensures that the RPC link correctly handles a wide range of data types and structures, which is crucial for a robust RPC implementation.

Also applies to: 90-93, 95-131

packages/openapi-client/src/adapters/fetch/openapi-link.ts (1)

4-4: Nice refactoring to use StandardOpenAPILink!

The refactoring simplifies this class by extending StandardOpenAPILink instead of implementing the serialization logic directly. This reduces code duplication and centralizes the OpenAPI link implementation.

Also applies to: 9-9, 13-13

packages/openapi-client/src/adapters/standard/openapi-link.ts (1)

2-8: Good implementation of StandardOpenAPILink

This new standard class is well-structured and extracts the common OpenAPI link logic from adapter-specific implementations. It handles the creation of serializers and the codec, which was previously duplicated across different adapters.

Also applies to: 12-20

packages/openapi/src/adapters/standard/openapi-handler.ts (1)

1-20: Good implementation of StandardOpenAPIHandler

This follows the same refactoring pattern as the link classes. Creating a standard handler implementation reduces duplication and provides a consistent pattern across the codebase.

packages/openapi-client/src/adapters/standard/openapi-link.test.ts (4)

6-35: Good test setup with mock router and handler

The test setup is clear and well-structured. It creates a test router with GET and POST handlers, and correctly configures the StandardOpenAPILink with a mock call implementation.


36-54: Good test coverage for GET method

This test effectively validates that the GET method correctly processes input parameters, including nested objects and date values.


56-101: Comprehensive test coverage for POST method with different input types

These tests thoroughly validate the POST method with various input scenarios, including regular JSON data, Blob objects in nested structures, and Blob objects at the root level. This ensures the StandardOpenAPILink handles all these cases correctly.


24-28: Good error handling

The error handling in the mock call implementation is simple but effective, ensuring that the tests will fail clearly if no response is received.

packages/client/src/adapters/standard/rpc-link.ts (1)

2-6: Import statements are correctly updated for the new structure.

The changes appropriately import the necessary types and classes that will be used in the new StandardRPCLink class, including the StandardRPCSerializer which is a new dependency.

@dinwwwh dinwwwh merged commit 0adc01c into main Apr 9, 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