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

smolrpc

v0.36.0

Published

A smol typesafe rpc implementation over websockets

Downloads

57

Readme

smolrpc

A really smol typesafe rpc implementation.

Communicates over websockets and supports three operations on user defined resources: get, set, and subscribe.

Uses Zod for runtime typechecking. No other dependencies.

Inspired by the typesafe Typescript apis as done by tRPC (https://trpc.io/), ts-rest (https://ts-rest.com/), and Zodios (https://www.zodios.org/). And by the WebSocket api as done by Sockette (https://github.com/lukeed/sockette).

You define the api in one place and import and use the same api types on both the server and the client.

Get and set ar practically the same, but it is a useful distinction so you can do read/write separation on the backedn, which is useful if you need to scale up and use leader/follower db clusters.

Subscriptions can be defined on the same resources as get and set operations, and are rxjs observable compatible.

All actions are on statically typed resource urls that support statically parsed params. You then define the request and response Zod types for such a resource.

How to use

Resources

First you need to define your typed resources using a resource object like in example/resources.ts.

Server

Then you define a router with resource handlers like in example/nodejs-server/router.ts.

Client

The Client is autogenerated using the resource object as the type and a Proxy object as the implementation, and you can just jump in and use the statically typed client like in example/nodejs-client/index.ts.

// client-example.ts
import { initClient } from 'smolrpc';
import type { Resources } from './resources';

const client = await initClient<SimpleResources>({
	url: 'ws://localhost:9200',
});

// type: { content: string; id: string; }[]
const posts = await client['/posts'].get();
// type: { content: string; id: string; }
const post123 = await client['/posts/:postId'].get({
	params: { postId: 123 },
});
client['/posts/:postId']
	.subscribe({
		params: { postId: 123 },
	})
	.subscribe({
		next: (post) => {
			console.log('event', post);
		},
	});
await client['/posts/:postId'].set({
	params: { postId: 123 },
	request: { content: 'sick post' },
});
// resources.ts
import { z } from 'zod';
import { AnyResources } from 'smolrpc';

const post = z.object({
	content: z.string(),
	id: z.string(),
});

export const resources = {
	'/posts': {
		response: z.array(post),
		type: 'get|subscribe',
	},
	'/posts/:id': {
		request: post.omit({ id: true }),
		response: post,
		type: 'get|set|subscribe',
	},
} as const satisfies AnyResources;
export type Resources = typeof resources;

How to run examples

Run these three commands in three separate terminals:

$ npm run check
$ npm run nodejs-server
$ npm run nodejs-client