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
npm install form-data-makerform-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.
bash
npm install form-data-maker
`
Usage
To use form-data-maker, import it and then call the objectToFormData function with your desired data.
`javascript
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);
`
$3
- obj: The object you want to convert to FormData.
- form: (optional) An instance of FormData to which you want to append. If not provided, a new FormData instance will be created.
- namespace: (optional) A namespace for keys. This helps you organize the keys hierarchically.
$3
`javascript
const userData = {
username: 'user123',
password: 'password123',
profile: {
firstName: 'John',
lastName: 'Doe'
}
};
const formData = objectToFormData(userData);
`
$3
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 the FormData`.