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

sveltekit-superactions

v0.7.0

Published

<img src="https://raw.githubusercontent.com/kumpmati/superactions-docs/main/public/logo.webp" width="64px" align="center" alt="Superactions logo" /> <h1>SvelteKit Superactions</h1>

Downloads

67

Readme

Test

Call your server code from the client like normal functions.

🚧 This library is in an early state, and breaking changes will likely happen before the API is stabilised for a v1.0 release! 🚧

Documentation

Why?

While SvelteKit's data fetching patterns are great, but the ease-of-use of React Server Actions doesn't seem to have an equivalent in SvelteKit. The ability to just 'call a function' in the client-side, have it perform logic on the server and return the result to the client is sometimes very useful.

SvelteKit's form actions are a great fit for many cases, but they can be clunky when you want to call an endpoint without a form element, or when you need to send data that is too complex to be represented in FormData.

This library aims to provide additional tools alongside SvelteKit's API routes:

Features

  • [x] Type satefy between server and client
  • [x] Automatic JSON conversion (request/response)
  • [x] Schema validation
    • [x] zod helper function
    • [x] joi helper function

Installation

Install Superactions with your favourite package manager:

# npm, yarn, pnpm, bun, etc
npm i sveltekit-superactions

Usage

A minimal setup requires the following.

In a +server.ts file, define the actions that you want to use:

// src/routes/api/+server.ts
import { endpoint } from 'sveltekit-superactions';
import { db } from '$lib/server/db';
import { deleteTodo, findTodo, type TodoUpdate } from '$lib/server/handlers';

// Always attach the endpoint as a POST handler
export const POST = endpoint({
	// e is the RequestEvent provided by SvelteKit,
	// and the second argument is the request body decoded as JSON.
	editTodo: async (e, body: TodoUpdate) => {
		// The returned value is automatically serialized as JSON.
		// The client-side function gets its return type directly from the return type of its server action
		return await db.update(body.id, body);
	},

	// You can also just import handlers from other files and group them here.
	deleteTodo,

	// Or give them custom names
	my_handler: findTodo
});

// export the type of the endpoint, so that we get types in the client
export type API = typeof POST;

And in any Svelte component import the superActions function and the exported types to instantiate the client.

<!-- src/routes/+page.svelte -->
<script lang="ts">
	import { superActions } from 'sveltekit-superactions';
	import type { API } from './api/+server.js'; // exported API type

	// give the client the path and API types to instantiate it.
	const api = superActions<API>('/api');
</script>

{#await api.getTodos()}
	<p>Loading TODOs...</p>
{:then todos}
	<ul>
		{#each todos as todo}
			<li>
				{todo.text}
				<input type="checkbox" checked={todo.checked} />
			</li>
		{/each}
	</ul>
{/await}