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

@wtheff/rpc

v1.0.1

Published

RPC generator for @wtheff

Readme

logo.png

@wtheff/rpc

A TypeScript library for generating type-safe Remote Procedure Calls (RPCs) with built-in schema validation using Suretype or TypeBox.

Features

  • Create type-safe query and mutation RPCs
  • Built-in schema validation using Suretype/TypeBox
  • Runtime validation of arguments
  • Generated JSON-Schema for RPCs

Installation

npm install @wtheff/rpc

Usage

Basic Example

import { RPCGenerator } from '@wtheff/rpc';
import { v } from 'suretype';

const { RegisterRPC } = RPCGenerator();

// Register a query RPC
const { getUserInfo } = RegisterRPC.query(
  'getUserInfo',
  {
    schema: {
      args: v.object({ userId: v.string().required() }),
      data: v.object({
        name: v.string().required(),
        email: v.string()
      })
    }
  },
  async (ctx, args) => {
    // Handle the RPC call
    return {
      name: 'John Doe',
      email: '[email protected]'
    };
  }
);

// Call the RPC
const userInfo = await getUserInfo({ userId: '123' });

With Custom Context

interface CustomContext {
  authToken: string;
}

const { RegisterRPC } = RPCGenerator<CustomContext>({
  // RPC handler registration callback
  onRegisterRPC: ({ handler, name }) => {
    httpRouter.post(`/rpc/${name}`, async (req) => {
      return handler({ authToken: req.headers.authorization }, req.body);
    });
  }
});

const { CheckIsAuthenticated } = RegisterRPC.query(
  'CheckIsAuthenticated',
  {
    context: async (ctx) => {
      if (validateAuthToken(ctx.authToken)) {
        const user = await getUserFromToken(ctx.authToken);

        return { user }
      }

      throw new RPCError('Invalid auth token');
    },

    schema: {
      data: v.string(),
    }
  },
  async (ctx, args) => {
    // Access context in handler with type-safety
    console.log(ctx.user);
    return 'User authentication is valid';
  }
);

// Call the RPC
CheckIsAuthenticated({authToken:'valid-token'}, {}); // Returns 'User authentication is valid'
CheckIsAuthenticated({authToken:'no-token'}, {}); // Throws 'RPCContextError: Invalid auth token'

Retrieving Registered RPCs

const { RegisterRPC, getRegisteredRPCs } = RPCGenerator();

// Later in your code
const registeredProcedures = getRegisteredRPCs();
const userInfoProcedure = registeredProcedures.get('getUserInfo');

Generated JSON-Schema

const { RegisterRPC, getRegisteredRPCs } = RPCGenerator();

const { getUserInfo, getUserInfoSchema } = RegisterRPC.query(
  'getUserInfo',
  {
    schema: {
      args: v.object({ userId: v.string().required() }),
      data: v.object({
        name: v.string().required(),
        email: v.string()
      })
    }
  },
  async (ctx, args) => {
    // Handle the RPC call
    return {
      name: 'John Doe',
      email: ''
    };
    }
);

console.log(getUserInfoSchema); // { args: JSON-schema; data: JSON-schema }
// or
console.log(getRegisteredRPCs().get('getUserInfo').config.schema); // { args: JSON-schema; data: JSON-schema }

API Reference

RPCGenerator(options?)

Creates a new RPC generator instance.

Options

  • onRegisterRPC: Callback function called when a new RPC is registered
    • handler: The RPC handler function
    • config: Configuration object containing schema and local RPC context handler
    • name: Name of the RPC procedure

RegisterRPC

Object containing methods to register new RPCs.

Methods

  • query(name, options, handler): Register a query RPC
  • mutation(name, options, handler): Register a mutation RPC

Note: query and mutation methods are identical, except for the type of RPC registered. These RPC "type"'s are used in client implementation to differentiate between query and mutation RPCs. This helps clients support caching calls appropriately if using a cache layer. For example, you can cache
query RPCs but should not cache mutation RPCs.

Options

  • schema:
    • args: Suretype/TypeBox validation schema for arguments
    • data: Suretype/TypeBox validation schema for return data
  • context: Function that returns additional context local to the RPC call

Error Handling

The library includes built-in validation error handling through the RPCValidationError class.

If the RPC config.args schema exists, the args are validated before the RPC handler is called. If the validation fails, an RPCValidationError is thrown with the validation error message.

Acknowledgements

  • @sinclair/typebox - For providing a great library for generating JSON schema with TypeScript.
  • @suretype/suretype - For providing a great library for runtime type validation.
  • @typeschema - For providing inspiration for the RPC schema resolver between Suretype or TypeBox.

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.