Skip to content

@http-rpc

Unify Frontend-Backend Interactions

RPC layer on top of http with full typesafety and more

@http-rpc/server

a simple example of how to use @http-rpc/server with fastify:

ts
import Fastify from 'fastify';
import { createRoute } from '@http-rpc/server';
import { rpcFastify, FastifyContext } from '@http-rpc/server/adapters/fastify';

const fastify = Fastify({
	logger: true,
});

const publicRoute = createRoute<FastifyContext>();

const router = {
	version: publicRoute.get(() => {
		return { version: 'v1.0.0' };
	}),
};

export type Router = typeof router;

fastify.register(rpcFastify, {
	prefix: '/rpc',
	router,
});

await fastify.listen({ port: 3000, host: '0.0.0.0' });

@http-rpc/client

a simple example of how to use @http-rpc/client:

ts
import { createClient } from '@http-rpc/client';
import type { Router } from './server';

const client = createClient<Router>({
	url: 'http://localhost:3000/rpc',
});

const versionData = await client.version.get();
//    ^? { version: string }

Released under the MIT License.