Skip to content

Conversation

@lukaszkowalik2
Copy link
Contributor

I’ve added the toFormData function to the library. The initial idea was inspired by the object-to-formdata project. I’ve expanded on that project by adding support for additional JavaScript data types like BigInt and Symbol.

Key Additions:

  • BigInt Support: The function now converts BigInt values to strings, ensuring they can be included in FormData.
  • Symbol Handling: Symbol values will throw an error, as they cannot be serialized.

Additionally, I attempted to make the function more type-safe, but due to the limitations outlined in this TypeScript issue, it’s not yet possible to implement a fully generic type-safe version of FormData.

Request for Help:

  • Documentation: If someone could help improve the translations I’ve added for Japanese, Korean, and Simplified Chinese documentation, that would be amazing.
  • CI Testing for File: I'm having trouble getting the tests involving File to work in a CI environment. Any guidance on how to resolve this would be greatly appreciated!

Thanks in advance to anyone who can assist!

@vercel
Copy link

vercel bot commented Oct 17, 2024

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
es-toolkit Ready Ready Preview Comment Sep 20, 2025 10:05am

@lukaszkowalik2 lukaszkowalik2 changed the title feat(toFormData): Added toFormData feat(toFormData): Add toFormData Oct 18, 2024
@codecov-commenter
Copy link

codecov-commenter commented Oct 18, 2024

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.75%. Comparing base (2f40871) to head (4a59dc2).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #726   +/-   ##
=======================================
  Coverage   99.75%   99.75%           
=======================================
  Files         468      470    +2     
  Lines        4439     4492   +53     
  Branches     1309     1339   +30     
=======================================
+ Hits         4428     4481   +53     
  Misses         11       11           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@raon0211
Copy link
Collaborator

raon0211 commented Nov 2, 2024

Let me review this weekend :)

@lukaszkowalik2
Copy link
Contributor Author

Hold off on reviewing this for now. I'm going to refactor it a bit and add some configuration to it. Then I'll make another PR after you (hopefully) accept this one. After that, I'll add a function called fromFormData to convert FormData to an object.

@lukaszkowalik2
Copy link
Contributor Author

@raon0211 I’ve added the changes I wanted, so you can go ahead with the review whenever you’re ready. Thanks! 😃

@raon0211
Copy link
Collaborator

Thanks! This is a new function so I might need some time to think. Let me review this in a week.

Copilot AI review requested due to automatic review settings September 9, 2025 14:03
@manudeli manudeli requested a review from dayongkr as a code owner September 9, 2025 14:03
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

The PR introduces a new toFormData function that converts JavaScript objects to FormData instances with extensive configuration options. The implementation is inspired by the object-to-formdata library but expands support to additional data types like BigInt and Symbol handling.

  • Adds a comprehensive toFormData function with support for nested objects, arrays, files, and various JavaScript data types
  • Implements extensive configuration options for customizing FormData conversion behavior
  • Provides thorough test coverage with 476 test lines covering all functionality and edge cases

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/object/toFormData.ts Core implementation of the toFormData function with recursive object conversion and configuration options
src/object/toFormData.spec.ts Comprehensive test suite covering all data types, configurations, and edge cases
docs/reference/object/toFormData.md English documentation with detailed examples and configuration options
docs/zh_hans/reference/object/toFormData.md Simplified Chinese translation of the documentation
docs/ko/reference/object/toFormData.md Korean translation of the documentation
docs/ja/reference/object/toFormData.md Japanese translation of the documentation

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

} else if ((isFile(data) || isBlob(data)) && parentKey) {
formData.append(parentKey, data);
} else if (typeof data === 'bigint' && parentKey) {
formData.append(parentKey, (data as bigint).toString());
Copy link

Copilot AI Sep 9, 2025

Choose a reason for hiding this comment

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

The type cast (data as bigint) is unnecessary since the condition typeof data === 'bigint' already ensures the type. Remove the cast for cleaner code.

Suggested change
formData.append(parentKey, (data as bigint).toString());
formData.append(parentKey, data.toString());

Copilot uses AI. Check for mistakes.
}
} else if (parentKey) {
formData.append(parentKey, String(data));
} else {
Copy link

Copilot AI Sep 9, 2025

Choose a reason for hiding this comment

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

This error will be thrown for valid primitive types like strings and numbers when parentKey is undefined or empty. The condition should check if data is a primitive that can be converted to string before throwing the error.

Suggested change
} else {
} else {
// If data is a serializable primitive but parentKey is missing, throw a more descriptive error
if (
parentKey == null &&
(
typeof data === 'string' ||
typeof data === 'number' ||
typeof data === 'boolean' ||
typeof data === 'bigint'
)
) {
throw new TypeError('Cannot serialize value without a key (parentKey is required for primitives)');
}

Copilot uses AI. Check for mistakes.
@lukaszkowalik2
Copy link
Contributor Author

Hey @raon0211, @dayongkr! So sorry for the long silence on this, I completely forgot about this PR. 😅

I've pushed the fixes suggested by Copilot and also tossed in the new isBigInt predicate. It's ready for another look whenever you have a moment. Thanks!

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.

4 participants