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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@thetinkerinc/commander

v0.2.0

Published

Commander is a SvelteKit utility library that lets you easily add customizable auth protection to your remote functions

Readme

Commander

Commander is a SvelteKit utility library that lets you easily add customizable auth protection to your remote functions

Basic setup

  1. Install
npm install @thetinkerinc/commander
pnpm add @thetinkerinc/commander
bun add @thetinkerinc/commander
yarn add @thetinkerinc/commander
  1. Create a commander
import { makeCommander } from '@thetinkerinc/commander';
import { error } from '@sveltejs/kit';
import auth from './auth';
import db from './db';

export const AuthenticatedCommander = makeCommander(async ({ event }) => {
	const userId = auth.decode(event.cookies.get('auth_token'));
	const user = await db.getUser(userId);
	if (!user) {
		error(403, 'You need to log in to perform this action');
	}
	return {
		userId: user.id
	};
});
  1. Write protected functions
import { AuthenticatedCommander } from './commanders.ts';
import { postSchema } from './schemas';
import db from './db';

export const getPosts = AuthenticatedCommander.query(async ({ ctx }) => {
	return await db.getPosts({
		where: {
			user: ctx.userId
		}
	});
});

export const makePost = AuthenticatedCommander.form(postSchema, async ({ ctx, data }) => {
	return await db.makePost({
		...data,
		user: ctx.userId
	});
});

Detailed usage

You use the makeCommander function to make reusable authentication schemes for your remote functions.

commander = makeCommander(({ event, data }) => any);
commander.query(schema, async ({ ctx, params }) => any);
commander.form(schema, async ({ ctx, data, issue }) => any);
commander.command(schema, async ({ ctx, data }) => any);

You'll have access to the current request's event (no need to call getRequestEvent), as well as whatever data, if any, was provided. The typical body of a commander will perform some sort of validation, return an error if it fails, then optionally return some data which can be used in your remote functions.

The commander will run the function body before each invocation of its remote functions.

Usage with schemas and TypeScript

It's possible to add type safety with Commander through the use of a standard schema (Zod, Valibot, ArkType, etc.).

Commander doesn't do any schema validation on its own. It simply uses SvelteKit's handling of schemas. Because of that, schemas are optional the same way base remote function schemas are optional, and if the provided data fails schema validation, neither the commander body nor the remote function body will be run.

import { error } from '@sveltejs/kit';
import { makeCommander } from '@thetinkerinc/commander';
import * as v from 'valibot';
import db from './db';

const postSchema = v.object({
	title: v.string(),
	body: v.pipe(v.string(), v.maxLength(256))
});

const commander = makeCommander(postSchema, ({ event, data }) => {
	// data is guaranteed to conform to postSchema
	const userId = event.cookies.get('user_id');
	if (!userId) {
		error(403, 'Please log in first');
	}
	return userId;
});

export const createPost = commander.form(postSchema, ({ ctx, data }) => {
	await db.createPost({
		...data,
		userId: ctx
	});
});

export const updatePost = commander.form(postSchema, ({ data }) => {
	await db.updatePost(data);
});

When using forms, you'll also have access to the issue parameter used for programmatic validation.

export const makeWithdrawal = commander.form(withdrawalSchema, async ({ ctx, data, issue }) => {
	const balance = await db.getAccountBalance(ctx.userId);
	if (data.amount > balance) {
		invalid(issue.amount('You can not withdraw more than your total balance'));
	}
	await db.withdraw(ctx.userId, data.amount);
});

Notes on using schemas to access data

Commanders are meant to be generic, reusable wrapper functions around your remote functions. Any individual data validation or schema checks should be done in the remote function body, not in the commander. You should use the passed data when you have a set of remote functions which all accept the same or similar shapes of data and all need to pass a certain test before being run.

For example, if all of your forms have hidden userId and orgId fields, you could use a commander to validate the user and the organization, and return the populated user object if it passes validation.