Skip to content

fix(client): FormData not working with transformers - #6495

Merged
KATT merged 1 commit into
nextfrom
02-13-formdata-bug
Feb 13, 2025
Merged

KATT merged 1 commit into
nextfrom
02-13-formdata-bug

Conversation

@KATT

@KATT KATT commented Feb 13, 2025 •

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • New Features
    • Enhanced request handling improves the validation and processing of various input types, including form data and binary content, providing clearer error feedback on unsupported submissions.
    • Integrated a data transformation mechanism that ensures consistent results between form-based inputs and JSON payloads, offering a more reliable experience when submitting data.

@KATT
KATT requested a review from a team as a code owner February 13, 2025 15:15
@coderabbitai

coderabbitai Bot commented Feb 13, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

This pull request refactors the logic in the universal requester and enhances the test suite. In the client code, the function now explicitly checks for the presence of an input property and separates the handling of FormData and Octet type inputs using dedicated conditionals with strict checks for mutation types or POST method overrides. In the test suite, a new scenario integrates a transformer via superjson within the TRPC context and client configuration, ensuring that both FormData and JSON inputs provide consistent results.

Changes

File Path Summary
packages/client/src/links/httpLink.ts Refactored universalRequester: now checks for an input property; separates FormData and Octet input handling into dedicated conditionals with error checks; moves relevant httpRequest calls inside conditionals; falls back to jsonHttpRequester when no special input type is detected.
packages/tests/server/react/formData.test.tsx Added a new describe('with transformer', ...) block; updated TRPC context creation to include a transformer via superjson; modified client configuration to pass the transformer to httpLink; introduced a regression test verifying consistent handling of FormData and JSON inputs for the polymorphic mutation.

Sequence Diagram(s)

sequenceDiagram
    participant C as Caller
    participant U as universalRequester
    participant H as httpRequest
    participant J as jsonHttpRequester

    C->>U: Call universalRequester(opts)
    U-->>U: Check if opts.input exists
    alt Input is FormData or Octet with POST/mutation
        U-)H: Execute httpRequest with input checks
    else
        U-)J: Execute jsonHttpRequester
    end
Loading
sequenceDiagram
    participant T as Test Suite
    participant TR as TRPC Context (with transformer)
    participant P as Polymorphic Procedure
    participant C as Client (httpLink with transformer)
    
    T->>TR: Initialize TRPC context with transformer
    TR->>P: Define polymorphic mutation procedure
    T->>C: Configure client with transformer
    C->>P: Call polymorphic mutation
    P-->>C: Return consistent result (FormData/JSON)
Loading

Poem

Hopping through code fields under the moon so bright,
I found clearer checks to set my logic right.
With FormData and transformers in a playful tune,
My rabbit heart leaps like a bug in bloom.
Cheers from my paws—happy coding, day or night! 🐇

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • 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. (Beta)
  • @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.

@vercel

vercel Bot commented Feb 13, 2025 •

Copy link
Copy Markdown

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

Name Status Preview Comments Updated (UTC)
next-prisma-starter ✅ Ready (Inspect) Visit Preview Feb 13, 2025 3:17pm
og-image ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 13, 2025 3:17pm
www ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 13, 2025 3:17pm

@KATT
KATT enabled auto-merge (squash) February 13, 2025 15:15
@railway-app

railway-app Bot commented Feb 13, 2025 •

Copy link
Copy Markdown

🚅 Previously deployed to Railway in the trpc-sse-and-websockets project. Environment has been deleted.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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/tests/server/react/formData.test.tsx (1)

261-270: Consider expanding transformer regression test coverage.

While the current test verifies basic FormData functionality with transformers, consider adding tests for:

  1. Complex data types that benefit from superjson transformation
  2. Edge cases with null/undefined values
  3. Nested object structures
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 3aa4966 and 93fd6ba.

📒 Files selected for processing (2)
  • packages/client/src/links/httpLink.ts (1 hunks)
  • packages/tests/server/react/formData.test.tsx (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: test
  • GitHub Check: Lint and auto-fix
🔇 Additional comments (5)
packages/client/src/links/httpLink.ts (3)

39-41: LGTM! Improved input handling with explicit property check.

The explicit check for 'input' property before destructuring prevents potential undefined errors.


41-53: LGTM! Enhanced FormData validation and handling.

The changes improve FormData handling by:

  1. Adding strict validation for mutation type or POST method
  2. Moving the httpRequest call inside the FormData block
  3. Setting contentTypeHeader to undefined to let browser handle it correctly

55-66: LGTM! Enhanced Octet type validation and handling.

The changes improve Octet type handling by:

  1. Adding strict validation for mutation type or POST method
  2. Moving the httpRequest call inside the Octet type block
  3. Setting appropriate content type header
packages/tests/server/react/formData.test.tsx (2)

20-20: LGTM! Added superjson transformer.

Added superjson for transformer support in tests.


226-259: LGTM! Well-structured test setup for transformer integration.

The test setup:

  1. Creates a new context with transformer
  2. Configures a simplified router
  3. Sets up client with transformer in httpLink

@KATT
KATT disabled auto-merge February 13, 2025 15:18
@KATT
KATT merged commit de6335c into next Feb 13, 2025
@KATT
KATT deleted the 02-13-formdata-bug branch February 13, 2025 15:18
@pkg-pr-new

pkg-pr-new Bot commented Feb 13, 2025

Copy link
Copy Markdown

Open in Stackblitz

@trpc/client

npm i https://pkg.pr.new/trpc/trpc/@trpc/client@6495

@trpc/next

npm i https://pkg.pr.new/trpc/trpc/@trpc/next@6495

@trpc/react-query

npm i https://pkg.pr.new/trpc/trpc/@trpc/react-query@6495

@trpc/tanstack-react-query

npm i https://pkg.pr.new/trpc/trpc/@trpc/tanstack-react-query@6495

@trpc/server

npm i https://pkg.pr.new/trpc/trpc/@trpc/server@6495

commit: 93fd6ba

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Feb 14, 2025

This branch was successfully deployed

5 active deployments
Preview – www — 93fd6ba6 Deployed Feb 13, 2025 by vercel[bot]
Preview – next-prisma-starter — 93fd6ba6 Deployed Feb 13, 2025 by vercel[bot]
Preview – trpc-next-app-dir — 93fd6ba6 Deployed Feb 13, 2025 by vercel[bot]
Preview – examples-todomvc — 93fd6ba6 Deployed Feb 13, 2025 by vercel[bot]
trpc-pr-6495 — 93fd6ba6 Deployed Feb 13, 2025 by railway-app[bot]
Preview – og-image — 93fd6ba6 Deployed Feb 13, 2025 by vercel[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants