From aaccac071f8e08b6fdd4e50b72a0926d9d156476 Mon Sep 17 00:00:00 2001 From: maany Date: Sun, 4 Aug 2024 03:19:48 +0200 Subject: [PATCH] clean trpc #7 --- .../[rc_id]/conversations/[conv_id]/page.tsx | 2 +- src/app/[rc_id]/conversations/page.tsx | 2 +- src/app/[rc_id]/sources/page.tsx | 2 +- src/app/api/trpc/[trpc]/route.ts | 4 +-- src/app/page.tsx | 2 +- src/app/sources/page.tsx | 2 +- src/lib/infrastructure/client/trpc/react.tsx | 2 +- .../auth/next-auth-credentials-provider.ts | 2 +- .../infrastructure/server/config/trpc/root.ts | 26 ------------------- .../infrastructure/server/trpc/app-router.ts | 26 +++++++++++++++++++ .../server/{config => }/trpc/context.ts | 2 +- .../routers}/conversations.ts | 2 +- .../routers}/health-check.ts | 2 +- .../routers}/messages.ts | 2 +- .../{trpc-routers => trpc/routers}/post.ts | 7 +---- .../routers}/research-contexts.ts | 6 ++--- .../routers}/source-data.ts | 2 +- .../server.ts => server/trpc/server-api.ts} | 6 ++--- .../{trpc/trpc.ts => server/trpc/server.ts} | 4 +-- 19 files changed, 49 insertions(+), 54 deletions(-) delete mode 100644 src/lib/infrastructure/server/config/trpc/root.ts create mode 100644 src/lib/infrastructure/server/trpc/app-router.ts rename src/lib/infrastructure/server/{config => }/trpc/context.ts (90%) rename src/lib/infrastructure/server/{trpc-routers => trpc/routers}/conversations.ts (97%) rename src/lib/infrastructure/server/{trpc-routers => trpc/routers}/health-check.ts (87%) rename src/lib/infrastructure/server/{trpc-routers => trpc/routers}/messages.ts (98%) rename src/lib/infrastructure/server/{trpc-routers => trpc/routers}/post.ts (87%) rename src/lib/infrastructure/server/{trpc-routers => trpc/routers}/research-contexts.ts (89%) rename src/lib/infrastructure/server/{trpc-routers => trpc/routers}/source-data.ts (98%) rename src/lib/infrastructure/{trpc/server.ts => server/trpc/server-api.ts} (76%) rename src/lib/infrastructure/{trpc/trpc.ts => server/trpc/server.ts} (94%) diff --git a/src/app/[rc_id]/conversations/[conv_id]/page.tsx b/src/app/[rc_id]/conversations/[conv_id]/page.tsx index 4750854..0142847 100644 --- a/src/app/[rc_id]/conversations/[conv_id]/page.tsx +++ b/src/app/[rc_id]/conversations/[conv_id]/page.tsx @@ -2,7 +2,7 @@ import { env } from "~/env"; import { redirect } from "next/navigation"; import { DummySendMessage } from "~/app/_components/dummy-send-message"; -import { api } from "~/lib/infrastructure/trpc/server"; +import { api } from "~/lib/infrastructure/server/trpc/server-api"; import type AuthGatewayOutputPort from "~/lib/core/ports/secondary/auth-gateway-output-port"; import serverContainer from "~/lib/infrastructure/server/config/ioc/server-container"; import { GATEWAYS } from "~/lib/infrastructure/server/config/ioc/server-ioc-symbols"; diff --git a/src/app/[rc_id]/conversations/page.tsx b/src/app/[rc_id]/conversations/page.tsx index 86cd501..6da3c1e 100644 --- a/src/app/[rc_id]/conversations/page.tsx +++ b/src/app/[rc_id]/conversations/page.tsx @@ -1,7 +1,7 @@ import { env } from "~/env"; import { redirect } from "next/navigation"; import { ListConversationsPage } from "../../_components/list-conversations"; -import { api } from "~/lib/infrastructure/trpc/server"; +import { api } from "~/lib/infrastructure/server/trpc/server-api"; import type AuthGatewayOutputPort from "~/lib/core/ports/secondary/auth-gateway-output-port"; import serverContainer from "~/lib/infrastructure/server/config/ioc/server-container"; import { GATEWAYS } from "~/lib/infrastructure/server/config/ioc/server-ioc-symbols"; diff --git a/src/app/[rc_id]/sources/page.tsx b/src/app/[rc_id]/sources/page.tsx index 6e88a4a..c97e976 100644 --- a/src/app/[rc_id]/sources/page.tsx +++ b/src/app/[rc_id]/sources/page.tsx @@ -1,6 +1,6 @@ import { env } from "~/env"; import { redirect } from "next/navigation"; -import { api } from "~/lib/infrastructure/trpc/server"; +import { api } from "~/lib/infrastructure/server/trpc/server-api"; import type AuthGatewayOutputPort from "~/lib/core/ports/secondary/auth-gateway-output-port"; import serverContainer from "~/lib/infrastructure/server/config/ioc/server-container"; import { GATEWAYS } from "~/lib/infrastructure/server/config/ioc/server-ioc-symbols"; diff --git a/src/app/api/trpc/[trpc]/route.ts b/src/app/api/trpc/[trpc]/route.ts index 7910bf8..ed27bcf 100644 --- a/src/app/api/trpc/[trpc]/route.ts +++ b/src/app/api/trpc/[trpc]/route.ts @@ -2,8 +2,8 @@ import { fetchRequestHandler } from "@trpc/server/adapters/fetch"; import { type NextRequest } from "next/server"; import { env } from "~/env"; -import { appRouter } from "~/lib/infrastructure/server/config/trpc/root"; -import { createTRPCContext } from "~/lib/infrastructure/trpc/trpc"; +import { appRouter } from "~/lib/infrastructure/server/trpc/app-router"; +import { createTRPCContext } from "~/lib/infrastructure/server/trpc/server"; /** * This wraps the `createTRPCContext` helper and provides the required context for the tRPC API when diff --git a/src/app/page.tsx b/src/app/page.tsx index aead727..d78c58a 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -2,10 +2,10 @@ import { env } from "~/env"; import { redirect } from "next/navigation"; import { ListResearchContextsPage } from "./_components/list-research-contexts"; import type { ResearchContext } from "@maany_shr/kernel-planckster-sdk-ts"; -import { api } from "~/lib/infrastructure/trpc/server"; import serverContainer from "~/lib/infrastructure/server/config/ioc/server-container"; import type AuthGatewayOutputPort from "~/lib/core/ports/secondary/auth-gateway-output-port"; import { GATEWAYS } from "~/lib/infrastructure/server/config/ioc/server-ioc-symbols"; +import { api } from "~/lib/infrastructure/server/trpc/server-api"; export default async function Home() { const authGateway = serverContainer.get(GATEWAYS.AUTH_GATEWAY); const session = await authGateway.getSession(); diff --git a/src/app/sources/page.tsx b/src/app/sources/page.tsx index e14bf1b..680acb6 100644 --- a/src/app/sources/page.tsx +++ b/src/app/sources/page.tsx @@ -2,7 +2,7 @@ import { env } from "~/env"; import { redirect } from "next/navigation"; import { DummyUploadComponent } from "../_components/dummy-upload"; import { DummyDownloadComponent } from "../_components/dummy-download"; -import { api } from "~/lib/infrastructure/trpc/server"; +import { api } from "~/lib/infrastructure/server/trpc/server-api"; import type AuthGatewayOutputPort from "~/lib/core/ports/secondary/auth-gateway-output-port"; import serverContainer from "~/lib/infrastructure/server/config/ioc/server-container"; import { GATEWAYS } from "~/lib/infrastructure/server/config/ioc/server-ioc-symbols"; diff --git a/src/lib/infrastructure/client/trpc/react.tsx b/src/lib/infrastructure/client/trpc/react.tsx index 55a54b7..7efd675 100644 --- a/src/lib/infrastructure/client/trpc/react.tsx +++ b/src/lib/infrastructure/client/trpc/react.tsx @@ -8,7 +8,7 @@ import { loggerLink, unstable_httpBatchStreamLink } from "@trpc/client"; import { createTRPCReact } from "@trpc/react-query"; import { useState } from "react"; import SuperJSON from "superjson"; -import type { AppRouter } from "~/lib/infrastructure/server/config/trpc/root"; +import type { AppRouter } from "~/lib/infrastructure/server/trpc/app-router"; const createQueryClient = () => new QueryClient(); diff --git a/src/lib/infrastructure/server/config/auth/next-auth-credentials-provider.ts b/src/lib/infrastructure/server/config/auth/next-auth-credentials-provider.ts index b0e4312..ff2a525 100644 --- a/src/lib/infrastructure/server/config/auth/next-auth-credentials-provider.ts +++ b/src/lib/infrastructure/server/config/auth/next-auth-credentials-provider.ts @@ -22,7 +22,7 @@ export default class NextAuthCredentialsProvider { id: env.KP_CLIENT_ID.toString() || "1", name: username, email: "planckster-example@mpi-sws.org", - image: "https://cdn-icons-png.freepik.com/512/3177/3177440.png", // TODO: change this to the actual image + image: "https://api.multiavatar.com/user.svg", // TODO: change this to the actual image kp: { client_id: env.KP_CLIENT_ID || 1, auth_token: env.KP_AUTH_TOKEN diff --git a/src/lib/infrastructure/server/config/trpc/root.ts b/src/lib/infrastructure/server/config/trpc/root.ts deleted file mode 100644 index 9d3778a..0000000 --- a/src/lib/infrastructure/server/config/trpc/root.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { postRouter } from "~/lib/infrastructure/server/trpc-routers/post"; -import { researchContextRouter } from "../../trpc-routers/research-contexts"; -import { conversationRouter } from "../../trpc-routers/conversations"; -import { messageRouter } from "../../trpc-routers/messages"; -import { sourceDataRouter } from "../../trpc-routers/source-data"; -import { kernelPlancksterHealthCheckRouter } from "../../trpc-routers/health-check"; -import { createTRPCRouter } from "../../../trpc/trpc"; - - -/** - * This is the primary router for your server. - * - * All routers added in /api/routers should be manually added here. - */ -export const appRouter = createTRPCRouter({ - post: postRouter, - researchContext: researchContextRouter, - conversation: conversationRouter, - message: messageRouter, - sourceData: sourceDataRouter, - healthCheck: kernelPlancksterHealthCheckRouter, -}); - -// export type definition of API -export type AppRouter = typeof appRouter; - diff --git a/src/lib/infrastructure/server/trpc/app-router.ts b/src/lib/infrastructure/server/trpc/app-router.ts new file mode 100644 index 0000000..a85ee45 --- /dev/null +++ b/src/lib/infrastructure/server/trpc/app-router.ts @@ -0,0 +1,26 @@ +import { postRouter } from "~/lib/infrastructure/server/trpc/routers/post"; +import { researchContextRouter } from "~/lib/infrastructure/server/trpc/routers/research-contexts"; +import { conversationRouter } from "~/lib/infrastructure/server/trpc/routers/conversations"; +import { messageRouter } from "~/lib/infrastructure/server/trpc/routers/messages"; +import { sourceDataRouter } from "~/lib/infrastructure/server/trpc/routers/source-data"; +import { kernelPlancksterHealthCheckRouter } from "./routers/health-check"; +import { createTRPCRouter } from "~/lib/infrastructure/server/trpc/server"; + + +/** + * This is the primary router for your server. + * + * All routers added in /api/routers should be manually added here. + */ +export const appRouter = createTRPCRouter({ + post: postRouter, + researchContext: researchContextRouter, + conversation: conversationRouter, + message: messageRouter, + sourceData: sourceDataRouter, + healthCheck: kernelPlancksterHealthCheckRouter, +}); + +// export type definition of API +export type AppRouter = typeof appRouter; + diff --git a/src/lib/infrastructure/server/config/trpc/context.ts b/src/lib/infrastructure/server/trpc/context.ts similarity index 90% rename from src/lib/infrastructure/server/config/trpc/context.ts rename to src/lib/infrastructure/server/trpc/context.ts index f62c651..b3189a7 100644 --- a/src/lib/infrastructure/server/config/trpc/context.ts +++ b/src/lib/infrastructure/server/trpc/context.ts @@ -1,6 +1,6 @@ import { headers } from "next/headers"; import { cache } from "react"; -import { createTRPCContext } from "~/lib/infrastructure/trpc/trpc"; +import { createTRPCContext } from "~/lib/infrastructure/server/trpc/server"; import { OpenAPI as KERNEL_PLANCKSTER_CONFIG } from "@maany_shr/kernel-planckster-sdk-ts"; import { env } from "~/env"; diff --git a/src/lib/infrastructure/server/trpc-routers/conversations.ts b/src/lib/infrastructure/server/trpc/routers/conversations.ts similarity index 97% rename from src/lib/infrastructure/server/trpc-routers/conversations.ts rename to src/lib/infrastructure/server/trpc/routers/conversations.ts index c6b8e11..d847bc8 100644 --- a/src/lib/infrastructure/server/trpc-routers/conversations.ts +++ b/src/lib/infrastructure/server/trpc/routers/conversations.ts @@ -1,9 +1,9 @@ import { z } from "zod"; -import { createTRPCRouter, protectedProcedure } from "~/lib/infrastructure/trpc/trpc"; import { ClientService as sdk } from "@maany_shr/kernel-planckster-sdk-ts"; import { env } from "~/env"; +import { createTRPCRouter, protectedProcedure } from "~/lib/infrastructure/server/trpc/server"; export const conversationRouter = createTRPCRouter({ list: protectedProcedure diff --git a/src/lib/infrastructure/server/trpc-routers/health-check.ts b/src/lib/infrastructure/server/trpc/routers/health-check.ts similarity index 87% rename from src/lib/infrastructure/server/trpc-routers/health-check.ts rename to src/lib/infrastructure/server/trpc/routers/health-check.ts index b647bb1..163b5ee 100644 --- a/src/lib/infrastructure/server/trpc-routers/health-check.ts +++ b/src/lib/infrastructure/server/trpc/routers/health-check.ts @@ -1,7 +1,7 @@ import { z } from 'zod'; -import { createTRPCRouter, publicProcedure } from '../../trpc/trpc'; import { HealthCheckService as sdk } from '@maany_shr/kernel-planckster-sdk-ts'; +import { createTRPCRouter, publicProcedure } from '../server'; export const kernelPlancksterHealthCheckRouter = createTRPCRouter({ ping: publicProcedure diff --git a/src/lib/infrastructure/server/trpc-routers/messages.ts b/src/lib/infrastructure/server/trpc/routers/messages.ts similarity index 98% rename from src/lib/infrastructure/server/trpc-routers/messages.ts rename to src/lib/infrastructure/server/trpc/routers/messages.ts index 9650f16..b09964c 100644 --- a/src/lib/infrastructure/server/trpc-routers/messages.ts +++ b/src/lib/infrastructure/server/trpc/routers/messages.ts @@ -1,10 +1,10 @@ import { z } from "zod"; -import { createTRPCRouter, protectedProcedure } from "~/lib/infrastructure/trpc/trpc"; import { ClientService as sdk } from "@maany_shr/kernel-planckster-sdk-ts"; import { env } from "~/env"; import OpenAIGateway from "~/lib/infrastructure/server/gateway/openai-gateway"; +import { createTRPCRouter, protectedProcedure } from "../server"; export const messageRouter = createTRPCRouter({ list: protectedProcedure diff --git a/src/lib/infrastructure/server/trpc-routers/post.ts b/src/lib/infrastructure/server/trpc/routers/post.ts similarity index 87% rename from src/lib/infrastructure/server/trpc-routers/post.ts rename to src/lib/infrastructure/server/trpc/routers/post.ts index 37624a2..067c7ed 100644 --- a/src/lib/infrastructure/server/trpc-routers/post.ts +++ b/src/lib/infrastructure/server/trpc/routers/post.ts @@ -1,10 +1,5 @@ import { z } from "zod"; - -import { - createTRPCRouter, - protectedProcedure, - publicProcedure, -} from "~/lib/infrastructure/trpc/trpc"; +import { createTRPCRouter, publicProcedure, protectedProcedure } from "../server"; let post = { id: 1, diff --git a/src/lib/infrastructure/server/trpc-routers/research-contexts.ts b/src/lib/infrastructure/server/trpc/routers/research-contexts.ts similarity index 89% rename from src/lib/infrastructure/server/trpc-routers/research-contexts.ts rename to src/lib/infrastructure/server/trpc/routers/research-contexts.ts index cb215dd..3b2eae8 100644 --- a/src/lib/infrastructure/server/trpc-routers/research-contexts.ts +++ b/src/lib/infrastructure/server/trpc/routers/research-contexts.ts @@ -1,13 +1,13 @@ import { z } from "zod"; -import { createTRPCRouter, protectedProcedure } from "~/lib/infrastructure/trpc/trpc"; import { ClientService as sdk } from "@maany_shr/kernel-planckster-sdk-ts"; import type { NewResearchContextViewModel } from "@maany_shr/kernel-planckster-sdk-ts"; import { env } from "~/env"; import type AuthGatewayOutputPort from "~/lib/core/ports/secondary/auth-gateway-output-port"; -import serverContainer from "../config/ioc/server-container"; -import { GATEWAYS } from "../config/ioc/server-ioc-symbols"; +import serverContainer from "../../config/ioc/server-container"; +import { GATEWAYS } from "../../config/ioc/server-ioc-symbols"; +import { createTRPCRouter, protectedProcedure } from "../server"; export const researchContextRouter = createTRPCRouter({ list: protectedProcedure diff --git a/src/lib/infrastructure/server/trpc-routers/source-data.ts b/src/lib/infrastructure/server/trpc/routers/source-data.ts similarity index 98% rename from src/lib/infrastructure/server/trpc-routers/source-data.ts rename to src/lib/infrastructure/server/trpc/routers/source-data.ts index 0d65d8b..acad7f3 100644 --- a/src/lib/infrastructure/server/trpc-routers/source-data.ts +++ b/src/lib/infrastructure/server/trpc/routers/source-data.ts @@ -1,12 +1,12 @@ import { z } from "zod"; -import { createTRPCRouter, protectedProcedure } from "~/lib/infrastructure/trpc/trpc"; import { ClientService as sdk } from "@maany_shr/kernel-planckster-sdk-ts"; import { downloadFile, uploadFile } from "~/lib/infrastructure/server/repository/file-repository"; import { env } from "~/env"; +import { createTRPCRouter, protectedProcedure } from "../server"; export const sourceDataRouter = createTRPCRouter({ listForClient: protectedProcedure diff --git a/src/lib/infrastructure/trpc/server.ts b/src/lib/infrastructure/server/trpc/server-api.ts similarity index 76% rename from src/lib/infrastructure/trpc/server.ts rename to src/lib/infrastructure/server/trpc/server-api.ts index 89a4e82..f31a331 100644 --- a/src/lib/infrastructure/trpc/server.ts +++ b/src/lib/infrastructure/server/trpc/server-api.ts @@ -2,9 +2,9 @@ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ import "server-only"; -import { createCallerFactory } from "./trpc"; -import { appRouter } from "../server/config/trpc/root"; -import createContext from "../server/config/trpc/context"; +import { appRouter } from "./app-router"; +import createContext from "./context"; +import { createCallerFactory } from "./server"; /** * Create a server-side caller for the tRPC API. diff --git a/src/lib/infrastructure/trpc/trpc.ts b/src/lib/infrastructure/server/trpc/server.ts similarity index 94% rename from src/lib/infrastructure/trpc/trpc.ts rename to src/lib/infrastructure/server/trpc/server.ts index 3072710..d1e45e2 100644 --- a/src/lib/infrastructure/trpc/trpc.ts +++ b/src/lib/infrastructure/server/trpc/server.ts @@ -10,8 +10,8 @@ import { initTRPC, TRPCError } from "@trpc/server"; import superjson from "superjson"; import { ZodError } from "zod"; import type AuthGatewayOutputPort from "~/lib/core/ports/secondary/auth-gateway-output-port"; -import serverContainer from "../server/config/ioc/server-container"; -import { GATEWAYS } from "../server/config/ioc/server-ioc-symbols"; +import serverContainer from "~/lib/infrastructure/server/config/ioc/server-container"; +import { GATEWAYS } from "~/lib/infrastructure/server/config/ioc/server-ioc-symbols"; /**