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

@willishq/vform-ts

v0.0.0

Published

VueJS Form module for TypeScript

Downloads

2

Readme

TypeScript Vue Form

Proof of concept package hacked together quickly, not recommended to use during production yet...

Premise / idea

I wanted to create an implementation of vform using TypeScript. After delving into the vform code, I discovered it used Object.assign() to add the properties from the form data directly to the class. This provided an interesting problem in TypeScript, which led me to develop this package using a slightly different approach to vform

Rather than tacking the properties on to the object, I went for a different approach. I maintain a property of the original data, and a one just of the changes, then use Object.defineProperty() to add a getter and setter for each key on the data. One thing this does not do right now is handle nested data or arrays, while I have no doubt that it will be possible, for this proof of concept I didn't feel like it was needed.

Request Handler

The next thing I did was to do away with the dependency for axios, and instead require users to set their request handler function themselves, enabling them to implement whatever http client of their choosing.

The request handler needs to follow the contract

import VForm, { RequestMethod, RequestException, ResponseData, RequestHandler } from '@willishq/ts-vform';

const form = new VForm({
    name: 'Andrew'
});

form.setRequestHandler(async function<T>(method: RequestMethod, url: string, data: Partial<T>): Promise<ResponseData<T>> {
    return {
        method, url, data
    };
});

Using with Vue

Coming soon...

Using with Nuxt.js

create a plugin for VForm

import VForm, { RequestMethod, RequestException, ResponseData, RequestHandler } from '@willishq/ts-vform';


export default ({app}, inject) => {

    const requestHandler: RequestHandler = async <T>(method: RequestMethod, url: string, data: RequestData<T>): Promise<ResponseData<T>> => {
        try {
            // makeRequest
            return {}
        } catch (e) {
            if (e.statusCode === 422) {
                const errors: ValidationErrors = {
                    statusCode: 422,
                    message: 'Validation Failed',
                    errors: {
                        // parse errors into valid structure, ie:
                        // name: ['must me set', 'must be andrew']
                    }
                };
                throw errors;
            }
        }
    }

    inject('vform', <M>(params: M) => {
        const form:VForm<M> = new VForm(params);
        form.setRequestHandler(requestHandler);
        return form;
    });

}

which can be used in your components like so

<template>
    <div>
        <p v-if="form.flashMessage()" v-text="form.flashMessage()"/>
        <input type="email" v-model="form.email">
        <div v-if="form.hasError('email')">
            <span v-for="message in form.getErrors('email')" v-text="message" />
        </div>
        <input type="password" v-model="form.password">
        <div v-if="form.hasError('password')">
            <span v-for="message in form.getErrors('password')" v-text="message" />
        </div>
        <button @click="submit">Login<button>
    </div>
</template>

<script lang="ts">
interface LoginModel {
    username: string,
    password: string
}

@Component
export default class LoginForm extends Vue {
    public form: VForm = this.$form<LoginModel>({
        email: '',
        password: ''
    });

    public async submit() {
        try {
            let response = await this.form.post('auth/login');
            // redirect user to next page
        } catch (e) {
            // silent?
        }
    }
}
</script>