https://github.com/srph/tofd2
Converts an object to a FormData representation. Also supports nested arrays, objects, File, and Buffers.
https://github.com/srph/tofd2
Last synced: about 1 month ago
JSON representation
Converts an object to a FormData representation. Also supports nested arrays, objects, File, and Buffers.
- Host: GitHub
- URL: https://github.com/srph/tofd2
- Owner: srph
- Created: 2018-07-18T11:11:57.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-07-19T13:35:25.000Z (almost 8 years ago)
- Last Synced: 2025-10-06T07:29:43.623Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 40 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# tofd2 [](https://npmjs.com/packages/@srph/tofd2) [](https://travis-ci.org/srph/tofd2?branch=master)
Converts an object to a `FormData` representation. Also supports nested arrays, objects, `File`, and `Buffer`s.
Fork of [tugorez/tofd](https://github.com/tugorez/tofd/pull/1).
## Installation
```bash
npm i @srph/tofd2 --save
```
## Why
This library allows you to declaratively create a `FormData` instead of having to `append` each property.
```diff
- const payload = new FormData()
- payload.append('first_name', firstName)
- payload.append('last_name', lastName)
+ const payload = tofd({
+ first_name: firstName,
+ last_name: lastName
+ })
fetch('/user', { method: 'POST', body: payload })
```
## Usage
```js
const payload = tofd({
// Basic data types
first_name: 'hello',
last_name: 'hello',
// Nested arrays and objects
tags: [{ id: 1 } , { id: 2 }],
// Files and Buffers
attachments: [File, File],
avatar: File
})
```
## API
```
tofd(obj: Object): FormData
```
Accepts an object that gets converted into `FormData`.
## Contributing
### Formatting
Run [Prettier](https://github.com/prettier/prettier) on the codebase.
```bash
yarn fmt
```
### Running tests
```bash
yarn test
```