From fb42dee78122d8f3f4078ced6b6c72ab8d35f3eb Mon Sep 17 00:00:00 2001 From: Sachin Raja Date: Tue, 1 Nov 2022 06:43:29 -0700 Subject: [PATCH] conditional rsc or trpc client --- .vscode/settings.json | 3 ++- @trpc/next-layout/client.ts | 32 +++++++++++++++++++++++ @trpc/next-layout/createTRPCNextLayout.ts | 14 +++++----- @trpc/next-layout/index.ts | 1 + @trpc/next-layout/package.json | 7 +++++ @trpc/next-layout/server.ts | 1 + app/layout.tsx | 18 ++++++------- client/CreatePostForm.tsx | 23 +++++++++------- next.config.js | 8 ++++-- package.json | 1 + pnpm-lock.yaml | 7 +++++ server-rsc/getUser.tsx | 2 +- server-rsc/trpc/client.ts | 4 +++ server-rsc/trpc/index.ts | 1 + server-rsc/trpc/package.json | 8 ++++++ server-rsc/{trpc.ts => trpc/server.ts} | 10 +++---- 16 files changed, 106 insertions(+), 34 deletions(-) create mode 100644 @trpc/next-layout/client.ts create mode 100644 @trpc/next-layout/package.json create mode 100644 @trpc/next-layout/server.ts create mode 100644 server-rsc/trpc/client.ts create mode 100644 server-rsc/trpc/index.ts create mode 100644 server-rsc/trpc/package.json rename server-rsc/{trpc.ts => trpc/server.ts} (51%) diff --git a/.vscode/settings.json b/.vscode/settings.json index c3d9d94..ac72b45 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,5 @@ { "typescript.tsdk": "./node_modules/typescript/lib", - "typescript.enablePromptUseWorkspaceTsdk": true + "typescript.enablePromptUseWorkspaceTsdk": true, + "prettier.requireConfig": false } \ No newline at end of file diff --git a/@trpc/next-layout/client.ts b/@trpc/next-layout/client.ts new file mode 100644 index 0000000..6f05662 --- /dev/null +++ b/@trpc/next-layout/client.ts @@ -0,0 +1,32 @@ +import { useQuery } from "@tanstack/react-query"; +import { createTRPCClient, httpBatchLink } from "@trpc/client"; +import { AnyRouter, ProcedureType } from "@trpc/server"; +import { createRecursiveProxy } from "@trpc/server/shared"; +import { createContext } from "react"; +import { DecoratedProcedureRecord } from "./createTRPCNextLayout"; +import superjson from "superjson"; +export type TRPCSchemaContext = Record; +export const TRPCSchemaContext = createContext(null as any); +export const TRPCSchemaContextProvider = TRPCSchemaContext.Provider; + +export async function createTRPCNextClientLayout< + TRouter extends AnyRouter +>(): Promise> { + const client = createTRPCClient({ + links: [httpBatchLink({ url: "/api/trpc" })], + transformer: superjson, + }); + + return createRecursiveProxy((callOpts) => { + const path = [...callOpts.path]; + path.pop(); + const flatPath = path.join("."); + // const schema = useContext(TRPCSchemaContext); + // const procedureType = schema[flatPath]; + + const { data } = useQuery([...path, callOpts.args[0]], () => { + return client.query(flatPath, callOpts.args[0]); + }); + return data; + }) as DecoratedProcedureRecord; +} diff --git a/@trpc/next-layout/createTRPCNextLayout.ts b/@trpc/next-layout/createTRPCNextLayout.ts index a71b8bd..fe4e264 100644 --- a/@trpc/next-layout/createTRPCNextLayout.ts +++ b/@trpc/next-layout/createTRPCNextLayout.ts @@ -11,7 +11,7 @@ import { import { createRecursiveProxy } from "@trpc/server/shared"; import { use } from "react"; -interface CreateTRPCNextLayoutOptions { +export interface CreateTRPCNextLayoutOptions { router: TRouter; createContext: () => MaybePromise>; } @@ -23,7 +23,7 @@ export type DecorateProcedure = TProcedure extends AnyQueryProcedure ? { use( - input: inferProcedureInput, + input: inferProcedureInput // FIXME: maybe this should be cache options? // opts?: ): inferProcedureOutput; @@ -41,7 +41,7 @@ type OmitNever = Pick< */ export type DecoratedProcedureRecord< TProcedures extends ProcedureRouterRecord, - TPath extends string = "", + TPath extends string = "" > = OmitNever<{ [TKey in keyof TProcedures]: TProcedures[TKey] extends AnyRouter ? DecoratedProcedureRecord< @@ -53,9 +53,9 @@ export type DecoratedProcedureRecord< : never; }>; -export function createTRPCNextLayout( - opts: CreateTRPCNextLayoutOptions, -): DecoratedProcedureRecord { +export async function createTRPCNextLayout( + opts: CreateTRPCNextLayoutOptions +): Promise> { return createRecursiveProxy((callOpts) => { const path = [...callOpts.path]; path.pop(); @@ -65,7 +65,7 @@ export function createTRPCNextLayout( const caller = opts.router.createCaller(ctx); return caller.query(path.join("."), callOpts.args[0]) as any; - })(), + })() ) as any; }) as DecoratedProcedureRecord; } diff --git a/@trpc/next-layout/index.ts b/@trpc/next-layout/index.ts index 61c958c..95ab9e7 100644 --- a/@trpc/next-layout/index.ts +++ b/@trpc/next-layout/index.ts @@ -1 +1,2 @@ export { createTRPCNextLayout } from "./createTRPCNextLayout"; + \ No newline at end of file diff --git a/@trpc/next-layout/package.json b/@trpc/next-layout/package.json new file mode 100644 index 0000000..a71b706 --- /dev/null +++ b/@trpc/next-layout/package.json @@ -0,0 +1,7 @@ +{ + "name": "@trpc/next-layout", + "exports": { + "./client": "./client.ts", + "./server": "./server.ts" + } +} diff --git a/@trpc/next-layout/server.ts b/@trpc/next-layout/server.ts new file mode 100644 index 0000000..43417c0 --- /dev/null +++ b/@trpc/next-layout/server.ts @@ -0,0 +1 @@ +export { createTRPCNextLayout as createTRPCNextServerLayout } from "./createTRPCNextLayout"; diff --git a/app/layout.tsx b/app/layout.tsx index 5623893..50f9bd9 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -12,24 +12,24 @@ export default function RootLayout(props: Props) { const user = rsc.whoami.use(); return ( - + Next.js hello -