-
Notifications
You must be signed in to change notification settings - Fork 502
feat(toFormData): Add toFormData
#726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
toFormData
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ 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:
|
|
Let me review this weekend :) |
|
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. |
|
@raon0211 I’ve added the changes I wanted, so you can go ahead with the review whenever you’re ready. Thanks! 😃 |
|
Thanks! This is a new function so I might need some time to think. Let me review this in a week. |
There was a problem hiding this 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
toFormDatafunction 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.
src/object/toFormData.ts
Outdated
| } else if ((isFile(data) || isBlob(data)) && parentKey) { | ||
| formData.append(parentKey, data); | ||
| } else if (typeof data === 'bigint' && parentKey) { | ||
| formData.append(parentKey, (data as bigint).toString()); |
Copilot
AI
Sep 9, 2025
There was a problem hiding this comment.
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.
| formData.append(parentKey, (data as bigint).toString()); | |
| formData.append(parentKey, data.toString()); |
| } | ||
| } else if (parentKey) { | ||
| formData.append(parentKey, String(data)); | ||
| } else { |
Copilot
AI
Sep 9, 2025
There was a problem hiding this comment.
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.
| } 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)'); | |
| } |
I’ve added the
toFormDatafunction 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 likeBigIntandSymbol.Key Additions:
BigIntvalues to strings, ensuring they can be included inFormData.Symbolvalues 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:
File: I'm having trouble getting the tests involvingFileto work in a CI environment. Any guidance on how to resolve this would be greatly appreciated!Thanks in advance to anyone who can assist!