Skip to content

feat(client, contract, openapi): support 3xx response#304

Merged
dinwwwh merged 3 commits into
mainfrom
feat/client-contract-openapi/support-3xx-response
Mar 28, 2025
Merged

feat(client, contract, openapi): support 3xx response#304
dinwwwh merged 3 commits into
mainfrom
feat/client-contract-openapi/support-3xx-response

Conversation

@dinwwwh
Copy link
Copy Markdown
Member

@dinwwwh dinwwwh commented Mar 28, 2025

Summary by CodeRabbit

  • Documentation

    • Introduced a new “Redirect Response” page in the OpenAPI docs with an updated sidebar for easier navigation.
  • New Features

    • Enhanced HTTP redirect handling to offer more precise control over redirection.
    • Improved error response validations for clearer, more reliable feedback.
  • Bug Fixes

    • Updated error handling logic to provide more accurate status code validations.

@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 28, 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 Mar 28, 2025 1:24pm

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 28, 2025

Walkthrough

This pull request introduces a new documentation page and sidebar item for handling HTTP redirect responses within the OpenAPI context. It also updates several client and adapter modules to refine error handling by replacing range checks with the isORPCErrorStatus function. Additionally, changes include configuration adjustments to fetch parameters (using { redirect: 'manual' }), removal of an unused types export, and corresponding updates in test suites to validate the new behaviors.

Changes

File(s) Change Summary
apps/content/.vitepress/config.ts
apps/content/docs/openapi/advanced/redirect-response.md
Added a new sidebar entry ("Redirect Response") and a corresponding documentation file detailing HTTP redirect response handling in the OpenAPI framework.
packages/client/src/adapters/fetch/index.ts
packages/client/src/adapters/fetch/link-fetch-client.test.ts
packages/client/src/adapters/fetch/link-fetch-client.ts
packages/client/src/adapters/fetch/types.ts
Removed the re-export of types (and deleted the unused types file); updated fetch client configuration to pass { redirect: 'manual' } and updated the method signature in LinkFetchClientOptions for more explicit redirect handling.
packages/client/src/adapters/standard/rpc-link-codec.test.ts
packages/client/src/adapters/standard/rpc-link-codec.ts
Updated import statements and logic in response decoding to use isORPCErrorStatus instead of numeric range checks; added a spy in tests to verify proper status handling.
packages/client/src/error.test.ts
packages/client/src/error.ts
Introduced the new isORPCErrorStatus function in both production and test files, and modified error handling logic in the ORPCError class to use it for validating status codes.
packages/contract/src/procedure.test.ts
packages/contract/src/procedure.ts
Updated ContractProcedure validation logic to use isORPCErrorStatus for checking successStatus and error map status codes; added spy assertions in tests to validate status boundaries.
packages/openapi-client/src/adapters/standard/openapi-link-codec.test.ts
packages/openapi-client/src/adapters/standard/openapi-link-codec.ts
Refactored import style for ORPCError and added a spy on isORPCErrorStatus; modified the response decoding logic in StandardOpenapiLinkCodec to determine success based on this function.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant LinkFetchClient
    participant FetchAPI
    participant ErrorChecker
    Client->>LinkFetchClient: Initiate fetch request
    LinkFetchClient->>FetchAPI: fetch(request, {redirect: 'manual'})
    FetchAPI-->>LinkFetchClient: Return response (status code)
    LinkFetchClient->>ErrorChecker: isORPCErrorStatus(response.status)
    ErrorChecker-->>LinkFetchClient: Boolean result
    LinkFetchClient-->>Client: Return processed response/error
Loading
sequenceDiagram
    participant Caller
    participant ContractProcedure
    participant ErrorValidator
    Caller->>ContractProcedure: Create procedure(def)
    ContractProcedure->>ErrorValidator: isORPCErrorStatus(successStatus)
    ErrorValidator-->>ContractProcedure: Validation result
    alt Validation fails
      ContractProcedure-->>Caller: Throw error "[ContractProcedure] Invalid successStatus."
    else Validation passes
      ContractProcedure-->>Caller: Initialize procedure successfully
    end
Loading

Poem

In the field of code I hop with glee,
New docs and tests set my spirit free.
Redirects now handled with a manual cue,
Errors checked with care—a sight so true.
I nibble on carrots of clean, crisp code,
Leaping past bugs on my happy, winding road!
🥕 Hoppy updates from this clever rabbit abode!


📜 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 01e90c6 and 1979453.

📒 Files selected for processing (1)
  • packages/client/src/adapters/fetch/link-fetch-client.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/client/src/adapters/fetch/link-fetch-client.ts

🪧 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.

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 28, 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 Mar 28, 2025

Open in Stackblitz

More templates

@orpc/arktype

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

@orpc/client

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

@orpc/openapi

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

@orpc/openapi-client

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

@orpc/react-query

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

@orpc/contract

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

@orpc/server

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

@orpc/shared

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

@orpc/solid-query

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

@orpc/standard-server

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

@orpc/standard-server-fetch

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

@orpc/standard-server-node

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

@orpc/svelte-query

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

@orpc/valibot

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

@orpc/vue-colada

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

@orpc/vue-query

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

@orpc/zod

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

commit: 1979453

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: 1

🧹 Nitpick comments (2)
apps/content/docs/openapi/advanced/redirect-response.md (1)

1-34: Clear and concise documentation for redirect responses!

The documentation effectively explains how to implement HTTP redirects in oRPC OpenAPI with a practical example and important limitations note.

Consider enhancing this documentation with:

  1. Examples of other redirect status codes (301, 302, 303, 308) and when to use each
  2. A complete example showing both client and server-side code
  3. Code snippets demonstrating how to properly handle redirects in client code
packages/contract/src/procedure.ts (1)

33-34: Consider using optional chaining for more robustness.

The validation looks good, but consider using optional chaining to make the code more robust against null or undefined values.

-if (Object.values(def.errorMap).some(val => val && val.status && !isORPCErrorStatus(val.status))) {
+if (Object.values(def.errorMap).some(val => val?.status && !isORPCErrorStatus(val.status))) {
  throw new Error('[ContractProcedure] Invalid error status code.')
}
🧰 Tools
🪛 Biome (1.9.4)

[error] 33-33: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between bec9f4e and 01e90c6.

📒 Files selected for processing (14)
  • apps/content/.vitepress/config.ts (1 hunks)
  • apps/content/docs/openapi/advanced/redirect-response.md (1 hunks)
  • packages/client/src/adapters/fetch/index.ts (0 hunks)
  • packages/client/src/adapters/fetch/link-fetch-client.test.ts (1 hunks)
  • packages/client/src/adapters/fetch/link-fetch-client.ts (2 hunks)
  • packages/client/src/adapters/fetch/types.ts (0 hunks)
  • packages/client/src/adapters/standard/rpc-link-codec.test.ts (3 hunks)
  • packages/client/src/adapters/standard/rpc-link-codec.ts (2 hunks)
  • packages/client/src/error.test.ts (3 hunks)
  • packages/client/src/error.ts (2 hunks)
  • packages/contract/src/procedure.test.ts (1 hunks)
  • packages/contract/src/procedure.ts (2 hunks)
  • packages/openapi-client/src/adapters/standard/openapi-link-codec.test.ts (3 hunks)
  • packages/openapi-client/src/adapters/standard/openapi-link-codec.ts (2 hunks)
💤 Files with no reviewable changes (2)
  • packages/client/src/adapters/fetch/index.ts
  • packages/client/src/adapters/fetch/types.ts
🧰 Additional context used
🧬 Code Definitions (5)
packages/client/src/adapters/standard/rpc-link-codec.ts (1)
packages/client/src/error.ts (1)
  • isORPCErrorStatus (177-179)
packages/openapi-client/src/adapters/standard/openapi-link-codec.test.ts (2)
packages/client/src/error.ts (1)
  • ORPCError (100-160)
packages/openapi-client/src/adapters/standard/openapi-link-codec.ts (1)
  • StandardOpenapiLinkCodec (31-242)
packages/contract/src/procedure.ts (1)
packages/client/src/error.ts (1)
  • isORPCErrorStatus (177-179)
packages/client/src/adapters/standard/rpc-link-codec.test.ts (1)
packages/client/src/error.ts (1)
  • ORPCError (100-160)
packages/openapi-client/src/adapters/standard/openapi-link-codec.ts (1)
packages/client/src/error.ts (1)
  • isORPCErrorStatus (177-179)
🪛 GitHub Actions: CI
packages/client/src/adapters/fetch/link-fetch-client.ts

[error] 10-10: error TS2304: Cannot find name 'RequestRedirect'.

🪛 Biome (1.9.4)
packages/contract/src/procedure.ts

[error] 33-33: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

🔇 Additional comments (25)
apps/content/.vitepress/config.ts (1)

194-194: Well-structured sidebar addition!

The new sidebar item for "Redirect Response" is appropriately placed in the Advanced section and follows the established naming and linking patterns.

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

43-43: Added redirect handling to support 3xx responses

The addition of { redirect: 'manual' } configures the fetch request to not automatically follow redirects, allowing the client to handle 3xx responses manually. This change is consistent with the PR objective of supporting HTTP redirect responses.

packages/client/src/error.test.ts (3)

1-1: Updated import to include the new isORPCErrorStatus function

The import statement is properly updated to include the newly added isORPCErrorStatus function.


45-47: Updated test case to validate new error status behavior

The test case has been modified to reflect the change in how error status codes are defined, now treating 399 as a non-error status and 400 as an error status. This is consistent with the new isORPCErrorStatus function implementation.


111-118: Added test for the new isORPCErrorStatus function

The test correctly validates that:

  • Status codes 200 and 399 (2xx and 3xx) are NOT considered error statuses
  • Status codes 400, 499, and 199 (4xx, 400+, and <200) ARE considered error statuses

This ensures the function behaves as expected for HTTP redirect (3xx) responses.

packages/client/src/error.ts (2)

107-109: Updated error validation to use new isORPCErrorStatus function

The constructor now uses the isORPCErrorStatus function to validate the status code, making the validation logic more maintainable and consistent throughout the codebase.


177-179: Added isORPCErrorStatus function to standardize error status checking

This new function provides a centralized way to determine if a status code is an error in the ORPC context. The implementation correctly identifies:

  • Non-error status codes: 2xx and 3xx (≥ 200 and < 400)
  • Error status codes: < 200 and ≥ 400

This is a key enabler for properly handling 3xx redirect responses as non-error responses.

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

6-6: Updated import to include isORPCErrorStatus function

The import statement is correctly updated to include the new utility function.


120-120: Improved status code checking to support 3xx responses

The decode method now uses the isORPCErrorStatus function to determine if a response is successful, replacing the previous range check that only allowed 2xx responses. This enables the codec to properly treat 3xx responses as non-error responses, fulfilling the PR objective.

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

2-2: Import and spy setup looks good.

The import change and creation of isORPCErrorStatusSpy makes sense here to facilitate testing the error status handling functionality.

Also applies to: 7-8


127-128: Good test coverage for success status checking.

Adding assertions to verify that isORPCErrorStatus is called with the correct status code improves the test's coverage of the error handling logic.


153-154: Good test coverage for error status checking.

Similar to the success case, these assertions ensure that isORPCErrorStatus is properly called when handling error statuses.

packages/contract/src/procedure.test.ts (3)

1-1: Test setup looks good.

The import change and the spy setup for isORPCErrorStatus along with test cleanup in beforeEach follow best practices for testing.

Also applies to: 5-9


12-31: Good test coverage for successStatus validation.

The test now properly validates the behavior of the constructor with both valid and invalid status codes, ensuring that isORPCErrorStatus is called with the correct arguments.


33-60: Good test coverage for errorMap validation.

The test ensures that the constructor validates status codes in the errorMap correctly, throwing an error for success statuses (200) and accepting error statuses (500).

packages/contract/src/procedure.ts (2)

5-5: Good addition of the import.

Importing isORPCErrorStatus from @orpc/client aligns with using a standardized approach to error status validation.


29-30: Good update to validation logic.

Using isORPCErrorStatus provides better standardization of error handling across the codebase compared to hardcoded range checks.

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

29-29: Good implementation of manual redirect handling.

Setting redirect: 'manual' is appropriate for handling 3xx responses explicitly, which aligns with the PR objective.

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

4-4: Import updated to support 3xx response handling

The import statement has been updated to include the isORPCErrorStatus function as a value import rather than just importing types. This change supports the new redirect handling functionality.


189-189: Improved status code handling to support HTTP redirects

The change from explicitly checking status codes in the 200-299 range to using !isORPCErrorStatus(response.status) is a significant enhancement. This now allows 3xx responses (redirects) to be treated as successful responses rather than errors, which aligns with the PR's objective.

Looking at the implementation of isORPCErrorStatus, this function returns true only for status codes < 200 or >= 400, meaning 3xx codes are no longer considered errors.

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

1-1: Updated module import approach

Changed from individual imports to a namespace import for better organization and to support spying on module functions.


9-10: Added test utility setup for error status checking

Extracting the ORPCError and creating a spy on isORPCErrorStatus enables proper testing of the new 3xx response handling functionality.


288-289: Added test verification for successful 201 response

These assertions verify that the isORPCErrorStatus function is called with the correct status code (201) and that non-error responses are properly handled in the compact output structure case.


309-311: Added test verification for detailed output structure

Similar to the compact output test, these assertions verify that isORPCErrorStatus is correctly used to determine successful responses in the detailed output structure case.


313-348: Added comprehensive test case for error handling

This new test case thoroughly verifies:

  1. Proper handling of error responses with valid ORPC error format (status 501)
  2. Proper handling of error responses with malformed format (status 409)
  3. Correct call count and parameters for the isORPCErrorStatus function

The test enhances coverage for the error handling logic while supporting the new 3xx response handling.

Comment thread packages/client/src/adapters/fetch/link-fetch-client.ts Outdated
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