Trpc

From InugamiDev/ultrathink-oss · View interactive version

Trpc is an AI agent skill from the GitHub repository InugamiDev/ultrathink-oss. > End-to-end typesafe APIs — no code generation, no schemas, just TypeScript inference from backend to frontend.

How to use this skill

Skill file

tRPC

End-to-end typesafe APIs — no code generation, no schemas, just TypeScript inference from backend to frontend.

When to Use

Core Patterns

Router Definition

import { initTRPC, TRPCError } from "@trpc/server";
import { z } from "zod";

const t = initTRPC.context<Context>().create();

const appRouter = t.router({
  user: t.router({
    byId: t.procedure
      .input(z.object({ id: z.string().uuid() }))
      .query(async ({ input, ctx }) => {
        const user = await ctx.db.user.findUnique({ where: { id: input.id } });
        if (!user) throw new TRPCError({ code: "NOT_FOUND", message: "User not found" });
        return user;
      }),
    create: t.procedure
      .input(z.object({ name: z.string().min(1), email: z.string().email() }))
      .mutation(async ({ input, ctx }) => ctx.db.user.create({ data: input })),
  }),
});

export type AppRouter = typeof appRouter;

Middleware & Context

const createContext = async ({ req }: { req: Request }) => ({
  db: prisma,
  user: await getUserFromHeader(req),
});

const authed = t.middleware(({ ctx, next }) => {
  if (!ctx.user) throw new TRPCError({ code: "UNAUTHORIZED" });
  return next({ ctx: { ...ctx, user: ctx.user } }); // Narrowed type
});
const protectedProcedure = t.procedure.use(authed);

React Query Integration

import { createTRPCReact } from "@trpc/react-query";
import type { AppRouter } from "~/server/router";

export const trpc = createTRPCReact<AppRouter>();

const { data } = trpc.user.byId.useQuery({ id: "abc" }); // Fully typed
const mutation = trpc.user.create.useMutation({
  onSuccess: () => utils.user.byId.invalidate(),
});

Server-Side Caller

const caller = appRouter.createCaller(await createContext({ req }));
const user = await caller.user.byId({ id: "abc" }); // Direct call, no HTTP

Key Features

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.