express-typed-rpc
WARNING! This repo is still a work in progress. README isn't finished, and this repo is still in beta. Read source code if you want to play around, and please contribute if you're interested
Simple express middleware to easily create a fully-typed JSON API over HTTP on both the server-side and client-side. This project is inspired by tRPC, but much simpler.
Crazy Simple and Easy to Use
- Works out of the box with express and Typescript
- No magic or black boxes
- No code generation or build steps! Works 100% statically via Typescript's
infer
keyword - No writing type specifications
- Minimal configuration
- Client included!
- Tiny codebase (~50LOC) with minimal dependencies. Written in clean, simple Typescript. Contribute or fork and make it your own.
Make Your Code More Reliable and Go Faster!
- Take advantage of Typescript and turn your runtime errors into compiler-time errors! Inputs and outputs are both fully typed.
- Easily unit-test your express handlers since they are now no longer dependent on
req
andres
Installation
npm i express-typed-rpc
Example Usage
server.ts
import express from 'express';
import { Router } from 'express';
import { createAPI, InferAPI } from 'express-typed-rpc';
// Define your Express router
const apiRouter = Router();
// Define your API
const api = {
greet: (name: string, context: any): string => `Hello, ${name}!`,
multiply: (numbers: {a: number, b: number}, context: any): number => numbers.a * numbers.b
};
// Create API using the provided function and api object
createAPI(apiRouter, api);
// Export type for use on client
export type API = InferAPI<typeof api>;
// Initialize Express application
const app = express();
// Use the router as middleware at the /api path
app.use('/api', apiRouter);
// Start the Express server
app.listen(process.env.PORT || 3000);
client.ts
import { client } from 'express-typed-rpc';
import { API } from '@yourorg/server' // You must publish your backend as a private repo
// (Github Packages is recommended). This imports
// the type only.
// Everything is now fully typed! Enjoy IDE autocompletion, validation,
// and compile-time TypeScript errors.
const greet = async (name: string): Promise<string> => {
return await client<API['greet']>('greet', name);
};
const multiply = async (numbers: {a: number, b: number}): Promise<number> => {
return await client<API['multiply']>('multiply', numbers);
};
Contribution
Please contribute to this project! Issue a PR against main
and request review.
- Please test your work thoroughly.
- Make sure all tests pass with appropriate coverage.
How to build locally
npm i
Running tests
npm test