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 🙏

© 2025 – Pkg Stats / Ryan Hefner

ts-rpc-http

v0.0.49

Published

Quickly generate a type safe RPC API over HTTP with Express along with a type safe promise based client

Readme

Type Safe HTTP JSON RPC API with TypeScript

Coverage Status

This small set of utilies makes it very easy to create an RPC API that is definined completely in TypeScript along with a generated client and optional automated api validation.

Installation

Install from NPM with the following:

npm i ts-rpc-http

Motivation

Type Hints

ts-rpc-http gives you type hints for all of your rpc routes on the server, enabling you to quickly understand exactly what json body you are receiving.

The response is also typed, ensuring our contract between server and client.

Type Safety

Beyond the type hints provided, ts-rpc-http also provides safety in that it will alert you if the response you are sending is invalid per the api contract:

Automatic Request Validation

By generating JSON schemas, your requests can be automatically validated with a small piece of middleware. Below is an example of JSONSchema generation:

npx ts-generate --model src/models.ts --schemas

Automatic Client Generation

A client is generated for you that works directly with Promise, making it extremely easy to get started on the frontend once the api service is available for consumption. Below is an example client generation command:

npx ts-generate --model src/models.ts --clients clients.ts

Model

It all starts with a model file, where you define all of your basic data model types and one or more service definitions with this comment tagging them on the line above //@http-rpc(<serviceName>). Here is a simple example:

import { RequestResponse, RPCService } from 'ts-rpc-http/requestResponse';

export interface createTodoRequest {
  description: string;
}

export interface Todo {
  id: string;
  description: string;
  dateCreated: Date;
}

export interface ServiceDefinition extends RPCService<"Todo"> {
  createTodo: RequestResponse<createTodoRequest, Todo>;
  createTodoAsync: RequestResponse<createTodoRequest, Todo>;
}

Server

The Server is a small wrapper around express that provides typing of the request body that is sent in JSON. It also provides optional automatic validation through use of generated JSONSchemas.

If you want to generate JSONSchema files for automatic validation, go ahead and run this command:

npx ts-generate --model src/models.ts --schemas

Where src/models.ts is the location of your models file. A directory called schema will be created and schema files will be created for each req/res import of your service.

Now we can create an example service:

import { ServiceDefinition } from './models';
import { Server } from 'ts-rpc-http/server';

const server = new Server<ServiceDefinition>();

//validate schemas per the schema folder (optional)
server.validateSchemas('/src/schema');

server.rpc('createTodoAsync', async (req, res) => {
  //do some async stuff
  const description: string = await new Promise((resolve, reject) => {
    return resolve('promise description' + req.body.description);
  });

  res.status(200).send({
    id: 'random-id',
    dateCreated: new Date(),
    description,
  });
});

server.rpc('createTodo', (req, res) => {
  res.status(200).send({
    id: 'random-id',
    dateCreated: new Date(),
    description: req.body.description,
  });
});

server
  .start()
  //no need to log this as it is done by the server automatically with the port
  .then(r => console.log('Started server...'))
  .catch(e => new Error(e));

Client

There is a base Client that can be used to make basic calls to the API, but the preferred way is to use the generated clients, making. All you need to do is copy the same models to your frontend and you can generate it there, or generate the clients with the server and provide it as a library.

To generate a client, type the following:

npx ts-generate --model src/models.ts --clients clients.ts

Which will generate as many clients as you have service definitions and put them in a file called clients.ts in the same directory as src/models.ts which in this case is src/. it may look something like the following:

import Client from 'ts-rpc-http/client';
import {
  ServiceDefinition,
  createTodoRequest,
  Todo,
} from './models';

export class TodoClient {
  private client: Client<ServiceDefinition>;
  constructor(baseURL: string) {
    this.client = new Client(baseURL);
  }

  public createTodo = async (
    body: createTodoRequest,
    token?: string
  ): Promise<Todo> => this.client.call('createTodo', body, token);

}