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

typed-formdata

v0.0.7

Published

Add types to your FormData!

Downloads

9

Readme

typed-formdata

npm GitHub top language GitHub Workflow Status (with branch)

Have you ever tried setting up forms for modern frameworks that support nested objects and arrays?

It´s not fun.

This package helps you with that.

Installation

# npm
npm i typed-formdata

# pnpm
pnpm i typed-formdata

# yarn
yarn add typed-formdata

Examples

Usage

First up, define your form data:

type MyForm = {
    settings: {
        mode: 'auto' | 'light' | 'dark';
        theme: 'red' | 'green' | 'blue';
    };
    favouriteFrameworks: Array<{
        name: string;
        satisfaction: number;
    }>;
    user: {
        firstname?: string;
        lastname?: string;
        image?: Blob;
    };
};

Fields

Use the fields helper to create your input names:

<script lang="ts">
    import { fields } from 'typed-formdata';

    const f = fields<MyForm>();
</script>

<form>
    <h2>Settings</h2>

    <label>
        <span>Mode</span>
        <select name="{f.settings.mode}">
            <options>auto</options>
            <options>light</options>
            <options>dark</options>
        </select>
    </label>
    <label>
        <span>Theme</span>
        <select name="{f.settings.theme}">
            <options>red</options>
            <options>green</options>
            <options>blue</options>
        </select>
    </label>


    <h2>3 favourite Frameworks</h2>

    <input type="text" name="{f.favouriteFrameworks[0].name}"/>
    <input type="number" name="{f.favouriteFrameworks[0].satisfaction}"/>

    <input type="text" name="{f.favouriteFrameworks[1].name}"/>
    <input type="number" name="{f.favouriteFrameworks[1].satisfaction}"/>

    <h2>User</h2>

    <input type="text" name="{f.user.firstname}"/>
    <input type="text" name="{f.user.lastname}"/>
</form>

fields() returns a proxy that will create a string.

Some examples:

For nested objects simply chain the keys:

fields<MyForm>().user.firstname;
> 'user.firstname'

For arrays you have to call the function:

fields<MyForm>().favouriteFrameworks();
> 'favouriteFrameworks[]'

Note: Only use primite values inside arrays if you don´t provide an index!

Bad:

fields<MyForm>().favouriteFrameworks().key;
> 'favouriteFrameworks[].key'

Good:

fields<MyForm>().favouriteFrameworks(1).key;
> 'favouriteFrameworks[1].key'

Simply pass in the index:

fields<MyForm>().favouriteFrameworks(2);
> 'favouriteFrameworks[2]'

It´s also possible to create objects in arrays

fields<MyForm>().favouriteFrameworks(2).satisfaction;
> 'favouriteFrameworks[2].satisfaction'

extractFormData

extractFormData pulls out the data from a FormData object where fields are typed as string and files are typed as Blob:

Note: Browsers send empty inputs as an empty string or file. extractFormData omits the values.

import { extractFormData } from 'typed-formdata';

export async function handlePost(request: Request) {
    const formData = await request.formData();

    const {
        data, // files and fields
        fields, // fields only
        files, // files only
    } = extractFormData<MyForm>(formData);

    // data:
    type Data = {
        settings?: {
            mode?: string;
            theme?: string;
        };
        favouriteFrameworks?: Array<{
            name?: string;
            satisfaction?: string;
        }>;
        user: {
            firstname?: string;
            lastname?: string;
            image?: Blob;
        };
    };

    // fields:
    type Fields = {
        settings?: {
            mode?: string;
            theme?: string;
        };
        favouriteFrameworks?: Array<{
            name?: string;
            satisfaction?: string;
        }>;
        user: {
            firstname?: string;
            lastname?: string;
        };
    };

    // files:
    type Files = {
        user: {
            image?: Blob;
        };
    };

    /**
     * You can validate the data using your library of choice
     *
     * https://zod.dev/
     * https://github.com/jquense/yup
     * https://github.com/ianstormtaylor/superstruct
     */
}

License

MIT