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

final-form-formdata

v0.1.0-alpha

Published

A decorator to wrap onSubmit and convert values to FormData

Downloads

3

Readme

final-form-formData

A decorator to convert submitted final-form data into formData.

Motivation

final-form stores it's state just as a normal javascript object. This would normally be serialized into JSON before being sent to the backend. However there are a few situations where other formats may be required.

  • File uploads require multi-part/formdata (Files are not JSON serializable). Whilst it is possible to handle file uploads separately, there are plenty of situations where it is preferable to submit your files and form-data as a single request.
  • If you are updating your front-end, and your backend is expecting multi-part/formdata this allows you to update front-end and back-end independently.

Installation

npm install final-form-formdata

Basic Usage

Simply wrap your onSubmit function with convertToFormData and instead of recieving the raw values from final-form, your onSubmit data will recieve a formData instance. e.g.

import {convertToFormData} from "final-form-formdata"

// for final-form
const formDataOnSubmit(onSubmit)
const form = createForm({ onSubmit: formDataOnSubmit })

//for react-final-form
const myForm = () => (
	<Form
	    onSubmit={convertToFormData(onSubmit)}
	    ...
	/>
)

There is an optional config argument explained further below. When you send the data, make sure you remember to set your content-type request header to "multipart/form-data".

Under the hood

FormData is made up of an entry list where each entry is a key/value pair consisting of only USVStrings or Blobs. All other types are converted. You can have multiple values with the same key (forming an array). Since final-form field values can be equal to anything, this leaves open the problem of how complex data-types are serialised into FormData entries.

final-form-formdata serializes the data according to the following rules:

  1. All registered fields are added as an entry, where the key is equal to the field name, and the value is the value of the field.
  2. Primative data-types are just added as is (since they will be converted into a sensible string representation of their value).
  3. Objects (including arrays) that have a registered sub-field will be ignored, since their children will make up their own entries.
  4. If a fields value is an array, then each element in the array is added with the same key. FieldArrays are dealt with in the same fashion. If an elements value is non-primative, rule 5 is applied when adding the entry.
  5. For values not covered by the rules 2-3, we defer to the handleComplexValues function.

config

config accepts one value, handleComplexValues which deals with non-primative data types.

For example:

const applyFilenames = (fieldName, value, formData, fieldNames) => {
    if (typeof value === 'object' && "filename" in value) {
    	formData.append(fieldName, value.file, value.filename)
    }
}

const decoratedOnSubmit = convertToFormData(onSubmit, applyFilenames)

const myForm = () => (
	<Form
	    onSubmit={decoratedOnSubmit}
	    ...
	/>
)

Two complex value handlers are exported with the library:

  • appendAll: all values are just added as an entry
  • onlyAcceptBlobs: Blobs (including Files) are added, but all other objects are ignored.

The default value is acceptAll, but you can of course write your own.

Testing

Note that this uses the FormData interface, some old testing frameworks may not support this. You can either mock the function, use a polyfill or use a more up to date test runner.

Contributions

Contributions are most welcome :) Please submit a PR or open an issue to discuss