@@ -89,35 +89,85 @@ function createAppRouter(
89
89
const prismaImport = getPrismaClientImportSpec ( zmodel , path . dirname ( indexFile ) ) ;
90
90
appRouter . addImportDeclarations ( [
91
91
{
92
- namedImports : [ 'AnyRootConfig' ] ,
92
+ namedImports : [ 'type AnyRootConfig' , 'type Procedure' , 'type ProcedureParams' , 'type ProcedureType '] ,
93
93
moduleSpecifier : '@trpc/server' ,
94
94
} ,
95
95
{
96
- namedImports : [ 'PrismaClient' ] ,
96
+ namedImports : [ 'type PrismaClient' , 'type Prisma '] ,
97
97
moduleSpecifier : prismaImport ,
98
- isTypeOnly : true ,
99
98
} ,
100
99
{
101
- namedImports : [ 'createRouterFactory' , 'AnyRouter' ] ,
100
+ namedImports : [ 'type createRouterFactory' , 'AnyRouter' ] ,
102
101
moduleSpecifier : '@trpc/server/dist/core/router' ,
103
102
} ,
104
103
{
105
- namedImports : [ 'createBuilder ' ] ,
104
+ namedImports : [ 'type ProcedureBuilder ' ] ,
106
105
moduleSpecifier : '@trpc/server/dist/core/internals/procedureBuilder' ,
107
106
} ,
107
+ { defaultImport : 'z' , moduleSpecifier : 'zod' , isTypeOnly : true } ,
108
108
] ) ;
109
109
110
110
appRouter . addStatements ( `
111
+ ${ /** to be used by the other routers without making a bigger commit */ '' }
112
+ export { PrismaClient } from '${ prismaImport } ';
113
+
111
114
export type BaseConfig = AnyRootConfig;
112
115
113
116
export type RouterFactory<Config extends BaseConfig> = ReturnType<
114
117
typeof createRouterFactory<Config>
115
118
>;
116
119
117
- export type ProcBuilder<Config extends BaseConfig> = ReturnType<
118
- typeof createBuilder<Config>
119
- >;
120
+ ${
121
+ /** this is needed in order to prevent type errors between a procedure and a middleware-extended procedure */ ''
122
+ }
123
+ export type ProcBuilder<Config extends BaseConfig> = ProcedureBuilder<{
124
+ _config: Config;
125
+ _ctx_out: Config['$types']['ctx'];
126
+ _input_in: any;
127
+ _input_out: any;
128
+ _output_in: any;
129
+ _output_out: any;
130
+ _meta: Config['$types']['meta'];
131
+ }>;
132
+
133
+ type ExtractParamsFromProcBuilder<Builder extends ProcedureBuilder<any>> =
134
+ Builder extends ProcedureBuilder<infer P> ? P : never;
120
135
136
+ type FromPromise<P extends Promise<any>> = P extends Promise<infer T>
137
+ ? T
138
+ : never;
139
+
140
+ ${ /** workaround to avoid creating 'typeof unsetMarker & object' on the procedure output */ '' }
141
+ type Join<A, B> = A extends symbol ? B : A & B;
142
+
143
+ ${
144
+ /** you can name it whatever you want, but this is to make sure that
145
+ the types from the middleware and the procedure are correctly merged */ ''
146
+ }
147
+ export type ProcReturns<
148
+ PType extends ProcedureType,
149
+ PBuilder extends ProcBuilder<BaseConfig>,
150
+ ZType extends z.ZodType,
151
+ PPromise extends Prisma.PrismaPromise<any>
152
+ > = Procedure<
153
+ PType,
154
+ ProcedureParams<
155
+ ExtractParamsFromProcBuilder<PBuilder>["_config"],
156
+ ExtractParamsFromProcBuilder<PBuilder>["_ctx_out"],
157
+ Join<ExtractParamsFromProcBuilder<PBuilder>["_input_in"], z.infer<ZType>>,
158
+ Join<ExtractParamsFromProcBuilder<PBuilder>["_input_out"], z.infer<ZType>>,
159
+ Join<
160
+ ExtractParamsFromProcBuilder<PBuilder>["_output_in"],
161
+ FromPromise<PPromise>
162
+ >,
163
+ Join<
164
+ ExtractParamsFromProcBuilder<PBuilder>["_output_out"],
165
+ FromPromise<PPromise>
166
+ >,
167
+ ExtractParamsFromProcBuilder<PBuilder>["_meta"]
168
+ >
169
+ >;
170
+
121
171
export function db(ctx: any) {
122
172
if (!ctx.prisma) {
123
173
throw new Error('Missing "prisma" field in trpc context');
@@ -233,7 +283,14 @@ function generateModelCreateRouter(
233
283
234
284
modelRouter . addImportDeclarations ( [
235
285
{
236
- namedImports : [ 'type RouterFactory' , 'type ProcBuilder' , 'type BaseConfig' , 'db' ] ,
286
+ namedImports : [
287
+ 'type RouterFactory' ,
288
+ 'type ProcBuilder' ,
289
+ 'type BaseConfig' ,
290
+ 'type ProcReturns' ,
291
+ 'type PrismaClient' ,
292
+ 'db' ,
293
+ ] ,
237
294
moduleSpecifier : '.' ,
238
295
} ,
239
296
] ) ;
0 commit comments