From dcfa0a873973dd7b77f2c3a628be4f798b171375 Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Thu, 22 Aug 2024 20:34:13 -0700 Subject: [PATCH] update --- packages/plugins/trpc/src/generator.ts | 13 ++--- packages/plugins/trpc/src/helpers.ts | 52 +++++++++--------- .../routers/generated/routers/Post.router.ts | 54 +++++++++++++++---- .../routers/generated/routers/User.router.ts | 54 +++++++++++++++---- .../api/routers/generated/routers/index.ts | 9 ++-- 5 files changed, 121 insertions(+), 61 deletions(-) diff --git a/packages/plugins/trpc/src/generator.ts b/packages/plugins/trpc/src/generator.ts index 55acfd7b1..b8ae9ec37 100644 --- a/packages/plugins/trpc/src/generator.ts +++ b/packages/plugins/trpc/src/generator.ts @@ -178,7 +178,6 @@ function createAppRouter( } appRouter.addStatements(` - export function db(ctx: any) { if (!ctx.prisma) { throw new Error('Missing "prisma" field in trpc context'); @@ -195,14 +194,14 @@ function createAppRouter( parameters: version === 'v10' ? [ - { name: 'createTRPCRouter', type: 'RouterFactory' }, + { name: 'router', type: 'RouterFactory' }, { name: 'procedure', type: 'ProcBuilder' }, ] : [], isExported: true, }) .setBodyText((writer) => { - writer.write('return createTRPCRouter('); + writer.write(`return ${version === 'v10' ? 'router' : 'createTRPCRouter'}(`); writer.block(() => { for (const modelOperation of filteredModelOperations) { const { model, ...operations } = modelOperation; @@ -229,9 +228,7 @@ function createAppRouter( }); if (version === 'v10') { - writer.writeLine( - `${lowerCaseFirst(model)}: create${model}Router(createTRPCRouter, procedure),` - ); + writer.writeLine(`${lowerCaseFirst(model)}: create${model}Router(router, procedure),`); } else { writer.writeLine(`${lowerCaseFirst(model)}: create${model}Router(),`); } @@ -352,7 +349,7 @@ function generateModelCreateRouter( ? modelRouter.addFunction({ name: 'createRouter', parameters: [ - { name: 'createTRPCRouter', type: 'RouterFactory' }, + { name: 'router', type: 'RouterFactory' }, { name: 'procedure', type: 'ProcBuilder' }, ], isExported: true, @@ -383,7 +380,7 @@ function generateModelCreateRouter( } createRouterFunc.setBodyText((funcWriter) => { - funcWriter.write('return createTRPCRouter('); + funcWriter.write(`return ${version === 'v10' ? 'router' : 'createTRPCRouter'}(`); funcWriter.block(() => { for (const [opType, opNameWithModel] of Object.entries(operations)) { if (isDelegateModel(dataModel) && (opType.startsWith('create') || opType.startsWith('upsert'))) { diff --git a/packages/plugins/trpc/src/helpers.ts b/packages/plugins/trpc/src/helpers.ts index f2a6dcac8..ca0ee55fc 100644 --- a/packages/plugins/trpc/src/helpers.ts +++ b/packages/plugins/trpc/src/helpers.ts @@ -197,49 +197,49 @@ export function generateRouterTyping( writer.block(() => { if (procType === 'query') { - const queryOptions = - version === 'v10' - ? `UseTRPCQueryOptions` - : `UseTRPCQueryOptions`; - - const infiniteQueryOptions = - version === 'v10' - ? `UseTRPCInfiniteQueryOptions` - : `UseTRPCInfiniteQueryOptions`; - - const infiniteQueryResult = - version === 'v10' - ? `UseTRPCInfiniteQueryResult<${resultType}, ${errorType}>` - : `UseTRPCInfiniteQueryResult<${resultType}, ${errorType}, T>`; - - writer.writeLine(` + if (version === 'v10') { + writer.writeLine(` useQuery: ( input: ${argsType}, - opts?: ${queryOptions} + opts?: UseTRPCQueryOptions ) => UseTRPCQueryResult< TData, ${errorType} >; useInfiniteQuery: ( input: Omit<${argsType}, 'cursor'>, - opts?: ${infiniteQueryOptions} - ) => ${infiniteQueryResult};`); - - if (version !== 'v10') { - // v11 uses tanstack-query v5 and supports suspense queries + opts?: UseTRPCInfiniteQueryOptions + ) => UseTRPCInfiniteQueryResult< + ${resultType}, + ${errorType} + >; + `); + } else { writer.writeLine(` - useSuspenseQuery: ( + useQuery: ( input: ${argsType}, - opts?: UseTRPCSuspenseQueryOptions<${resultType}, TData, Error> - ) => UseTRPCSuspenseQueryResult< + opts?: UseTRPCQueryOptions<${resultType}, TData, Error> + ) => UseTRPCQueryResult< TData, ${errorType} >; + useInfiniteQuery: ( + input: Omit<${argsType}, 'cursor'>, + opts?: UseTRPCInfiniteQueryOptions + ) => UseTRPCInfiniteQueryResult< + ${resultType}, + ${errorType}, + T + >; + useSuspenseQuery: ( + input: ${argsType}, + opts?: UseTRPCSuspenseQueryOptions<${resultType}, TData, Error> + ) => UseTRPCSuspenseQueryResult; useSuspenseInfiniteQuery: ( input: Omit<${argsType}, 'cursor'>, opts?: UseTRPCSuspenseInfiniteQueryOptions ) => UseTRPCSuspenseInfiniteQueryResult<${resultType}, ${errorType}, T>; - `); + `); } } else if (procType === 'mutation') { writer.writeLine(` diff --git a/packages/plugins/trpc/tests/projects/t3-trpc-v10/src/server/api/routers/generated/routers/Post.router.ts b/packages/plugins/trpc/tests/projects/t3-trpc-v10/src/server/api/routers/generated/routers/Post.router.ts index dd087c5de..3edfe9c94 100644 --- a/packages/plugins/trpc/tests/projects/t3-trpc-v10/src/server/api/routers/generated/routers/Post.router.ts +++ b/packages/plugins/trpc/tests/projects/t3-trpc-v10/src/server/api/routers/generated/routers/Post.router.ts @@ -8,8 +8,8 @@ import type { UseTRPCMutationOptions, UseTRPCMutationResult, UseTRPCQueryOptions import type { TRPCClientErrorLike } from '@trpc/client'; import type { AnyRouter } from '@trpc/server'; -export default function createRouter(createTRPCRouter: RouterFactory, procedure: ProcBuilder) { - return createTRPCRouter({ +export default function createRouter(router: RouterFactory, procedure: ProcBuilder) { + return router({ aggregate: procedure.input($Schema.PostInputSchema.aggregate).query(({ ctx, input }) => checkRead(db(ctx).post.aggregate(input as any))), @@ -58,7 +58,11 @@ export interface ClientType( input: Omit, 'cursor'>, opts?: UseTRPCInfiniteQueryOptions, Error> - ) => UseTRPCInfiniteQueryResult, TRPCClientErrorLike>; + ) => UseTRPCInfiniteQueryResult< + Prisma.GetPostAggregateType, + TRPCClientErrorLike + >; + }; createMany: { @@ -128,7 +132,11 @@ export interface ClientType( input: Omit, 'cursor'>, opts?: UseTRPCInfiniteQueryOptions, Error> - ) => UseTRPCInfiniteQueryResult, TRPCClientErrorLike>; + ) => UseTRPCInfiniteQueryResult< + Prisma.PostGetPayload, + TRPCClientErrorLike + >; + }; findFirstOrThrow: { @@ -142,7 +150,11 @@ export interface ClientType( input: Omit, 'cursor'>, opts?: UseTRPCInfiniteQueryOptions, Error> - ) => UseTRPCInfiniteQueryResult, TRPCClientErrorLike>; + ) => UseTRPCInfiniteQueryResult< + Prisma.PostGetPayload, + TRPCClientErrorLike + >; + }; findMany: { @@ -156,7 +168,11 @@ export interface ClientType( input: Omit, 'cursor'>, opts?: UseTRPCInfiniteQueryOptions>, Error> - ) => UseTRPCInfiniteQueryResult>, TRPCClientErrorLike>; + ) => UseTRPCInfiniteQueryResult< + Array>, + TRPCClientErrorLike + >; + }; findUnique: { @@ -170,7 +186,11 @@ export interface ClientType( input: Omit, 'cursor'>, opts?: UseTRPCInfiniteQueryOptions, Error> - ) => UseTRPCInfiniteQueryResult, TRPCClientErrorLike>; + ) => UseTRPCInfiniteQueryResult< + Prisma.PostGetPayload, + TRPCClientErrorLike + >; + }; findUniqueOrThrow: { @@ -184,7 +204,11 @@ export interface ClientType( input: Omit, 'cursor'>, opts?: UseTRPCInfiniteQueryOptions, Error> - ) => UseTRPCInfiniteQueryResult, TRPCClientErrorLike>; + ) => UseTRPCInfiniteQueryResult< + Prisma.PostGetPayload, + TRPCClientErrorLike + >; + }; groupBy: { @@ -310,7 +334,11 @@ export interface ClientType( input: Omit & InputErrors, 'cursor'>, opts?: UseTRPCInfiniteQueryOptions : InputErrors, Error> - ) => UseTRPCInfiniteQueryResult<{} extends InputErrors ? Prisma.GetPostGroupByPayload : InputErrors, TRPCClientErrorLike>; + ) => UseTRPCInfiniteQueryResult< + {} extends InputErrors ? Prisma.GetPostGroupByPayload : InputErrors, + TRPCClientErrorLike + >; + }; updateMany: { @@ -378,10 +406,14 @@ export interface ClientType : number, Error> - ) => UseTRPCInfiniteQueryResult<'select' extends keyof T + ) => UseTRPCInfiniteQueryResult< + 'select' extends keyof T ? T['select'] extends true ? number : Prisma.GetScalarType - : number, TRPCClientErrorLike>; + : number, + TRPCClientErrorLike + >; + }; } diff --git a/packages/plugins/trpc/tests/projects/t3-trpc-v10/src/server/api/routers/generated/routers/User.router.ts b/packages/plugins/trpc/tests/projects/t3-trpc-v10/src/server/api/routers/generated/routers/User.router.ts index d05935451..366ccfcb0 100644 --- a/packages/plugins/trpc/tests/projects/t3-trpc-v10/src/server/api/routers/generated/routers/User.router.ts +++ b/packages/plugins/trpc/tests/projects/t3-trpc-v10/src/server/api/routers/generated/routers/User.router.ts @@ -8,8 +8,8 @@ import type { UseTRPCMutationOptions, UseTRPCMutationResult, UseTRPCQueryOptions import type { TRPCClientErrorLike } from '@trpc/client'; import type { AnyRouter } from '@trpc/server'; -export default function createRouter(createTRPCRouter: RouterFactory, procedure: ProcBuilder) { - return createTRPCRouter({ +export default function createRouter(router: RouterFactory, procedure: ProcBuilder) { + return router({ aggregate: procedure.input($Schema.UserInputSchema.aggregate).query(({ ctx, input }) => checkRead(db(ctx).user.aggregate(input as any))), @@ -58,7 +58,11 @@ export interface ClientType( input: Omit, 'cursor'>, opts?: UseTRPCInfiniteQueryOptions, Error> - ) => UseTRPCInfiniteQueryResult, TRPCClientErrorLike>; + ) => UseTRPCInfiniteQueryResult< + Prisma.GetUserAggregateType, + TRPCClientErrorLike + >; + }; createMany: { @@ -128,7 +132,11 @@ export interface ClientType( input: Omit, 'cursor'>, opts?: UseTRPCInfiniteQueryOptions, Error> - ) => UseTRPCInfiniteQueryResult, TRPCClientErrorLike>; + ) => UseTRPCInfiniteQueryResult< + Prisma.UserGetPayload, + TRPCClientErrorLike + >; + }; findFirstOrThrow: { @@ -142,7 +150,11 @@ export interface ClientType( input: Omit, 'cursor'>, opts?: UseTRPCInfiniteQueryOptions, Error> - ) => UseTRPCInfiniteQueryResult, TRPCClientErrorLike>; + ) => UseTRPCInfiniteQueryResult< + Prisma.UserGetPayload, + TRPCClientErrorLike + >; + }; findMany: { @@ -156,7 +168,11 @@ export interface ClientType( input: Omit, 'cursor'>, opts?: UseTRPCInfiniteQueryOptions>, Error> - ) => UseTRPCInfiniteQueryResult>, TRPCClientErrorLike>; + ) => UseTRPCInfiniteQueryResult< + Array>, + TRPCClientErrorLike + >; + }; findUnique: { @@ -170,7 +186,11 @@ export interface ClientType( input: Omit, 'cursor'>, opts?: UseTRPCInfiniteQueryOptions, Error> - ) => UseTRPCInfiniteQueryResult, TRPCClientErrorLike>; + ) => UseTRPCInfiniteQueryResult< + Prisma.UserGetPayload, + TRPCClientErrorLike + >; + }; findUniqueOrThrow: { @@ -184,7 +204,11 @@ export interface ClientType( input: Omit, 'cursor'>, opts?: UseTRPCInfiniteQueryOptions, Error> - ) => UseTRPCInfiniteQueryResult, TRPCClientErrorLike>; + ) => UseTRPCInfiniteQueryResult< + Prisma.UserGetPayload, + TRPCClientErrorLike + >; + }; groupBy: { @@ -310,7 +334,11 @@ export interface ClientType( input: Omit & InputErrors, 'cursor'>, opts?: UseTRPCInfiniteQueryOptions : InputErrors, Error> - ) => UseTRPCInfiniteQueryResult<{} extends InputErrors ? Prisma.GetUserGroupByPayload : InputErrors, TRPCClientErrorLike>; + ) => UseTRPCInfiniteQueryResult< + {} extends InputErrors ? Prisma.GetUserGroupByPayload : InputErrors, + TRPCClientErrorLike + >; + }; updateMany: { @@ -378,10 +406,14 @@ export interface ClientType : number, Error> - ) => UseTRPCInfiniteQueryResult<'select' extends keyof T + ) => UseTRPCInfiniteQueryResult< + 'select' extends keyof T ? T['select'] extends true ? number : Prisma.GetScalarType - : number, TRPCClientErrorLike>; + : number, + TRPCClientErrorLike + >; + }; } diff --git a/packages/plugins/trpc/tests/projects/t3-trpc-v10/src/server/api/routers/generated/routers/index.ts b/packages/plugins/trpc/tests/projects/t3-trpc-v10/src/server/api/routers/generated/routers/index.ts index e3df84e9a..523f4b645 100644 --- a/packages/plugins/trpc/tests/projects/t3-trpc-v10/src/server/api/routers/generated/routers/index.ts +++ b/packages/plugins/trpc/tests/projects/t3-trpc-v10/src/server/api/routers/generated/routers/index.ts @@ -20,7 +20,6 @@ export type ProcBuilder = ProcedureBuilder< ProcedureParams >; - export function db(ctx: any) { if (!ctx.prisma) { throw new Error('Missing "prisma" field in trpc context'); @@ -28,10 +27,10 @@ export function db(ctx: any) { return ctx.prisma as PrismaClient; } -export function createRouter(createTRPCRouter: RouterFactory, procedure: ProcBuilder) { - return createTRPCRouter({ - user: createUserRouter(createTRPCRouter, procedure), - post: createPostRouter(createTRPCRouter, procedure), +export function createRouter(router: RouterFactory, procedure: ProcBuilder) { + return router({ + user: createUserRouter(router, procedure), + post: createPostRouter(router, procedure), } ); }