This is a "simple" base project that I've been developing for a few months. As the name suggests, it uses Fastify. My initial goal was to create a dynamic router, but the project evolved into something more like tRPC to meet my needs. You can also use this base in your front-end via the packages/rpc
package, as shown below.
Note: This project is developed on-demand as new features are needed and does not have continuous maintenance. Do not expect regular bug fixes!
To use the generated types in the server folder, you need to modify your project's tsconfig.json
to enable dynamic type acquisition:
{
"compilerOptions": {
// The usual
},
"references": [
{ "path": "../server" }
]
}
For an example configuration, see the client/tsconfig.json file.
For more details on what the references
field means, please refer to the TypeScript Project References documentation.
import { Router } from '@/controllers/router.js'
export default new Router({
name: 'Home',
description: 'Home API',
methods: {
get({ reply }) {
return reply.code(200).send({
message: 'hello world',
data: {}
})
}
},
})
import { Client, ErrorResponse } from 'rpc'
import type { Routers } from 'server'
const rpc = new Client<Routers>('http://0.0.0.0:3500')
const result = await rpc.query('/', 'get')
// All routes can return errors—learn how to handle them!
// A request might not always be successful.
if (result instanceof ErrorResponse) {
console.log(result.message)
process.exit()
}
console.log(result.message) // hello world
console.log(result.data) // {}
The routes are dynamically typed, but their types are built in server/src/build/rpc.ts
.
Important: Do not use this project in production unless you’re ready for potential headaches! 😅