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

@rhangai/vue-submit-helper

v1.0.0

Published

Wrapper to handle requests

Downloads

15

Readme

@rhangai/vue-submit-helper

Simplify your submission logic.

Installation

yarn add @rhangai/vue-submit-helper

Basic Usage

Create a submit.ts composable file

import { useSubmitHelper, useSubmitMultipleHelper } from '@rhangai/vue-submit-helper';

export const useSubmit = useSubmitHelper;
export const useSubmitMultiple = useSubmitHelperMultiple;

Simple form submission

<script setup>
import axios from 'axios';
import { useSubmit } from './my/submit';

const form = reactive({
	name: '',
});

const { submit, submitting } = useSubmit(() =>
	axios({
		method: 'post',
		url: 'some url',
		data: form,
	})
);
</script>
<template>
	<form @submit.prevent="submit">
		<div>Name: <input name="name" v-model="form.name" :disabled="submitting" /></div>
		<button type="submit" :disabled="submitting">Submit</button>
	</form>
</template>

Multiple submissions

<script setup>
import axios from 'axios';
import { useSubmitMultiple } from './my/submit';

const items = [
	{
		id: 1,
		name: 'John',
	},
	{
		id: 2,
		name: 'Mary',
	},
];

const { submit, isSubmitting } = useSubmitMultiple(
	(item) =>
		axios({
			method: 'post',
			url: 'some url',
			data: { id: item.id },
		}),
	(item) => item.id
);
</script>
<template>
	<div>
		<div v-for="item in items" :key="item.id">
			<button @click="submit(item)" :disabled="isSubmitting(item)">
				Delete Item {{ item.name }}
			</button>
		</div>
	</div>
</template>

Custom Submit Logic

A more complex example using a custom submit logic and some notification, confirmation and axios

import {
	useSubmitHelper,
	useSubmitHelperMultiple,
	UseSubmitHelperOptions,
	UseSubmitHelperResult,
	UseSubmitHelperMultipleResult,
} from '@rhangai/vue-submit-helper';
import axios, { AxiosResponse, AxiosRequestConfig } from 'axios';
import { useConfirm } from 'my-confirmation-lib';
import { useNotify } from 'my-notification-lib';

/**
 * UseSubmit options
 */
export type UseSubmitOptions<TArg, TResult> = UseSubmitHelperOptions<TArg, TResult> & {
	confirmation?: (args: TArg) => string;
	notificationSuccess?: (result: TResult, arg: TArg) => string;
	notificationError?: (error: unknown, arg: TArg) => string;
	notificationValidateError?: (error: unknown, arg: TArg) => string;
};

/**
 * Implements the useSubmitAxios
 */
export function useSubmitAxios<TArg = void, TData = any>(
	config: (arg: TArg) => AxiosRequestConfig | Promise<AxiosRequestConfig>,
	options: UseSubmitOptions<TArg, AxiosResponse<TData>> = {}
): UseSubmitHelperResult<TArg, AxiosResponse<TData>> {
	const submitOptions = useDefaultAxiosSubmitOptions<TArg, TData>(options);
	const request = async (arg: TArg) => axios(await config(arg));
	return useSubmitHelper(request, submitOptions);
}

/**
 * Implements the useSubmitAxiosMultiple
 */
export function useSubmitAxiosMultiple<TArg, TData = any>(
	config: (arg: TArg) => AxiosRequestConfig | Promise<AxiosRequestConfig>,
	keyGetter: (arg: TArg) => string,
	options: UseSubmitOptions<TArg, AxiosResponse<TData>> = {}
): UseSubmitHelperMultipleResult<TArg, AxiosResponse<TData>> {
	const submitOptions = useDefaultAxiosSubmitOptions<TArg, TData>(options);
	const request = async (arg: TArg) => axios(await config(arg));
	return useSubmitHelperMultiple(request, keyGetter, submitOptions);
}

/**
 * Get the default submit options
 */
function useDefaultAxiosSubmitOptions<TArg, TData = any>(
	options: UseSubmitOptions<TArg, AxiosResponse<TData>>
): UseSubmitHelperOptions<TArg, AxiosResponse<TData>> {
	const {
		confirmation,
		notificationSuccess,
		notificationError,
		notificationValidateError,
		...submitOptions
	} = options;
	const { notify } = useNotify();
	const { confirm } = useConfirm();
	return {
		...submitOptions,
		async prepare(arg) {
			if (confirmation != null) {
				const confirmResult = await confirm(confirmation(arg));
				if (confirmResult === false) return false;
			}
			return submitOptions.prepare?.(arg);
		},
		async onSuccess(result, arg) {
			await submitOptions?.onSuccess?.(result, arg);
			notify(notificationSuccess?.(result, arg) ?? 'Success');
		},
		async onError(error, arg) {
			await submitOptions?.onError?.(error, arg);
			notify(notificationError?.(error, arg) ?? 'Error');
		},
		async onValidateError(error, arg) {
			await submitOptions?.onValidateError?.(error, arg);
			notify(notificationValidateError?.(error, arg) ?? 'Error validating');
		},
	};
}