@srph/tofd2

1.0.0 • Public • Published

tofd2 npm version Build Status

Converts an object to a FormData representation. Also supports nested arrays, objects, File, and Buffers.

Fork of tugorez/tofd.

Installation

npm i @srph/tofd2 --save

Why

This library allows you to declaratively create a FormData instead of having to append each property.

- 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

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 on the codebase.

yarn fmt

Running tests

yarn test

Readme

Keywords

none

Package Sidebar

Install

npm i @srph/tofd2

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

28.8 kB

Total Files

5

Last publish

Collaborators

  • srph