Client
Byte provides a client named bit
with route type reflection.
To use the client, you need to export the app type.
import { Byte, send, parse } from '@bit-js/byte';
// Chaining is required for type inferenceconst app = new Byte() .get('/', send.body('Hi')) .post('/echo', async (ctx) => ctx.json(await ctx.req.json()));
export type TApp = typeof app;
Then import the type and use it in client-side code:
import type { TApp } from './server';import { bit } from '@bit-js/byte';
// Base URL to create requestsconst client = bit<TApp>('http://localhost:3000');
// Routes get automatic type inferenceconst msg = await client.get('/');await msg.text(); // 'Hi'
const echo = await client.post('/echo', { // Body get automatically serialized body: { hi: 'there' }});await echo.json(); // { hi: 'there' }
Testing
You can create a similar client for server-side testing:
const client = app.client();
The difference is that the client side bundle will not include the whole server-side app.