mirror of
https://github.com/Xevion/xevion.dev.git
synced 2025-12-10 08:09:17 -06:00
Repository init
This commit is contained in:
26
src/server/trpc/context.ts
Normal file
26
src/server/trpc/context.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { type inferAsyncReturnType } from "@trpc/server";
|
||||
import { type CreateNextContextOptions } from "@trpc/server/adapters/next";
|
||||
|
||||
/**
|
||||
* Replace this with an object if you want to pass things to createContextInner
|
||||
*/
|
||||
type CreateContextOptions = Record<string, never>;
|
||||
|
||||
/** Use this helper for:
|
||||
* - testing, so we dont have to mock Next.js' req/res
|
||||
* - trpc's `createSSGHelpers` where we don't have req/res
|
||||
* @see https://create.t3.gg/en/usage/trpc#-servertrpccontextts
|
||||
**/
|
||||
export const createContextInner = async (opts: CreateContextOptions) => {
|
||||
return {};
|
||||
};
|
||||
|
||||
/**
|
||||
* This is the actual context you'll use in your router
|
||||
* @link https://trpc.io/docs/context
|
||||
**/
|
||||
export const createContext = async (opts: CreateNextContextOptions) => {
|
||||
return await createContextInner({});
|
||||
};
|
||||
|
||||
export type Context = inferAsyncReturnType<typeof createContext>;
|
||||
9
src/server/trpc/router/_app.ts
Normal file
9
src/server/trpc/router/_app.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { router } from "../trpc";
|
||||
import { exampleRouter } from "./example";
|
||||
|
||||
export const appRouter = router({
|
||||
example: exampleRouter,
|
||||
});
|
||||
|
||||
// export type definition of API
|
||||
export type AppRouter = typeof appRouter;
|
||||
13
src/server/trpc/router/example.ts
Normal file
13
src/server/trpc/router/example.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { router, publicProcedure } from "../trpc";
|
||||
|
||||
export const exampleRouter = router({
|
||||
hello: publicProcedure
|
||||
.input(z.object({ text: z.string().nullish() }).nullish())
|
||||
.query(({ input }) => {
|
||||
return {
|
||||
greeting: `Hello ${input?.text ?? "world"}`,
|
||||
};
|
||||
}),
|
||||
});
|
||||
15
src/server/trpc/trpc.ts
Normal file
15
src/server/trpc/trpc.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { initTRPC } from "@trpc/server";
|
||||
import superjson from "superjson";
|
||||
|
||||
import { type Context } from "./context";
|
||||
|
||||
const t = initTRPC.context<Context>().create({
|
||||
transformer: superjson,
|
||||
errorFormatter({ shape }) {
|
||||
return shape;
|
||||
},
|
||||
});
|
||||
|
||||
export const router = t.router;
|
||||
|
||||
export const publicProcedure = t.procedure;
|
||||
Reference in New Issue
Block a user