new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

tRPC · all subjects

server adapters/fastify

10 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Install Fastify adapter dependencies

To use tRPC with Fastify, install the following dependencies: yarn add @trpc/server fastify zod

Fastify adapter version requirement

The tRPC v11 Fastify adapter requires Fastify v5 or higher. Using Fastify v4 may cause requests to return empty responses without errors.

Fastify adapter conversion

The tRPC Fastify adapter converts your tRPC router into a Fastify plugin via fastifyTRPCPlugin. When setting up the Fastify server, set the maxParamLength router option to a suitable value to prevent errors during large batch requests.

Fastify HTTP endpoint routes for queries and mutations

After registering the fastifyTRPCPlugin with tRPC, queries are available as GET requests and mutations as POST requests. For example: queries use GET http://localhost:3000/trpc/procedureName?input=INPUT where INPUT is a URI-encoded JSON string, and mutations use POST http://localhost:3000/trpc/procedureName with the request body containing the input data.

Fastify plugin registration example

Example of registering fastifyTRPCPlugin: server.register(fastifyTRPCPlugin, { prefix: '/trpc', trpcOptions: { router: appRouter, createContext, onError({ path, error }) { console.error(`Error in tRPC handler on path '${path}':`, error); }, } satisfies FastifyTRPCPluginOptions<AppRouter>['trpcOptions'], });

Fastify context creation

The context function receives CreateFastifyContextOptions which contains req and res properties. Example: export function createContext({ req, res }: CreateFastifyContextOptions) { const user = { name: req.headers.username ?? 'anonymous' }; return { req, res, user }; }

Fastify plugin options configuration

The fastifyTRPCPlugin accepts the following options: prefix (string, optional, default '/trpc') - URL prefix for tRPC routes; useWSS (boolean, optional, default false) - Enable WebSocket support via @fastify/websocket; trpcOptions (FastifyHandlerOptions<AppRouter, Request, Reply>, required) - tRPC handler options including router, createContext, etc.

Fastify sample router example

Sample Fastify tRPC router with query and mutation: import { initTRPC } from '@trpc/server'; import { z } from 'zod'; type User = { id: string; name: string; bio?: string; }; const users: Record<string, User> = {}; export const t = initTRPC.create(); export const appRouter = t.router({ getUserById: t.procedure.input(z.string()).query((opts) => { return users[opts.input]; }), createUser: t.procedure .input( z.object({ name: z.string().min(3), bio: z.string().max(142).optional(), }), ) .mutation((opts) => { const id = Date.now().toString(); const user: User = { id, ...opts.input }; users[user.id] = user; return user; }), }); export type AppRouter = typeof appRouter;

Fastify server setup example

Example of setting up a Fastify server with tRPC: import { fastifyTRPCPlugin, FastifyTRPCPluginOptions, } from '@trpc/server/adapters/fastify'; import fastify from 'fastify'; import { createContext } from './context'; import { appRouter, type AppRouter } from './router'; const server = fastify({ routerOptions: { maxParamLength: 5000, }, }); server.register(fastifyTRPCPlugin, { prefix: '/trpc', trpcOptions: { router: appRouter, createContext, onError({ path, error }) { console.error(`Error in tRPC handler on path '${path}':`, error); }, } satisfies FastifyTRPCPluginOptions<AppRouter>['trpcOptions'], }); (async () => { try { await server.listen({ port: 3000 }); } catch (err) { server.log.error(err); process.exit(1); } })();

tRPC Fastify adapter

Host tRPC API using Fastify plugin. Refer to adapter-fastify skill for setup and configuration.

Give your agent this brain