mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-26 15:48:32 -06:00
moving to trpc
This commit is contained in:
36
nextjs/src/services/trpc/TrpcProvider.tsx
Normal file
36
nextjs/src/services/trpc/TrpcProvider.tsx
Normal file
@@ -0,0 +1,36 @@
|
||||
"use client";
|
||||
import { useState } from "react";
|
||||
import superjson from "superjson";
|
||||
import { httpBatchLink, httpLink } from "@trpc/client";
|
||||
import { trpc } from "./utils";
|
||||
import { getQueryClient } from "@/app/providersQueryClientUtils";
|
||||
|
||||
export default function TrpcProvider({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
// NOTE: Your production URL environment variable may be different
|
||||
const url = "/api/trpc";
|
||||
// process.env.NEXT_PUBLIC_APP_DOMAIN &&
|
||||
// !process.env.NEXT_PUBLIC_APP_DOMAIN.includes("localhost")
|
||||
// ? `https://www.${process.env.NEXT_PUBLIC_APP_DOMAIN}/api/trpc/`
|
||||
// : "http://localhost:3000/api/trpc/";
|
||||
|
||||
const [trpcClient] = useState(() =>
|
||||
trpc.createClient({
|
||||
links: [
|
||||
httpLink({
|
||||
url,
|
||||
transformer: superjson,
|
||||
}),
|
||||
],
|
||||
})
|
||||
);
|
||||
|
||||
return (
|
||||
<trpc.Provider client={trpcClient} queryClient={getQueryClient()}>
|
||||
{children}
|
||||
</trpc.Provider>
|
||||
);
|
||||
}
|
||||
9
nextjs/src/services/trpc/context.ts
Normal file
9
nextjs/src/services/trpc/context.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { CreateNextContextOptions } from '@trpc/server/adapters/next';
|
||||
|
||||
export const createContext = async () => {
|
||||
|
||||
return {
|
||||
};
|
||||
};
|
||||
|
||||
export type Context = typeof createContext;
|
||||
5
nextjs/src/services/trpc/procedures/public.ts
Normal file
5
nextjs/src/services/trpc/procedures/public.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { procedure } from "../trpc";
|
||||
|
||||
const publicProcedure = procedure;
|
||||
|
||||
export default publicProcedure;
|
||||
23
nextjs/src/services/trpc/router/app.ts
Normal file
23
nextjs/src/services/trpc/router/app.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { createContext } from "../context";
|
||||
import publicProcedure from "../procedures/public";
|
||||
import { createCallerFactory, mergeRouters, router } from "../trpc";
|
||||
|
||||
export const helloRouter = router({
|
||||
sayHello: publicProcedure.query(() => {
|
||||
// runs on the server I think
|
||||
console.log("hello world router on the server?");
|
||||
return { greeting: `Hello World!` };
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
export const appRouter = mergeRouters(helloRouter);
|
||||
|
||||
export const createCaller = createCallerFactory(appRouter);
|
||||
|
||||
export const createAsyncCaller = async () => {
|
||||
const context = await createContext();
|
||||
return createCaller(context);
|
||||
};
|
||||
|
||||
export type AppRouter = typeof appRouter;
|
||||
35
nextjs/src/services/trpc/router/courseItemRouter.ts
Normal file
35
nextjs/src/services/trpc/router/courseItemRouter.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import publicProcedure from "../procedures/public";
|
||||
import { z } from "zod";
|
||||
import { router } from "../trpc";
|
||||
import { fileStorageService } from "@/services/fileStorage/fileStorageService";
|
||||
|
||||
export const courseItemRouter = router({
|
||||
getAssignment: publicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
courseName: z.string(),
|
||||
moduleName: z.string(),
|
||||
assignmentName: z.string(),
|
||||
})
|
||||
)
|
||||
.query(async ({ input: { courseName, moduleName, assignmentName } }) => {
|
||||
return await fileStorageService.assignments.getAssignment(
|
||||
courseName,
|
||||
moduleName,
|
||||
assignmentName
|
||||
);
|
||||
}),
|
||||
getAllAssignments: publicProcedure
|
||||
.input(
|
||||
z.object({
|
||||
courseName: z.string(),
|
||||
moduleName: z.string(),
|
||||
})
|
||||
)
|
||||
.query(async ({ input: { courseName, moduleName } }) => {
|
||||
return await fileStorageService.assignments.getAssignments(
|
||||
courseName,
|
||||
moduleName
|
||||
);
|
||||
}),
|
||||
});
|
||||
13
nextjs/src/services/trpc/trpc.ts
Normal file
13
nextjs/src/services/trpc/trpc.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { initTRPC } from "@trpc/server";
|
||||
import superjson from 'superjson';
|
||||
|
||||
const t = initTRPC.create({
|
||||
transformer: superjson,
|
||||
});
|
||||
|
||||
export const middleware = t.middleware;
|
||||
export const createCallerFactory = t.createCallerFactory;
|
||||
export const mergeRouters = t.mergeRouters;
|
||||
|
||||
export const router = t.router;
|
||||
export const procedure = t.procedure;
|
||||
4
nextjs/src/services/trpc/utils.ts
Normal file
4
nextjs/src/services/trpc/utils.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { createTRPCReact } from "@trpc/react-query";
|
||||
import { AppRouter } from "./router/app";
|
||||
|
||||
export const trpc = createTRPCReact<AppRouter>();
|
||||
Reference in New Issue
Block a user