this repo has no description
at master 2.4 kB view raw
1/** 2 * YOU PROBABLY DON'T NEED TO EDIT THIS FILE, UNLESS: 3 * 1. You want to modify request context (see Part 1). 4 * 2. You want to create a new middleware or type of procedure (see Part 3). 5 * 6 * TL;DR - This is where all the tRPC server stuff is created and plugged in. The pieces you will 7 * need to use are documented accordingly near the end. 8 */ 9import { initTRPC } from "@trpc/server"; 10import superjson from "superjson"; 11import { ZodError } from "zod"; 12 13import { db } from "~/server/db"; 14 15/** 16 * 1. CONTEXT 17 * 18 * This section defines the "contexts" that are available in the backend API. 19 * 20 * These allow you to access things when processing a request, like the database, the session, etc. 21 * 22 * This helper generates the "internals" for a tRPC context. The API handler and RSC clients each 23 * wrap this and provides the required context. 24 * 25 * @see https://trpc.io/docs/server/context 26 */ 27export const createTRPCContext = async (opts: { headers: Headers }) => { 28 return { 29 db, 30 ...opts, 31 }; 32}; 33 34/** 35 * 2. INITIALIZATION 36 * 37 * This is where the tRPC API is initialized, connecting the context and transformer. We also parse 38 * ZodErrors so that you get typesafety on the frontend if your procedure fails due to validation 39 * errors on the backend. 40 */ 41const t = initTRPC.context<typeof createTRPCContext>().create({ 42 transformer: superjson, 43 errorFormatter({ shape, error }) { 44 return { 45 ...shape, 46 data: { 47 ...shape.data, 48 zodError: 49 error.cause instanceof ZodError ? error.cause.flatten() : null, 50 }, 51 }; 52 }, 53}); 54 55/** 56 * Create a server-side caller. 57 * 58 * @see https://trpc.io/docs/server/server-side-calls 59 */ 60export const createCallerFactory = t.createCallerFactory; 61 62/** 63 * 3. ROUTER & PROCEDURE (THE IMPORTANT BIT) 64 * 65 * These are the pieces you use to build your tRPC API. You should import these a lot in the 66 * "/src/server/api/routers" directory. 67 */ 68 69/** 70 * This is how you create new routers and sub-routers in your tRPC API. 71 * 72 * @see https://trpc.io/docs/router 73 */ 74export const createTRPCRouter = t.router; 75 76/** 77 * Public (unauthenticated) procedure 78 * 79 * This is the base piece you use to build new queries and mutations on your tRPC API. It does not 80 * guarantee that a user querying is authorized, but you can still access user session data if they 81 * are logged in. 82 */ 83export const publicProcedure = t.procedure;