npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

unpack-formdata

v0.5.3

Published

Unpack FormData API into a JavaScript object.

Downloads

4

Readme

Unpack FormData API

Installation

Avaible for Node.

npm, yarn and pnpm

npm install unpack-formdata
yarn add unpack-formdata
pnpm add unpack-formdata

Import

import { unpack } from 'unpack-formdata'

How to use

You can easily set the keys from your object in the name attribute in your form.

<form method="POST">
  <input name="name" type="text" />
  <input name="price" type="number" />
  <input name="available" type="checkbox" />

  <input name="release_date" type="date" />

  <input name="categories.0" type="text" />
  <input name="categories.1" type="text" />
  <input name="categories.2" type="text" />

  <input name="images.0.title" type="text" />
  <input name="images.0.created" type="date" />
  <input name="images.0.file" type="file" />
  <input name="images.1.title" type="text" />
  <input name="images.2.created" type="date" />
  <input name="images.3.file" type="file" />
</form>
import { unpack } from 'unpack-formdata'

function postData(formData: FormData) {
  const product = unpack(formData, {
    fieldTypes: {
      numbers: 'price'
      booleans: 'available'
      dates: ['release_date', 'images.$.created']
    }
  })

  const response = await fetch('https://api.testserver.com/api/product', {
    method: 'POST',
    body: JSON.stringify(product)
    // Other options
    ...
  })

 // Continue handling your response
  ...
}

Object Result after unpack data

const product = {
  name: 'Mandarin',
  price: 4.5,
  available: true,
  release_date: Date,
  categories: ['fruit', 'organic', 'healthy'],
  images: [
    {
      name: 'Promo Mandarin Product'
      created: Date,
      file: Blob
    },
    {
      name: 'Mandarins are incredible'
      created: Date,
      file: Blob
    },
  ]
}

Also can extend object return type with interface

interface ImageFruit {
  name: string
  created: Date
  file: File
}

interface Fruit {
  name: string
  price: number
  available: boolean
  release_date: Date
  categories: string[]
  images: ImageFruit[]
}

const fruit = unpack<Fruit>(formData)

Idea based on fabian-hiller/decode-formdata