form-data-maker
v1.0.1
Published
form-data-maker is a lightweight JavaScript utility for converting objects into FormData format. It simplifies the process of preparing form data for submission, making it easy to handle nested objects and file uploads. Ideal for web developers who need t
Downloads
2
Readme
form-data-maker
form-data-maker
is a simple package for converting JavaScript objects into FormData
. This tool helps you easily create form data and prepare it for sending to a server.
Installation
To install the package, use npm:
npm install form-data-maker
Usage
To use form-data-maker
, import it and then call the objectToFormData
function with your desired data.
import objectToFormData from 'form-data-maker';
// Sample data
const data = {
name: 'John Doe',
age: 30,
files: [new File(['content'], 'file.txt')]
};
// Convert to FormData
const formData = objectToFormData(data);
Parameters
obj
: The object you want to convert toFormData
.form
: (optional) An instance ofFormData
to which you want to append. If not provided, a newFormData
instance will be created.namespace
: (optional) A namespace for keys. This helps you organize the keys hierarchically.
Example
const userData = {
username: 'user123',
password: 'password123',
profile: {
firstName: 'John',
lastName: 'Doe'
}
};
const formData = objectToFormData(userData);
Output
The objectToFormData
function returns a FormData
object that includes the input data as key-value pairs.
Notes
- The function works recursively and can correctly process nested objects.
- If any input value is a
File
object, it will be properly added to theFormData
.
Contributing
If you have suggestions or want to help improve this package, we would love to hear from you!
Feel free to let me know if you need any changes or additions!