zlFetch now supports FormData!

Zell Liew šŸ¤— - Nov 7 '23 - - Dev Community

"Now" is grossly inaccurate because zlFetch has supported Form Data since v5.0 (since April) and weā€™re already at v6.0 šŸ™ƒ.

Nevertheless, let me share with you what this is all about.

How zlFetch supports Form Data

You can now pass Form Data content into zlFetch and it will correctly send a multipart/form-data encoding to the server.

form.addEventListener('submit', async event => {
  const data = new FormData(form)

  const response = await zlFetch('some-url', {
    body: data,
  })
})
Enter fullscreen mode Exit fullscreen mode

This is similar to the rest of our API where zlFetch automatically helps you set the Content-Type header:

  • If you pass in an object, it converts the data into JSON and sends an application/json content type.
  • If you pass in a string, it uses the application/x-www-form-urlencoded content type.

Problems with Form Data

Iā€™m not a fan of using Form Data because of two reasons:

  1. The backend needs to support it
  2. You canā€™t tell whatā€™s inside a Form Data easily

The backend needs to support it

Form Data is more complicated than json or x-www-form-urlencoded data because it can be sent in multiple parts.

In express, you can support Form Data by adding the multer package.

import multer from 'multer'
const upload = multer()
app.use(upload.array())
Enter fullscreen mode Exit fullscreen mode

Most of the time, I prefer to send JSON since there's no need to use multer and mess with multipart/form-data.

Itā€™s harder to debug Form Data

You canā€™t see whatā€™s inside a Form Data easily. If you want to see the contents, you have to loop through the form data and log each entry.

const data = new FormData(form)
for (const key, value of data) {
    console.log(key, value)
}
Enter fullscreen mode Exit fullscreen mode

This is way too complex for my liking. I prefer to use an Object and see everything at once.

Converting Form Data into an object

Because I like working with objects more than Form Data, I included a utility in zlFetch that lets you convert Form Data into an object.

With this, you no longer need to extract each form element individually. Just use the utility and itā€™ll handle the data for you.

import zlFetch, { toObject } from 'zl-fetch'

form.addEventListener('submit', async event => {
  const formData = new FormData(form)
  const data = toObject(formData)

  console.log(data)
})
Enter fullscreen mode Exit fullscreen mode

You can then send this data as JSON over to the server like this:

import zlFetch, { toObject } from 'zl-fetch'

form.addEventListener('submit', async event => {
  const formData = new FormData(form)
  const data = toObject(formData)

  const response = await zlFetch('some-url', {
    body: data,
  })
})
Enter fullscreen mode Exit fullscreen mode

I also added this utility into Splendid UI šŸ™ƒ. So you can tell that Iā€™m putting lots of helpful things into Splendid UI as I go along.

Itā€™s gonna be one of the best component libraries around. Iā€™ll share more about Splendid in another post.

Speaking about utilities, I've created another utility to it easy to send form-urlencoded data.

Utility for form-urlencoded data

zlFetch contains a toQueryString utility that can convert an object into a query string.

This makes it easy to send x-www-form-urlencoded data.

import { toQueryString } from 'zl-fetch'

zlFetch.post('some-url', {
  body: toQueryString({ message: 'Good game' }),
})
Enter fullscreen mode Exit fullscreen mode

Behind the scenes, this is a simple utility since we're just passing the object through URLSearchParams šŸ™ƒ. But hey, it's good to have this utility since it makes things a little bit simpler than before!

export function toQueryString(object) {
  const searchParams = new URLSearchParams(object)
  return searchParams.toString()
}
Enter fullscreen mode Exit fullscreen mode

That's it for today!

Further reading

By the way, this article is originally written on my blog. Feel free to visit that if you want to have these articles delivered to your email first-hand whenever they're released! šŸ™‚.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .