mirror of
https://github.com/soconnor0919/lewisburg-coffee.git
synced 2026-02-04 23:56:32 -05:00
100 lines
2.9 KiB
TypeScript
100 lines
2.9 KiB
TypeScript
/**
|
|
* YOU PROBABLY DON'T NEED TO EDIT THIS FILE, UNLESS:
|
|
* 1. You want to modify request context (see Part 1).
|
|
* 2. You want to create a new middleware or type of procedure (see Part 3).
|
|
*
|
|
* TL;DR - This is where all the tRPC server stuff is created and plugged in. The pieces you will
|
|
* need to use are documented accordingly near the end.
|
|
*/
|
|
import { initTRPC } from "@trpc/server";
|
|
import superjson from "superjson";
|
|
import { ZodError } from "zod";
|
|
|
|
/**
|
|
* 1. CONTEXT
|
|
*
|
|
* This section defines the "contexts" that are available in the backend API.
|
|
*
|
|
* These allow you to access things when processing a request, like the database, the session, etc.
|
|
*
|
|
* This helper generates the "internals" for a tRPC context. The API handler and RSC clients each
|
|
* wrap this and provides the required context.
|
|
*
|
|
* @see https://trpc.io/docs/server/context
|
|
*/
|
|
export const createTRPCContext = async (opts: { headers: Headers }) => {
|
|
return {
|
|
...opts,
|
|
};
|
|
};
|
|
|
|
/**
|
|
* 2. INITIALIZATION
|
|
*
|
|
* This is where the tRPC API is initialized, connecting the context and transformer. We also parse
|
|
* ZodErrors so that you get typesafety on the frontend if your procedure fails due to validation
|
|
* errors on the backend.
|
|
*/
|
|
const t = initTRPC.context<typeof createTRPCContext>().create({
|
|
transformer: superjson,
|
|
errorFormatter({ shape, error }) {
|
|
return {
|
|
...shape,
|
|
data: {
|
|
...shape.data,
|
|
zodError:
|
|
error.cause instanceof ZodError ? error.cause.flatten() : null,
|
|
},
|
|
};
|
|
},
|
|
});
|
|
|
|
/**
|
|
* Create a server-side caller.
|
|
*
|
|
* @see https://trpc.io/docs/server/server-side-calls
|
|
*/
|
|
export const createCallerFactory = t.createCallerFactory;
|
|
|
|
/**
|
|
* 3. ROUTER & PROCEDURE (THE IMPORTANT BIT)
|
|
*
|
|
* These are the pieces you use to build your tRPC API. You should import these a lot in the
|
|
* "/src/server/api/routers" directory.
|
|
*/
|
|
|
|
/**
|
|
* This is how you create new routers and sub-routers in your tRPC API.
|
|
*
|
|
* @see https://trpc.io/docs/router
|
|
*/
|
|
export const createTRPCRouter = t.router;
|
|
|
|
/**
|
|
* Middleware for timing procedure execution and adding an artificial delay.
|
|
*
|
|
* You can remove this if you don't want to do it.
|
|
*/
|
|
const timingMiddleware = t.middleware(async ({ next }) => {
|
|
if (t._config.isDev) {
|
|
// artificial delay in dev 100-500ms
|
|
const waitMs = Math.floor(Math.random() * 400) + 100;
|
|
await new Promise((resolve) => setTimeout(resolve, waitMs));
|
|
}
|
|
const result = await next();
|
|
if (t._config.isDev) {
|
|
// const durationMs = Date.now() - start;
|
|
// console.log(`[TRPC] ${path} took ${durationMs}ms to execute`);
|
|
}
|
|
return result;
|
|
});
|
|
|
|
/**
|
|
* Public (unauthenticated) procedure
|
|
*
|
|
* This is the base piece you use to build new queries and mutations on your tRPC API. It does not
|
|
* guarantee that a user querying is authorized, but you can still access user session data if they
|
|
* are logged in.
|
|
*/
|
|
export const publicProcedure = t.procedure.use(timingMiddleware);
|