web-data-fetching-trpc-944b55 — independently scanned and version-tracked by SaferSkills.
SaferSkills independently audited web-data-fetching-trpc-944b55 (Agent Skill) and scored it 100/100 (green). The audit ran 55 deterministic rules across Security, Supply Chain, Maintenance, Transparency, and Community; it found 0 high-severity and 0 lower-severity findings. The full rule-by-rule trace and per-finding evidence are below. Free, methodology-open.
Findings & checks · 0 flagged
Every scanned point with the score it earned and what moved between them.
First recorded scan — no prior version to compare against.
The primary manifest — the file an agent reads to learn what this artifact does.
Quick Guide: tRPC provides end-to-end type safety by sharing TypeScript types directly from server to client -- no code generation, no schema files. ExportAppRoutertype from your router (this is the key bridge). Use Zod for input validation,TRPCErrorwith proper codes for errors, and middleware for auth. v11 is the current stable version: transformer goes insidehttpBatchLink(), subscriptions use async generators (notobservable()), and@trpc/tanstack-react-queryis the recommended React integration.
<critical_requirements>
(You MUST export `AppRouter` type from your tRPC router for client-side type inference)
(You MUST use `TRPCError` with appropriate error codes -- never throw raw Error objects)
(You MUST use Zod for input validation on ALL procedures accepting user input)
(You MUST place transformer inside `httpBatchLink()` in v11 -- NOT at client level)
</critical_requirements>
Auto-detection: tRPC router, initTRPC, createTRPCClient, createTRPCContext, @trpc/server, @trpc/client, @trpc/react-query, @trpc/tanstack-react-query, TRPCError, procedure, publicProcedure, protectedProcedure, query, mutation, subscription, httpBatchLink, queryOptions, mutationOptions, useTRPC
When to use:
When NOT to use:
Key patterns covered:
@trpc/tanstack-react-query (recommended) or @trpc/react-query (classic)Detailed Resources:
<philosophy>
tRPC eliminates API layer friction by sharing types directly between server and client. No schemas to write, no code to generate -- export your router type and import it client-side for full autocompletion and type safety.
Core principles:
Trade-offs:
</philosophy>
<patterns>
Initialize tRPC once per application. Export the router and procedure factories.
import { initTRPC, TRPCError } from "@trpc/server";
import { ZodError } from "zod";
import type { Context } from "./context";
const t = initTRPC.context<Context>().create({
errorFormatter({ shape, error }) {
return {
...shape,
data: {
...shape.data,
zodError:
error.cause instanceof ZodError ? error.cause.flatten() : null,
},
};
},
});
export const router = t.router;
export const publicProcedure = t.procedure;
export const middleware = t.middleware;Why good: Single initialization point, error formatter provides structured Zod errors to client, exported factories enable composition across router files
See examples/core.md Pattern 1 for complete router and context factory.
Zod schemas provide runtime validation AND TypeScript inference from a single source.
const createUserSchema = z.object({
email: z.string().email(),
name: z.string().min(1).max(100),
});
export const userRouter = router({
create: protectedProcedure
.input(createUserSchema)
.mutation(async ({ input, ctx }) => {
// input is typed: { email: string; name: string }
return ctx.db.user.create({ data: input });
}),
});// BAD: No input validation -- input is 'unknown'
publicProcedure.mutation(async ({ input }) => {
return ctx.db.user.create({ data: input as any }); // Dangerous!
});Why bad: Without Zod validation, input is unknown type, no runtime validation, injection risks, as any defeats TypeScript
See examples/core.md Pattern 2 for complete CRUD router.
Middleware narrows context types -- ctx.user becomes non-nullable after auth middleware.
const isAuthenticated = middleware(async ({ ctx, next }) => {
if (!ctx.session || !ctx.user) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return next({ ctx: { ...ctx, session: ctx.session, user: ctx.user } });
});
export const protectedProcedure = publicProcedure.use(isAuthenticated);Why good: Auth enforced at procedure definition, TypeScript narrows ctx.user to non-nullable, eliminates duplicated if-checks in every handler
See examples/middleware.md for logging, rate limiting, and org-scoped access patterns.
This is the KEY to tRPC's type safety. Export the router type for client-side inference.
export const appRouter = router({
user: userRouter,
post: postRouter,
});
// THIS IS ESSENTIAL -- without it, clients have no type inference
export type AppRouter = typeof appRouter;Use inferRouterInputs/inferRouterOutputs for extracting procedure types:
import type { inferRouterInputs, inferRouterOutputs } from "@trpc/server";
type RouterInputs = inferRouterInputs<AppRouter>;
type RouterOutputs = inferRouterOutputs<AppRouter>;
// Extract specific type
type User = RouterOutputs["user"]["getById"];See examples/core.md Pattern 4 for complete type inference utilities.
v11 introduces @trpc/tanstack-react-query with queryOptions/mutationOptions factories that work directly with TanStack Query hooks.
// Setup: createTRPCContext provides typed hooks
import { createTRPCContext } from "@trpc/tanstack-react-query";
export const { TRPCProvider, useTRPC } = createTRPCContext<AppRouter>();
// Usage: standard TanStack Query hooks with tRPC type safety
const trpc = useTRPC();
const { data } = useQuery(trpc.user.getById.queryOptions({ id: userId }));v11 CRITICAL: Transformer must be inside httpBatchLink(), NOT at createTRPCClient() level.
// BAD: v11 error
createTRPCClient({ transformer: superjson, links: [...] });
// GOOD: transformer inside the link
httpBatchLink({ url: "/api/trpc", transformer: superjson });See examples/core.md Patterns 3 and 5 for complete provider and component setup.
Use standardized error codes that map to HTTP status codes.
// Server: throw TRPCError with appropriate code
throw new TRPCError({
code: "NOT_FOUND",
message: "User not found",
});
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Failed to delete",
cause: error, // Preserves original stack trace
});// Client: typed error handling
const trpc = useTRPC();
const deletePost = useMutation({
...trpc.post.delete.mutationOptions(),
onError: (error) => {
switch (error.data?.code) {
case "NOT_FOUND":
toast.error("Not found");
break;
case "FORBIDDEN":
toast.error("Not allowed");
break;
}
},
});See reference.md for complete error code table with HTTP status mappings.
Cancel queries, snapshot state, optimistically update, rollback on error, invalidate on settle.
const trpc = useTRPC();
const queryClient = useQueryClient();
const toggleTodo = useMutation({
...trpc.todo.toggle.mutationOptions(),
onMutate: async ({ id }) => {
await queryClient.cancelQueries({ queryKey: trpc.todo.list.queryKey() });
const previousTodos = queryClient.getQueryData(trpc.todo.list.queryKey());
queryClient.setQueryData(trpc.todo.list.queryKey(), (old: any) =>
old?.map((t: any) =>
t.id === id ? { ...t, completed: !t.completed } : t,
),
);
return { previousTodos };
},
onError: (err, vars, context) => {
if (context?.previousTodos)
queryClient.setQueryData(
trpc.todo.list.queryKey(),
context.previousTodos,
);
},
onSettled: () =>
queryClient.invalidateQueries({ queryKey: trpc.todo.list.queryKey() }),
});Why good: Immediate UI feedback, automatic rollback on failure, eventual consistency via invalidation
See examples/optimistic-updates.md for complete pattern with like button example.
</patterns>
<red_flags>
High Priority Issues:
TRPCError with appropriate code for HTTP mappingunknownhttpBatchLink(), not at createTRPCClient() levelMedium Priority Issues:
onError handler to restore previous stateobservable() is the v10 patterngetRawInput() functionGotchas & Edge Cases:
httpBatchLink combines requests -- all batched requests share the same HTTP status codequeryKey() method (v11) or getQueryKey() for manual accesstracked() requires lastEventId in input schemaretry: false) -- retrying writes can cause duplicates</red_flags>
<critical_reminders>
(You MUST export `AppRouter` type from your tRPC router for client-side type inference)
(You MUST use `TRPCError` with appropriate error codes -- never throw raw Error objects)
(You MUST use Zod for input validation on ALL procedures accepting user input)
(You MUST place transformer inside `httpBatchLink()` in v11 -- NOT at client level)
Failure to follow these rules will break type safety, cause runtime errors, and defeat the purpose of using tRPC.
</critical_reminders>
~30 seconds. Free. No account. Every finding cites a rule and a line of evidence.