This skill helps you implement and integrate a typesafe-rpc workflow in Node/TypeScript by generating end-to-end typed client and server.
npx playbooks add skill bacali95/typesafe-rpc --skill typesafe-rpc
This repo is a type-safe RPC library. The server accepts POST bodies { entity, operation, params } and runs the matching handler. The client is a typed proxy: client.entity.operation(params, signal?, context?). Types are shared so client calls are inferred from the schema.
For real-time server-push/subscriptions over WebSocket, see the companion typesafe-ws package (skills/typesafe-ws/SKILL.md) in this same repo — typesafe-rpc itself is HTTP request/response only.
{ request: Request | Express.Request }{ params: Params; context: Context }(args: Args<Params, Context>) => Promise<Result>{ [entity: string]: { [operation: string]: Handler<any, any, any> } }Handlers receive only params and context (no ExtraParams). Extend BaseContext for app-specific context.
Schema is a nested object: entity → operation → handler. Use as const so the client gets literal types.
import type { BaseContext, Handler } from 'typesafe-rpc';
type Ctx = BaseContext & { userId?: string };
const getItem: Handler<{ id: string }, Ctx, { name: string }> = async ({ params, context }) => {
return { name: 'Item ' + params.id };
};
export const apiSchema = {
items: {
getById: getItem,
},
} as const;
From typesafe-rpc/server:
import { createRpcHandler } from 'typesafe-rpc/server';
const result = await createRpcHandler({
context: { request }, // must include request
operations: apiSchema,
errorHandler: (error) => new Response(JSON.stringify({ error: '...' }), { status: 500 }),
hooks: {
preCall: (args) => {},
postCall: (args, performance) => {},
error: (args, performance, error) => {},
},
});
context.request.method === 'POST'; body must be JSON { entity, operation, params }.{ entity, operation, params, context }.Response on error.errorHandler, throws a generic 500 Response.From typesafe-rpc/client:
import { createRpcClient } from 'typesafe-rpc/client';
import type { apiSchema } from './api-schema';
// Static headers
const client = createRpcClient<typeof apiSchema>('/api/rpc', { Authorization: 'Bearer ...' });
// Dynamic headers via function (receives context passed to call)
const client = createRpcClient<typeof apiSchema, MyContext>('/api/rpc', (ctx) => ({
Authorization: ctx.token,
}));
const result = await client.items.getById({ id: '1' });
const withAbort = await client.items.getById({ id: '1' }, signal);
const withContext = await client.items.getById({ id: '1' }, undefined, context);
Client signature: client.entity.operation(params, signal?, context?). Calls POST endpoint?entity::operation with body { entity, operation, params }. Use the same schema type (typeof apiSchema) for full inference.
From typesafe-rpc/server: Route, Middleware, orMiddleware.
(args: Args<Params, Context>) => Promise<void> (throw to abort)..middleware(...fns) then .handle(handler)..middleware(a, b, c): OR — first success wins (via orMiddleware)..middleware(a).middleware(b): AND — all run in order..handle() has an overrideMiddlewares(...middlewares) method to replace middlewares (useful for testing).import { Route } from 'typesafe-rpc/server';
const handler = new Route<{ id: string }, BaseContext>()
.middleware(authOrAnonymous)
.middleware(requireReadPermission)
.handle(async ({ params, context }) => ({ name: '...' }));
// For testing: bypass middlewares
handler.overrideMiddlewares(mockAuth);
Use the resulting handler as the function stored in the schema (e.g. getById: handler).
request.json() (Fetch) or request.body (Express).response.json(). Non-ok responses throw FetchError.From typesafe-rpc/client:
import { FetchError } from 'typesafe-rpc/client';
class FetchError extends Error {
readonly key: string; // error key from response JSON, or 'internalError'
readonly status: number; // HTTP status code
readonly data?: any; // optional data from response JSON
}
The client parses error responses as JSON { key, message, data }. If parsing fails, key defaults to 'internalError' and message is the raw response text.
shared/; server in server/, client in client/.libs/typesafe-rpc/src/shared/rpc-types.ts. Prefer those over README if they differ (e.g. no ExtraParams in Handler).Weekly roundup of top Claude Code skills, MCP servers, and AI coding tips.