@http-rpc/client
TypeScript icon, indicating that this package has built-in type declarations

0.2.15 • Public • Published

HTTP RPC Client

Documentation

Full documentation can be found here.

Installation

npm i @http-rpc/client superjson

Usage

./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 }[]

Package Sidebar

Install

npm i @http-rpc/client

Weekly Downloads

34

Version

0.2.15

License

MIT

Unpacked Size

5.38 kB

Total Files

4

Last publish

Collaborators

  • lachero