prisma-generator-express 1.18.0 → 1.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/README.md +399 -194
  2. package/dist/bin.d.ts +2 -0
  3. package/dist/bin.js +1 -1
  4. package/dist/bin.js.map +1 -1
  5. package/dist/client/encodeQueryParams.d.ts +1 -0
  6. package/dist/client/encodeQueryParams.js +33 -0
  7. package/dist/client/encodeQueryParams.js.map +1 -0
  8. package/dist/constants.d.ts +1 -0
  9. package/dist/copy/misc.d.ts +5 -0
  10. package/dist/copy/misc.js +52 -0
  11. package/dist/copy/misc.js.map +1 -0
  12. package/dist/generators/generateImportPrismaStatement.d.ts +3 -0
  13. package/dist/generators/generateImportPrismaStatement.js +55 -0
  14. package/dist/generators/generateImportPrismaStatement.js.map +1 -0
  15. package/dist/generators/generateQueryBuilderHelper.d.ts +2 -0
  16. package/dist/generators/generateQueryBuilderHelper.js +139 -0
  17. package/dist/generators/generateQueryBuilderHelper.js.map +1 -0
  18. package/dist/generators/generateRouter.d.ts +6 -0
  19. package/dist/generators/generateRouter.js +340 -0
  20. package/dist/generators/generateRouter.js.map +1 -0
  21. package/dist/generators/generateUnifiedDocs.d.ts +1 -0
  22. package/dist/generators/generateUnifiedDocs.js +171 -0
  23. package/dist/generators/generateUnifiedDocs.js.map +1 -0
  24. package/dist/generators/generateUnifiedHandler.d.ts +6 -0
  25. package/dist/generators/generateUnifiedHandler.js +444 -0
  26. package/dist/generators/generateUnifiedHandler.js.map +1 -0
  27. package/dist/generators/generateUnifiedScalarUI.d.ts +5 -0
  28. package/dist/generators/generateUnifiedScalarUI.js +1390 -0
  29. package/dist/generators/generateUnifiedScalarUI.js.map +1 -0
  30. package/dist/index.d.ts +1 -0
  31. package/dist/index.js +80 -0
  32. package/dist/index.js.map +1 -0
  33. package/dist/utils/copyFiles.d.ts +6 -0
  34. package/dist/utils/copyFiles.js +123 -21
  35. package/dist/utils/copyFiles.js.map +1 -1
  36. package/dist/utils/strings.d.ts +2 -0
  37. package/dist/utils/writeFileSafely.d.ts +10 -0
  38. package/dist/utils/writeFileSafely.js +86 -14
  39. package/dist/utils/writeFileSafely.js.map +1 -1
  40. package/package.json +59 -28
  41. package/src/bin.ts +1 -1
  42. package/src/client/encodeQueryParams.ts +56 -0
  43. package/src/copy/buildModelOpenApi.ts +1569 -0
  44. package/src/copy/misc.ts +21 -0
  45. package/src/copy/operationDefinitions.ts +96 -0
  46. package/src/copy/parseQueryParams.ts +36 -21
  47. package/src/copy/routeConfig.ts +68 -28
  48. package/src/generators/generateImportPrismaStatement.ts +78 -0
  49. package/src/generators/generateQueryBuilderHelper.ts +138 -0
  50. package/src/generators/generateRouter.ts +352 -0
  51. package/src/generators/generateUnifiedDocs.ts +168 -0
  52. package/src/generators/generateUnifiedHandler.ts +469 -0
  53. package/src/generators/generateUnifiedScalarUI.ts +1409 -0
  54. package/src/index.ts +100 -0
  55. package/src/utils/copyFiles.ts +123 -16
  56. package/src/utils/writeFileSafely.ts +79 -25
  57. package/dist/generator.js +0 -47
  58. package/dist/generator.js.map +0 -1
  59. package/dist/helpers/generateImportPrismaStatement.js +0 -25
  60. package/dist/helpers/generateImportPrismaStatement.js.map +0 -1
  61. package/dist/helpers/generateOperation.js +0 -471
  62. package/dist/helpers/generateOperation.js.map +0 -1
  63. package/dist/helpers/generateRouteFile.js +0 -210
  64. package/dist/helpers/generateRouteFile.js.map +0 -1
  65. package/dist/utils/formatFile.js +0 -26
  66. package/dist/utils/formatFile.js.map +0 -1
  67. package/src/copy/encodeQueryParams.spec.ts +0 -303
  68. package/src/copy/encodeQueryParams.ts +0 -44
  69. package/src/copy/misc.spec.ts +0 -62
  70. package/src/copy/parseQueryParams.spec.ts +0 -187
  71. package/src/copy/transformZod.spec.ts +0 -763
  72. package/src/generator.ts +0 -54
  73. package/src/helpers/generateImportPrismaStatement.ts +0 -38
  74. package/src/helpers/generateOperation.ts +0 -515
  75. package/src/helpers/generateRouteFile.ts +0 -213
  76. package/src/utils/formatFile.ts +0 -22
package/src/generator.ts DELETED
@@ -1,54 +0,0 @@
1
- import { generatorHandler, GeneratorOptions } from '@prisma/generator-helper'
2
- import { logger } from '@prisma/sdk'
3
- import { GENERATOR_NAME } from './constants'
4
- import { writeFileSafely } from './utils/writeFileSafely'
5
- import { generateImportPrismaStatement } from './helpers/generateImportPrismaStatement'
6
- import { generateRouterFunction } from './helpers/generateRouteFile'
7
- import { generateOperationFunction, OPERATION_CONFIGS } from './helpers/generateOperation'
8
- import { copyFiles } from './utils/copyFiles'
9
-
10
- const { version } = require('../package.json')
11
-
12
- generatorHandler({
13
- onManifest() {
14
- logger.info(`${GENERATOR_NAME}:Registered`)
15
- return {
16
- version,
17
- defaultOutput: '../generated',
18
- prettyName: GENERATOR_NAME,
19
- }
20
- },
21
- onGenerate: async (options: GeneratorOptions) => {
22
- const prismaImportStatement = generateImportPrismaStatement(options)
23
-
24
- for await (const model of options.dmmf.datamodel.models) {
25
- if (
26
- model.documentation &&
27
- model.documentation.includes('generator off')
28
- ) {
29
- logger.info(
30
- `Skipping generation for model ${model.name} as it is marked with 'generator off'.`,
31
- )
32
- continue
33
- }
34
-
35
- for (const cfg of OPERATION_CONFIGS) {
36
- await writeFileSafely({
37
- content: generateOperationFunction(cfg, model, prismaImportStatement),
38
- options,
39
- model,
40
- operation: cfg.operation,
41
- })
42
- }
43
-
44
- await writeFileSafely({
45
- content: generateRouterFunction({ model }),
46
- options,
47
- model,
48
- operation: 'index',
49
- })
50
- }
51
-
52
- await copyFiles(options)
53
- },
54
- })
@@ -1,38 +0,0 @@
1
- import { GeneratorOptions } from '@prisma/generator-helper'
2
- import path from 'path'
3
-
4
- interface ImportPrismaStatementOptions {
5
- outputPath: string
6
- }
7
-
8
- function getImportGeneratorOptions(
9
- options: GeneratorOptions,
10
- ): ImportPrismaStatementOptions {
11
- const clientGenerator = options.otherGenerators.find(
12
- (gen) => gen.name === 'client',
13
- )
14
-
15
- if (!clientGenerator || !clientGenerator.output?.value) {
16
- throw new Error('Prisma client generator not found.')
17
- }
18
-
19
- const modelDirPath = path.join(
20
- options.generator.output?.value!,
21
- 'modelFolder', // workaround for saving with /modelName/operation structure
22
- )
23
- const clientOutputPath = clientGenerator.output.value
24
-
25
- const relativeImportPath = path.relative(modelDirPath, clientOutputPath)
26
-
27
- return {
28
- outputPath: relativeImportPath.split(path.sep).join(path.posix.sep),
29
- }
30
- }
31
-
32
- export function generateImportPrismaStatement(
33
- generatorOptions: GeneratorOptions,
34
- ): string {
35
- const options = getImportGeneratorOptions(generatorOptions)
36
- // console.log('options.outputPath :>> ', options.outputPath)
37
- return `import type { Prisma } from '${options.outputPath}';\nimport type { PrismaClient } from '${options.outputPath}';\n`
38
- }
@@ -1,515 +0,0 @@
1
- import { DMMF } from '@prisma/generator-helper'
2
- import { capitalize, toPascalCase } from '../utils/strings'
3
-
4
- type MiddlewareType = 'queryFull' | 'bodyArgs' | 'partialArgs' | 'emptyParsedQs'
5
-
6
- export interface OperationConfig {
7
- operation: string
8
- prismaMethod: string
9
- source: 'query' | 'body'
10
- successStatus: number
11
- resultVar: 'data' | 'result'
12
- exportInterface: boolean
13
- hasPassToNext: boolean
14
- importsModel: boolean
15
- argsPartial: boolean
16
- useCapitalizeForArgs: boolean
17
- tsIgnoreCall: boolean
18
- noCastInCall: boolean
19
- wrapCount: boolean
20
- useResLocalsValidator: boolean
21
- importValidatorConfig: boolean
22
- localsDataType: 'nullable' | 'array' | null
23
- validatorLocalsType: string
24
- zodTypeGeneric: string | null
25
- middlewareType: MiddlewareType
26
- }
27
-
28
- export const OPERATION_CONFIGS: OperationConfig[] = [
29
- {
30
- operation: 'FindUnique',
31
- prismaMethod: 'findUnique',
32
- source: 'query',
33
- successStatus: 200,
34
- resultVar: 'data',
35
- exportInterface: true,
36
- hasPassToNext: true,
37
- importsModel: true,
38
- argsPartial: false,
39
- useCapitalizeForArgs: false,
40
- tsIgnoreCall: false,
41
- noCastInCall: false,
42
- wrapCount: false,
43
- useResLocalsValidator: false,
44
- importValidatorConfig: false,
45
- localsDataType: 'nullable',
46
- validatorLocalsType: 'ZodType',
47
- zodTypeGeneric: null,
48
- middlewareType: 'queryFull',
49
- },
50
- {
51
- operation: 'FindFirst',
52
- prismaMethod: 'findFirst',
53
- source: 'query',
54
- successStatus: 200,
55
- resultVar: 'data',
56
- exportInterface: true,
57
- hasPassToNext: true,
58
- importsModel: true,
59
- argsPartial: false,
60
- useCapitalizeForArgs: false,
61
- tsIgnoreCall: false,
62
- noCastInCall: false,
63
- wrapCount: false,
64
- useResLocalsValidator: false,
65
- importValidatorConfig: false,
66
- localsDataType: 'nullable',
67
- validatorLocalsType: 'ZodType',
68
- zodTypeGeneric: null,
69
- middlewareType: 'queryFull',
70
- },
71
- {
72
- operation: 'FindMany',
73
- prismaMethod: 'findMany',
74
- source: 'query',
75
- successStatus: 200,
76
- resultVar: 'data',
77
- exportInterface: true,
78
- hasPassToNext: true,
79
- importsModel: true,
80
- argsPartial: false,
81
- useCapitalizeForArgs: false,
82
- tsIgnoreCall: false,
83
- noCastInCall: false,
84
- wrapCount: false,
85
- useResLocalsValidator: false,
86
- importValidatorConfig: false,
87
- localsDataType: 'array',
88
- validatorLocalsType: 'ZodType',
89
- zodTypeGeneric: null,
90
- middlewareType: 'queryFull',
91
- },
92
- {
93
- operation: 'Create',
94
- prismaMethod: 'create',
95
- source: 'body',
96
- successStatus: 201,
97
- resultVar: 'data',
98
- exportInterface: false,
99
- hasPassToNext: false,
100
- importsModel: false,
101
- argsPartial: false,
102
- useCapitalizeForArgs: false,
103
- tsIgnoreCall: false,
104
- noCastInCall: false,
105
- wrapCount: false,
106
- useResLocalsValidator: false,
107
- importValidatorConfig: false,
108
- localsDataType: null,
109
- validatorLocalsType: 'ZodType',
110
- zodTypeGeneric: null,
111
- middlewareType: 'bodyArgs',
112
- },
113
- {
114
- operation: 'CreateMany',
115
- prismaMethod: 'createMany',
116
- source: 'body',
117
- successStatus: 201,
118
- resultVar: 'data',
119
- exportInterface: false,
120
- hasPassToNext: false,
121
- importsModel: false,
122
- argsPartial: false,
123
- useCapitalizeForArgs: false,
124
- tsIgnoreCall: false,
125
- noCastInCall: false,
126
- wrapCount: false,
127
- useResLocalsValidator: false,
128
- importValidatorConfig: false,
129
- localsDataType: null,
130
- validatorLocalsType: 'ZodType',
131
- zodTypeGeneric: null,
132
- middlewareType: 'bodyArgs',
133
- },
134
- {
135
- operation: 'Update',
136
- prismaMethod: 'update',
137
- source: 'body',
138
- successStatus: 200,
139
- resultVar: 'data',
140
- exportInterface: false,
141
- hasPassToNext: false,
142
- importsModel: false,
143
- argsPartial: false,
144
- useCapitalizeForArgs: false,
145
- tsIgnoreCall: false,
146
- noCastInCall: false,
147
- wrapCount: false,
148
- useResLocalsValidator: false,
149
- importValidatorConfig: false,
150
- localsDataType: null,
151
- validatorLocalsType: 'ZodType',
152
- zodTypeGeneric: null,
153
- middlewareType: 'bodyArgs',
154
- },
155
- {
156
- operation: 'UpdateMany',
157
- prismaMethod: 'updateMany',
158
- source: 'body',
159
- successStatus: 200,
160
- resultVar: 'data',
161
- exportInterface: false,
162
- hasPassToNext: false,
163
- importsModel: false,
164
- argsPartial: false,
165
- useCapitalizeForArgs: false,
166
- tsIgnoreCall: false,
167
- noCastInCall: false,
168
- wrapCount: true,
169
- useResLocalsValidator: false,
170
- importValidatorConfig: false,
171
- localsDataType: null,
172
- validatorLocalsType: 'ZodType',
173
- zodTypeGeneric: 'UpdateManyResult',
174
- middlewareType: 'bodyArgs',
175
- },
176
- {
177
- operation: 'Upsert',
178
- prismaMethod: 'upsert',
179
- source: 'body',
180
- successStatus: 200,
181
- resultVar: 'data',
182
- exportInterface: false,
183
- hasPassToNext: false,
184
- importsModel: false,
185
- argsPartial: false,
186
- useCapitalizeForArgs: false,
187
- tsIgnoreCall: false,
188
- noCastInCall: false,
189
- wrapCount: false,
190
- useResLocalsValidator: false,
191
- importValidatorConfig: false,
192
- localsDataType: null,
193
- validatorLocalsType: 'ZodType',
194
- zodTypeGeneric: null,
195
- middlewareType: 'bodyArgs',
196
- },
197
- {
198
- operation: 'Delete',
199
- prismaMethod: 'delete',
200
- source: 'body',
201
- successStatus: 200,
202
- resultVar: 'data',
203
- exportInterface: false,
204
- hasPassToNext: false,
205
- importsModel: false,
206
- argsPartial: false,
207
- useCapitalizeForArgs: false,
208
- tsIgnoreCall: false,
209
- noCastInCall: false,
210
- wrapCount: false,
211
- useResLocalsValidator: false,
212
- importValidatorConfig: false,
213
- localsDataType: null,
214
- validatorLocalsType: 'ZodType',
215
- zodTypeGeneric: null,
216
- middlewareType: 'bodyArgs',
217
- },
218
- {
219
- operation: 'DeleteMany',
220
- prismaMethod: 'deleteMany',
221
- source: 'body',
222
- successStatus: 200,
223
- resultVar: 'result',
224
- exportInterface: false,
225
- hasPassToNext: false,
226
- importsModel: false,
227
- argsPartial: false,
228
- useCapitalizeForArgs: false,
229
- tsIgnoreCall: false,
230
- noCastInCall: false,
231
- wrapCount: false,
232
- useResLocalsValidator: false,
233
- importValidatorConfig: false,
234
- localsDataType: null,
235
- validatorLocalsType: 'ZodType',
236
- zodTypeGeneric: null,
237
- middlewareType: 'bodyArgs',
238
- },
239
- {
240
- operation: 'Aggregate',
241
- prismaMethod: 'aggregate',
242
- source: 'query',
243
- successStatus: 200,
244
- resultVar: 'result',
245
- exportInterface: false,
246
- hasPassToNext: false,
247
- importsModel: false,
248
- argsPartial: true,
249
- useCapitalizeForArgs: true,
250
- tsIgnoreCall: false,
251
- noCastInCall: false,
252
- wrapCount: false,
253
- useResLocalsValidator: true,
254
- importValidatorConfig: true,
255
- localsDataType: null,
256
- validatorLocalsType: 'ValidatorConfig',
257
- zodTypeGeneric: null,
258
- middlewareType: 'partialArgs',
259
- },
260
- {
261
- operation: 'Count',
262
- prismaMethod: 'count',
263
- source: 'query',
264
- successStatus: 200,
265
- resultVar: 'result',
266
- exportInterface: false,
267
- hasPassToNext: false,
268
- importsModel: false,
269
- argsPartial: true,
270
- useCapitalizeForArgs: false,
271
- tsIgnoreCall: false,
272
- noCastInCall: false,
273
- wrapCount: false,
274
- useResLocalsValidator: false,
275
- importValidatorConfig: false,
276
- localsDataType: null,
277
- validatorLocalsType: 'ZodType',
278
- zodTypeGeneric: null,
279
- middlewareType: 'emptyParsedQs',
280
- },
281
- {
282
- operation: 'GroupBy',
283
- prismaMethod: 'groupBy',
284
- source: 'query',
285
- successStatus: 200,
286
- resultVar: 'result',
287
- exportInterface: false,
288
- hasPassToNext: false,
289
- importsModel: false,
290
- argsPartial: true,
291
- useCapitalizeForArgs: false,
292
- tsIgnoreCall: true,
293
- noCastInCall: true,
294
- wrapCount: false,
295
- useResLocalsValidator: false,
296
- importValidatorConfig: false,
297
- localsDataType: null,
298
- validatorLocalsType: 'ZodType',
299
- zodTypeGeneric: null,
300
- middlewareType: 'emptyParsedQs',
301
- },
302
- {
303
- operation: 'CreateManyAndReturn',
304
- prismaMethod: 'createManyAndReturn',
305
- source: 'body',
306
- successStatus: 201,
307
- resultVar: 'data',
308
- exportInterface: false,
309
- hasPassToNext: false,
310
- importsModel: false,
311
- argsPartial: false,
312
- useCapitalizeForArgs: false,
313
- tsIgnoreCall: false,
314
- noCastInCall: false,
315
- wrapCount: false,
316
- useResLocalsValidator: false,
317
- importValidatorConfig: false,
318
- localsDataType: null,
319
- validatorLocalsType: 'ZodType',
320
- zodTypeGeneric: null,
321
- middlewareType: 'bodyArgs',
322
- },
323
- {
324
- operation: 'UpdateManyAndReturn',
325
- prismaMethod: 'updateManyAndReturn',
326
- source: 'body',
327
- successStatus: 200,
328
- resultVar: 'data',
329
- exportInterface: false,
330
- hasPassToNext: false,
331
- importsModel: false,
332
- argsPartial: false,
333
- useCapitalizeForArgs: false,
334
- tsIgnoreCall: false,
335
- noCastInCall: false,
336
- wrapCount: false,
337
- useResLocalsValidator: false,
338
- importValidatorConfig: false,
339
- localsDataType: null,
340
- validatorLocalsType: 'ZodType',
341
- zodTypeGeneric: null,
342
- middlewareType: 'bodyArgs',
343
- },
344
- ]
345
-
346
- export function generateOperationFunction(
347
- cfg: OperationConfig,
348
- model: DMMF.Model,
349
- prismaImportStatement: string,
350
- ): string {
351
- const modelName = model.name
352
- const functionName = `${modelName}${cfg.operation}`
353
- const interfaceName = `${cfg.operation}Request`
354
- const middlewareTypeName = `${cfg.operation}Middleware`
355
-
356
- const argsTypeName = cfg.useCapitalizeForArgs
357
- ? `Prisma.${capitalize(modelName)}${cfg.operation}Args`
358
- : `Prisma.${modelName}${cfg.operation}Args`
359
-
360
- const zodType = cfg.zodTypeGeneric ? `ZodType<${cfg.zodTypeGeneric}>` : 'ZodType'
361
-
362
- const prismaImport = cfg.importsModel
363
- ? prismaImportStatement.replace('{ Prisma }', `{ Prisma, ${modelName} }`)
364
- : prismaImportStatement
365
-
366
- const importLines = [
367
- prismaImport,
368
- `import { Request, Response, NextFunction } from 'express';`,
369
- `import { RequestHandler, ParamsDictionary } from 'express-serve-static-core';`,
370
- ]
371
-
372
- if (cfg.source === 'query') {
373
- importLines.push(`import { ParsedQs } from 'qs';`)
374
- }
375
-
376
- importLines.push(`import { ZodType } from 'zod';`)
377
-
378
- if (cfg.importValidatorConfig) {
379
- importLines.push(`import { ValidatorConfig } from '../routeConfig';`)
380
- }
381
-
382
- const extraTypes = cfg.wrapCount ? `\ntype UpdateManyResult = { count: number };\n` : ''
383
-
384
- const queryType = cfg.argsPartial
385
- ? `Partial<${argsTypeName}> & ParsedQs`
386
- : `${argsTypeName} & ParsedQs`
387
-
388
- let interfaceBody: string
389
- if (cfg.source === 'query') {
390
- const localsEntries: string[] = []
391
- if (cfg.localsDataType === 'nullable') {
392
- localsEntries.push(` data?: ${modelName} | null`)
393
- } else if (cfg.localsDataType === 'array') {
394
- localsEntries.push(` data?: ${modelName}[]`)
395
- }
396
- localsEntries.push(` outputValidator?: ${cfg.validatorLocalsType};`)
397
-
398
- interfaceBody = [
399
- ` prisma: PrismaClient;`,
400
- ` query: ${queryType};`,
401
- ` outputValidation?: ${zodType};`,
402
- ...(cfg.hasPassToNext ? [` passToNext?: boolean;`] : []),
403
- ` locals?: {`,
404
- ...localsEntries,
405
- ` }`,
406
- ].join('\n')
407
- } else {
408
- interfaceBody = [
409
- ` prisma: PrismaClient;`,
410
- ` body: ${argsTypeName};`,
411
- ` outputValidation?: ${zodType};`,
412
- ` locals?: {`,
413
- ` outputValidator?: ${zodType};`,
414
- ` };`,
415
- ].join('\n')
416
- }
417
-
418
- const exportKw = cfg.exportInterface ? 'export ' : ''
419
- const interfaceDecl = `${exportKw}interface ${interfaceName} extends Request {\n${interfaceBody}\n}`
420
-
421
- let middlewareTypeDecl: string
422
- switch (cfg.middlewareType) {
423
- case 'queryFull':
424
- middlewareTypeDecl = `export type ${middlewareTypeName} = RequestHandler<ParamsDictionary, any, any, ${argsTypeName} & ParsedQs, Record<string, any>>`
425
- break
426
- case 'bodyArgs':
427
- middlewareTypeDecl = `export type ${middlewareTypeName} = RequestHandler<ParamsDictionary, any, ${argsTypeName}, Record<string, any>>`
428
- break
429
- case 'partialArgs':
430
- middlewareTypeDecl = `export type ${middlewareTypeName} = RequestHandler<ParamsDictionary, any, Partial<${argsTypeName}>, Record<string, any>>`
431
- break
432
- case 'emptyParsedQs':
433
- middlewareTypeDecl = `export type ${middlewareTypeName} = RequestHandler<ParamsDictionary, any, {}, ParsedQs>`
434
- break
435
- }
436
-
437
- const validatorLine = cfg.useResLocalsValidator
438
- ? `const outputValidator = res.locals.outputValidator?.schema || req.outputValidation;`
439
- : `const outputValidator = req.locals?.outputValidator || req.outputValidation;`
440
-
441
- const v = cfg.resultVar
442
- const callArg = cfg.source === 'body'
443
- ? 'req.body'
444
- : cfg.noCastInCall
445
- ? 'req.query'
446
- : `req.query as ${argsTypeName}`
447
-
448
- const prismaCall = [
449
- cfg.tsIgnoreCall ? ' // @ts-ignore' : '',
450
- ` const ${v} = await req.prisma.${toPascalCase(modelName)}.${cfg.prismaMethod}(${callArg});`,
451
- ].filter(Boolean).join('\n')
452
-
453
- const successJson = cfg.wrapCount
454
- ? `{ count: validationResult.data.count }`
455
- : `validationResult.data`
456
-
457
- const elseJson = cfg.wrapCount
458
- ? `{ count: ${v}.count }`
459
- : v
460
-
461
- let bodyLines: string[]
462
-
463
- if (cfg.hasPassToNext) {
464
- bodyLines = [
465
- ` ${validatorLine}`,
466
- ``,
467
- prismaCall,
468
- ` if (req.passToNext) {`,
469
- ` if (req.locals) req.locals.data = ${v};`,
470
- ` next();`,
471
- ` } else if (outputValidator) {`,
472
- ` const validationResult = outputValidator.safeParse(${v});`,
473
- ` if (validationResult.success) {`,
474
- ` return res.status(${cfg.successStatus}).json(${successJson});`,
475
- ` } else {`,
476
- ` return res.status(400).json({ error: 'Invalid data format', details: validationResult.error });`,
477
- ` }`,
478
- ` } else {`,
479
- ` return res.status(${cfg.successStatus}).json(${elseJson});`,
480
- ` }`,
481
- ]
482
- } else {
483
- bodyLines = [
484
- ` ${validatorLine}`,
485
- ``,
486
- prismaCall,
487
- ``,
488
- ` if (outputValidator) {`,
489
- ` const validationResult = outputValidator.safeParse(${v});`,
490
- ` if (validationResult.success) {`,
491
- ` return res.status(${cfg.successStatus}).json(${successJson});`,
492
- ` } else {`,
493
- ` return res.status(400).json({ error: 'Invalid data format', details: validationResult.error });`,
494
- ` }`,
495
- ` } else {`,
496
- ` return res.status(${cfg.successStatus}).json(${elseJson});`,
497
- ` }`,
498
- ]
499
- }
500
-
501
- return `
502
- ${importLines.join('\n')}
503
- ${extraTypes}
504
- ${interfaceDecl}
505
-
506
- ${middlewareTypeDecl}
507
-
508
- export async function ${functionName}(req: ${interfaceName}, res: Response, next: NextFunction) {
509
- try {
510
- ${bodyLines.join('\n')}
511
- } catch(error: unknown) {
512
- next(error)
513
- }
514
- }`
515
- }