Full documentation can be found here.
npm i @http-rpc/client superjson
./rpcClient.ts
import superjson from 'superjson';
import { createClient } from '@http-rpc/client';
import type { Router } from '../server/rpc/router';
export const client = createClient<Router>({
url: 'http://localhost:3000/rpc',
transformer: superjson,
async headers() {
const token = await getToken();
return {
Authorization: `Bearer ${token}`,
};
},
/* Optional hooks */
onRequest(req) {
console.log(`${req.method?.toUpperCase() ?? 'Unknown'} ${req.url}`);
return req;
},
onResponse(res) {
console.log(`${res.status} ${res.statusText}`);
return res;
},
onError(err) {
console.error(err);
return err;
},
});
import { client } from './rpcClient';
const version = await client.version.get();
// ^? { version: string }
const orders = await client.orders.list({ fields: ['id', 'amount'] });
// ^? { id: number, amount: number }[]