DEV Community

Alex Spinov
Alex Spinov

Posted on

tRPC v11 Has a Free API That Most Developers Dont Know About

tRPC v11 brings SSE subscriptions, FormData uploads, and improved middleware.

Middleware

const isAuthed = t.middleware(async ({ ctx, next }) => {
  if (!ctx.user) throw new TRPCError({ code: "UNAUTHORIZED" });
  return next({ ctx: { user: ctx.user } });
});
Enter fullscreen mode Exit fullscreen mode

Server-Sent Events

onNewMessage: t.procedure.subscription(async function* ({ ctx }) {
  for await (const msg of ctx.stream) yield msg;
})
Enter fullscreen mode Exit fullscreen mode

FormData Upload

upload: t.procedure
  .input(z.object({ file: z.instanceof(File) }))
  .mutation(async ({ input }) => saveFile(input.file))
Enter fullscreen mode Exit fullscreen mode

Key Changes

  • SSE for real-time
  • File upload support
  • Better RSC integration
  • Smaller bundle

Need to scrape or monitor web data at scale? Check out my web scraping actors on Apify or email spinov001@gmail.com for custom solutions.

Top comments (0)