Skip to content

Conversation

rubnogueira
Copy link

@rubnogueira rubnogueira commented Dec 23, 2024

Allow usage of FormData payloads and URLSearchParams. Using Headers, case-sensitive header handling is also ensured.

Fix #32, #178

Summary by CodeRabbit

  • New Features
    • Added support for multipart/form-data requests, including file uploads.
  • Bug Fixes
    • Prevented accidental JSON serialization of FormData and URLSearchParams.
    • Improved Content-Type detection and handling to respect existing headers.
    • Automatically sets application/json when appropriate for JSON bodies without an explicit content type.
  • Refactor
    • Centralized request header preparation for consistency across requests.
  • Tests
    • Added end-to-end tests for multipart/form-data uploads, validating file name and size handling.

Copy link

coderabbitai bot commented Oct 11, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

Centralizes header handling using the Headers API, adjusts content-type detection and setting, and conditionally JSON-serializes bodies only when appropriate. Adds test coverage for multipart/form-data via a new /form-data route and a client test posting FormData to verify correct file name and size handling.

Changes

Cohort / File(s) Summary
Fetch header/body handling
src/fetch/index.ts
Replaced object headers with Headers; normalized content-type access via headers.get/set; conditionally JSON.stringify only when body isn’t FormData/URLSearchParams and no explicit non-JSON content-type; set application/json when serializable and no content-type; preserved URL assembly and fetch call.
Form-data test and route
test/fetch.test.ts
Added /form-data route with parse: 'formdata' and schema { file: t.File() }; imported form helper; added client test posting FormData and asserting returned file name and size.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant C as Client Code
  participant EF as edenFetch()
  participant H as Headers
  participant F as fetch()
  participant S as Server

  C->>EF: call edenFetch(url, { body, headers })
  EF->>H: construct Headers from options
  alt Body is FormData or URLSearchParams
    EF-->>H: do not set Content-Type
    note over EF,H: Let browser set multipart/boundary
    EF->>F: fetch(url, { headers, body })
  else Body is other
    EF->>H: read content-type via headers.get()
    alt No content-type and JSON.stringify succeeds
      EF->>H: set content-type: application/json
      EF->>F: fetch(url, { headers, body: JSON.stringify(body) })
    else Explicit non-JSON content-type
      EF->>F: fetch(url, { headers, body })
    end
  end
  F->>S: HTTP request
  S-->>F: Response
  F-->>C: Response
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

I thump my paw at headers neat,
Let browsers spin their boundary sheet;
If JSON fits, we set it right,
If forms arrive, we don’t uptight.
A tiny hop, a tidy send—
Now files and bytes both comprehend.
Carrot-approve! ✅🥕

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 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 1e387cd and 02b0911.

📒 Files selected for processing (2)
  • src/fetch/index.ts (1 hunks)
  • test/fetch.test.ts (3 hunks)

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@SaltyAom SaltyAom merged commit 3c2548e into elysiajs:main Oct 11, 2025
1 of 2 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.

Allow to pass empty content-type for edenFetch and prevent

2 participants