"Stockfight" is a simple frontend to compare stocks on the swedish stock market
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 */
9
10import { initTRPC } from "@trpc/server";
11import { type CreateNextContextOptions } from "@trpc/server/adapters/next";
12import superjson from "superjson";
13import { ZodError } from "zod";
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
23type CreateContextOptions = Record<string, never>;
24
25/**
26 * This helper generates the "internals" for a tRPC context. If you need to use it, you can export
27 * it from here.
28 *
29 * Examples of things you may need it for:
30 * - testing, so we don't have to mock Next.js' req/res
31 * - tRPC's `createSSGHelpers`, where we don't have req/res
32 *
33 * @see https://create.t3.gg/en/usage/trpc#-serverapitrpcts
34 */
35const createInnerTRPCContext = (_opts: CreateContextOptions) => {
36 return {};
37};
38
39/**
40 * This is the actual context you will use in your router. It will be used to process every request
41 * that goes through your tRPC endpoint.
42 *
43 * @see https://trpc.io/docs/context
44 */
45export const createTRPCContext = (_opts: CreateNextContextOptions) => {
46 return createInnerTRPCContext({});
47};
48
49/**
50 * 2. INITIALIZATION
51 *
52 * This is where the tRPC API is initialized, connecting the context and transformer. We also parse
53 * ZodErrors so that you get typesafety on the frontend if your procedure fails due to validation
54 * errors on the backend.
55 */
56
57const t = initTRPC.context<typeof createTRPCContext>().create({
58 transformer: superjson,
59 errorFormatter({ shape, error }) {
60 return {
61 ...shape,
62 data: {
63 ...shape.data,
64 zodError:
65 error.cause instanceof ZodError ? error.cause.flatten() : null,
66 },
67 };
68 },
69});
70
71/**
72 * 3. ROUTER & PROCEDURE (THE IMPORTANT BIT)
73 *
74 * These are the pieces you use to build your tRPC API. You should import these a lot in the
75 * "/src/server/api/routers" directory.
76 */
77
78/**
79 * This is how you create new routers and sub-routers in your tRPC API.
80 *
81 * @see https://trpc.io/docs/router
82 */
83export const createTRPCRouter = t.router;
84
85/**
86 * Public (unauthenticated) procedure
87 *
88 * This is the base piece you use to build new queries and mutations on your tRPC API. It does not
89 * guarantee that a user querying is authorized, but you can still access user session data if they
90 * are logged in.
91 */
92export const publicProcedure = t.procedure;