@@ -50,6 +50,24 @@ export async function generate(model: Model, options: PluginOptions, dmmf: DMMF.
50
50
outDir = resolvePath ( outDir , options ) ;
51
51
ensureEmptyDir ( outDir ) ;
52
52
53
+ const version = typeof options . version === 'string' ? options . version : 'v10' ;
54
+ if ( ! [ 'v10' , 'v11' ] . includes ( version ) ) {
55
+ throw new PluginError ( name , `Unsupported tRPC version "${ version } ". Use "v10" (default) or "v11".` ) ;
56
+ }
57
+
58
+ if ( version === 'v11' ) {
59
+ // v11 require options for importing `createTRPCRouter` and `procedure`
60
+ const importCreateRouter = options . importCreateRouter as string ;
61
+ if ( ! importCreateRouter ) {
62
+ throw new PluginError ( name , `Option "importCreateRouter" is required for tRPC v11` ) ;
63
+ }
64
+
65
+ const importProcedure = options . importProcedure as string ;
66
+ if ( ! importProcedure ) {
67
+ throw new PluginError ( name , `Option "importProcedure" is required for tRPC v11` ) ;
68
+ }
69
+ }
70
+
53
71
const prismaClientDmmf = dmmf ;
54
72
55
73
let modelOperations = prismaClientDmmf . mappings . modelOperations ;
@@ -71,8 +89,10 @@ export async function generate(model: Model, options: PluginOptions, dmmf: DMMF.
71
89
generateClientHelpers ,
72
90
model ,
73
91
zodSchemasImport ,
74
- options
92
+ options ,
93
+ version
75
94
) ;
95
+
76
96
createHelper ( outDir ) ;
77
97
78
98
await saveProject ( project ) ;
@@ -86,7 +106,8 @@ function createAppRouter(
86
106
generateClientHelpers : string [ ] | undefined ,
87
107
zmodel : Model ,
88
108
zodSchemasImport : string ,
89
- options : PluginOptions
109
+ options : PluginOptions ,
110
+ version : string
90
111
) {
91
112
const indexFile = path . resolve ( outDir , 'routers' , `index.ts` ) ;
92
113
const appRouter = project . createSourceFile ( indexFile , undefined , {
@@ -96,31 +117,45 @@ function createAppRouter(
96
117
appRouter . addStatements ( '/* eslint-disable */' ) ;
97
118
98
119
const prismaImport = getPrismaClientImportSpec ( path . dirname ( indexFile ) , options ) ;
120
+
121
+ if ( version === 'v10' ) {
122
+ appRouter . addImportDeclarations ( [
123
+ {
124
+ namedImports : [
125
+ 'unsetMarker' ,
126
+ 'AnyRouter' ,
127
+ 'AnyRootConfig' ,
128
+ 'CreateRouterInner' ,
129
+ 'Procedure' ,
130
+ 'ProcedureBuilder' ,
131
+ 'ProcedureParams' ,
132
+ 'ProcedureRouterRecord' ,
133
+ 'ProcedureType' ,
134
+ ] ,
135
+ isTypeOnly : true ,
136
+ moduleSpecifier : '@trpc/server' ,
137
+ } ,
138
+ ] ) ;
139
+ } else {
140
+ appRouter . addImportDeclarations ( [
141
+ {
142
+ namedImports : [ 'AnyTRPCRouter as AnyRouter' ] ,
143
+ isTypeOnly : true ,
144
+ moduleSpecifier : '@trpc/server' ,
145
+ } ,
146
+ ] ) ;
147
+ }
148
+
99
149
appRouter . addImportDeclarations ( [
100
- {
101
- namedImports : [
102
- 'unsetMarker' ,
103
- 'AnyRouter' ,
104
- 'AnyRootConfig' ,
105
- 'CreateRouterInner' ,
106
- 'Procedure' ,
107
- 'ProcedureBuilder' ,
108
- 'ProcedureParams' ,
109
- 'ProcedureRouterRecord' ,
110
- 'ProcedureType' ,
111
- ] ,
112
- isTypeOnly : true ,
113
- moduleSpecifier : '@trpc/server' ,
114
- } ,
115
150
{
116
151
namedImports : [ 'PrismaClient' ] ,
117
152
isTypeOnly : true ,
118
153
moduleSpecifier : prismaImport ,
119
154
} ,
120
155
] ) ;
121
156
122
- appRouter . addStatements ( `
123
-
157
+ if ( version === 'v10' ) {
158
+ appRouter . addStatements ( `
124
159
export type BaseConfig = AnyRootConfig;
125
160
126
161
export type RouterFactory<Config extends BaseConfig> = <
@@ -133,30 +168,40 @@ function createAppRouter(
133
168
134
169
export type ProcBuilder<Config extends BaseConfig> = ProcedureBuilder<
135
170
ProcedureParams<Config, any, any, any, UnsetMarker, UnsetMarker, any>
136
- >;
171
+ >;
172
+ ` ) ;
173
+ } else {
174
+ appRouter . addImportDeclaration ( {
175
+ namedImports : [ 'createTRPCRouter' ] ,
176
+ moduleSpecifier : options . importCreateRouter as string ,
177
+ } ) ;
178
+ }
137
179
180
+ appRouter . addStatements ( `
138
181
export function db(ctx: any) {
139
182
if (!ctx.prisma) {
140
183
throw new Error('Missing "prisma" field in trpc context');
141
184
}
142
185
return ctx.prisma as PrismaClient;
143
186
}
144
-
145
187
` ) ;
146
188
147
189
const filteredModelOperations = modelOperations . filter ( ( mo ) => ! hiddenModels . includes ( mo . model ) ) ;
148
190
149
191
appRouter
150
192
. addFunction ( {
151
- name : 'createRouter<Config extends BaseConfig>' ,
152
- parameters : [
153
- { name : 'router' , type : 'RouterFactory<Config>' } ,
154
- { name : 'procedure' , type : 'ProcBuilder<Config>' } ,
155
- ] ,
193
+ name : version === 'v10' ? 'createRouter<Config extends BaseConfig>' : 'createRouter' ,
194
+ parameters :
195
+ version === 'v10'
196
+ ? [
197
+ { name : 'router' , type : 'RouterFactory<Config>' } ,
198
+ { name : 'procedure' , type : 'ProcBuilder<Config>' } ,
199
+ ]
200
+ : [ ] ,
156
201
isExported : true ,
157
202
} )
158
203
. setBodyText ( ( writer ) => {
159
- writer . write ( ' return router(' ) ;
204
+ writer . write ( ` return ${ version === 'v10' ? ' router' : 'createTRPCRouter' } (` ) ;
160
205
writer . block ( ( ) => {
161
206
for ( const modelOperation of filteredModelOperations ) {
162
207
const { model, ...operations } = modelOperation ;
@@ -173,15 +218,20 @@ function createAppRouter(
173
218
generateClientHelpers ,
174
219
zodSchemasImport ,
175
220
options ,
176
- zmodel
221
+ zmodel ,
222
+ version
177
223
) ;
178
224
179
225
appRouter . addImportDeclaration ( {
180
226
defaultImport : `create${ model } Router` ,
181
227
moduleSpecifier : `./${ model } .router` ,
182
228
} ) ;
183
229
184
- writer . writeLine ( `${ lowerCaseFirst ( model ) } : create${ model } Router(router, procedure),` ) ;
230
+ if ( version === 'v10' ) {
231
+ writer . writeLine ( `${ lowerCaseFirst ( model ) } : create${ model } Router(router, procedure),` ) ;
232
+ } else {
233
+ writer . writeLine ( `${ lowerCaseFirst ( model ) } : create${ model } Router(),` ) ;
234
+ }
185
235
}
186
236
} ) ;
187
237
writer . write ( ');' ) ;
@@ -204,30 +254,30 @@ function createAppRouter(
204
254
} ) ,
205
255
} ) ;
206
256
207
- createClientHelpers ( outDir , generateClientHelpers ) ;
257
+ createClientHelpers ( outDir , generateClientHelpers , version ) ;
208
258
}
209
259
210
260
appRouter . formatText ( ) ;
211
261
}
212
262
213
- function createClientHelpers ( outputDir : string , generateClientHelpers : string [ ] ) {
263
+ function createClientHelpers ( outputDir : string , generateClientHelpers : string [ ] , version : string ) {
214
264
const utils = project . createSourceFile ( path . resolve ( outputDir , 'client' , `utils.ts` ) , undefined , {
215
265
overwrite : true ,
216
266
} ) ;
217
- utils . replaceWithText ( fs . readFileSync ( path . join ( __dirname , ' ./res/client/utils.ts' ) , 'utf-8' ) ) ;
267
+ utils . replaceWithText ( fs . readFileSync ( path . join ( __dirname , ` ./res/client/${ version } / utils.ts` ) , 'utf-8' ) ) ;
218
268
219
269
for ( const client of generateClientHelpers ) {
220
270
switch ( client ) {
221
271
case 'react' : {
222
- const content = fs . readFileSync ( path . join ( __dirname , ' ./res/client/react.ts' ) , 'utf-8' ) ;
272
+ const content = fs . readFileSync ( path . join ( __dirname , ` ./res/client/${ version } / react.ts` ) , 'utf-8' ) ;
223
273
project . createSourceFile ( path . resolve ( outputDir , 'client' , 'react.ts' ) , content , {
224
274
overwrite : true ,
225
275
} ) ;
226
276
break ;
227
277
}
228
278
229
279
case 'next' : {
230
- const content = fs . readFileSync ( path . join ( __dirname , ' ./res/client/next.ts' ) , 'utf-8' ) ;
280
+ const content = fs . readFileSync ( path . join ( __dirname , ` ./res/client/${ version } / next.ts` ) , 'utf-8' ) ;
231
281
project . createSourceFile ( path . resolve ( outputDir , 'client' , 'next.ts' ) , content , { overwrite : true } ) ;
232
282
break ;
233
283
}
@@ -244,36 +294,72 @@ function generateModelCreateRouter(
244
294
generateClientHelpers : string [ ] | undefined ,
245
295
zodSchemasImport : string ,
246
296
options : PluginOptions ,
247
- zmodel : Model
297
+ zmodel : Model ,
298
+ version : string
248
299
) {
249
300
const modelRouter = project . createSourceFile ( path . resolve ( outputDir , 'routers' , `${ model } .router.ts` ) , undefined , {
250
301
overwrite : true ,
251
302
} ) ;
252
303
253
304
modelRouter . addStatements ( '/* eslint-disable */' ) ;
254
305
255
- modelRouter . addImportDeclarations ( [
256
- {
257
- namedImports : [ 'type RouterFactory' , 'type ProcBuilder' , 'type BaseConfig' , 'db' ] ,
258
- moduleSpecifier : '.' ,
259
- } ,
260
- ] ) ;
306
+ if ( version === 'v10' ) {
307
+ modelRouter . addImportDeclarations ( [
308
+ {
309
+ namedImports : [ 'type RouterFactory' , 'type ProcBuilder' , 'type BaseConfig' , 'db' ] ,
310
+ moduleSpecifier : '.' ,
311
+ } ,
312
+ ] ) ;
313
+ } else {
314
+ modelRouter . addImportDeclarations ( [
315
+ {
316
+ namedImports : [ 'db' ] ,
317
+ moduleSpecifier : '.' ,
318
+ } ,
319
+ ] ) ;
261
320
321
+ modelRouter . addImportDeclarations ( [
322
+ {
323
+ namedImports : [ 'createTRPCRouter' ] ,
324
+ moduleSpecifier : options . importCreateRouter as string ,
325
+ } ,
326
+ ] ) ;
327
+
328
+ modelRouter . addImportDeclarations ( [
329
+ {
330
+ namedImports : [ 'procedure' ] ,
331
+ moduleSpecifier : options . importProcedure as string ,
332
+ } ,
333
+ ] ) ;
334
+ }
335
+
336
+ // zod schema import
262
337
generateRouterSchemaImport ( modelRouter , zodSchemasImport ) ;
338
+
339
+ // runtime helpers
263
340
generateHelperImport ( modelRouter ) ;
341
+
342
+ // client helper imports
264
343
if ( generateClientHelpers ) {
265
- generateRouterTypingImports ( modelRouter , options ) ;
344
+ generateRouterTypingImports ( modelRouter , options , version ) ;
266
345
}
267
346
268
- const createRouterFunc = modelRouter . addFunction ( {
269
- name : 'createRouter<Config extends BaseConfig>' ,
270
- parameters : [
271
- { name : 'router' , type : 'RouterFactory<Config>' } ,
272
- { name : 'procedure' , type : 'ProcBuilder<Config>' } ,
273
- ] ,
274
- isExported : true ,
275
- isDefaultExport : true ,
276
- } ) ;
347
+ const createRouterFunc =
348
+ version === 'v10'
349
+ ? modelRouter . addFunction ( {
350
+ name : 'createRouter<Config extends BaseConfig>' ,
351
+ parameters : [
352
+ { name : 'router' , type : 'RouterFactory<Config>' } ,
353
+ { name : 'procedure' , type : 'ProcBuilder<Config>' } ,
354
+ ] ,
355
+ isExported : true ,
356
+ isDefaultExport : true ,
357
+ } )
358
+ : modelRouter . addFunction ( {
359
+ name : 'createRouter' ,
360
+ isExported : true ,
361
+ isDefaultExport : true ,
362
+ } ) ;
277
363
278
364
let routerTypingStructure : InterfaceDeclarationStructure | undefined = undefined ;
279
365
if ( generateClientHelpers ) {
@@ -294,7 +380,7 @@ function generateModelCreateRouter(
294
380
}
295
381
296
382
createRouterFunc . setBodyText ( ( funcWriter ) => {
297
- funcWriter . write ( ' return router(' ) ;
383
+ funcWriter . write ( ` return ${ version === 'v10' ? ' router' : 'createTRPCRouter' } (` ) ;
298
384
funcWriter . block ( ( ) => {
299
385
for ( const [ opType , opNameWithModel ] of Object . entries ( operations ) ) {
300
386
if ( isDelegateModel ( dataModel ) && ( opType . startsWith ( 'create' ) || opType . startsWith ( 'upsert' ) ) ) {
@@ -322,7 +408,7 @@ function generateModelCreateRouter(
322
408
kind : StructureKind . PropertySignature ,
323
409
name : generateOpName ,
324
410
type : ( writer ) => {
325
- generateRouterTyping ( writer , generateOpName , model , baseOpType ) ;
411
+ generateRouterTyping ( writer , generateOpName , model , baseOpType , version ) ;
326
412
} ,
327
413
} ) ;
328
414
}
0 commit comments