prisma-generator-express 1.67.0 → 1.68.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.
- package/README.md +223 -28
- package/dist/copy/operationDefinitions.d.ts +4 -1
- package/dist/copy/operationDefinitions.js +64 -3
- package/dist/copy/operationDefinitions.js.map +1 -1
- package/dist/generators/generateFastifyHandler.js +55 -9
- package/dist/generators/generateFastifyHandler.js.map +1 -1
- package/dist/generators/generateHonoHandler.js +44 -4
- package/dist/generators/generateHonoHandler.js.map +1 -1
- package/dist/generators/generateHonoOpenApi.d.ts +13 -0
- package/dist/generators/generateHonoOpenApi.js +70 -0
- package/dist/generators/generateHonoOpenApi.js.map +1 -0
- package/dist/generators/generateMcpApp.d.ts +10 -0
- package/dist/generators/generateMcpApp.js +113 -0
- package/dist/generators/generateMcpApp.js.map +1 -0
- package/dist/generators/generateModelMcp.d.ts +9 -0
- package/dist/generators/generateModelMcp.js +145 -0
- package/dist/generators/generateModelMcp.js.map +1 -0
- package/dist/generators/generateModelMetadata.js +9 -5
- package/dist/generators/generateModelMetadata.js.map +1 -1
- package/dist/generators/generateRouter.js +62 -56
- package/dist/generators/generateRouter.js.map +1 -1
- package/dist/generators/generateRouterFastify.js +56 -64
- package/dist/generators/generateRouterFastify.js.map +1 -1
- package/dist/generators/generateRouterHono.js +43 -411
- package/dist/generators/generateRouterHono.js.map +1 -1
- package/dist/generators/generateRouterPartsHono.d.ts +14 -0
- package/dist/generators/generateRouterPartsHono.js +180 -0
- package/dist/generators/generateRouterPartsHono.js.map +1 -0
- package/dist/generators/generateUnifiedHandler.js +36 -20
- package/dist/generators/generateUnifiedHandler.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +101 -3
- package/dist/index.js.map +1 -1
- package/dist/utils/copyFiles.d.ts +1 -1
- package/dist/utils/copyFiles.js +20 -1
- package/dist/utils/copyFiles.js.map +1 -1
- package/dist/utils/mcpGate.d.ts +10 -0
- package/dist/utils/mcpGate.js +118 -0
- package/dist/utils/mcpGate.js.map +1 -0
- package/dist/utils/writeFileSafely.js +6 -0
- package/dist/utils/writeFileSafely.js.map +1 -1
- package/package.json +18 -3
- package/src/copy/buildModelOpenApi.ts +76 -126
- package/src/copy/docsRenderer.ts +14 -15
- package/src/copy/mcpRuntime.ts +1017 -0
- package/src/copy/operationDefinitions.ts +78 -4
- package/src/copy/operationPipeline.ts +267 -0
- package/src/copy/operationRuntime.ts +24 -14
- package/src/copy/operationSchemas.ts +2773 -0
- package/src/copy/pagination.ts +10 -2
- package/src/copy/routeConfig.hono.ts +34 -5
- package/src/copy/routeConfig.ts +1 -1
- package/src/copy/routerParts.ts +524 -0
- package/src/generators/generateFastifyHandler.ts +55 -9
- package/src/generators/generateHonoHandler.ts +44 -4
- package/src/generators/generateHonoOpenApi.ts +93 -0
- package/src/generators/generateMcpApp.ts +133 -0
- package/src/generators/generateModelMcp.ts +208 -0
- package/src/generators/generateModelMetadata.ts +16 -10
- package/src/generators/generateRouter.ts +62 -56
- package/src/generators/generateRouterFastify.ts +56 -64
- package/src/generators/generateRouterHono.ts +49 -459
- package/src/generators/generateRouterPartsHono.ts +220 -0
- package/src/generators/generateUnifiedHandler.ts +36 -21
- package/src/index.ts +116 -3
- package/src/utils/copyFiles.ts +36 -0
- package/src/utils/mcpGate.ts +157 -0
- package/src/utils/writeFileSafely.ts +6 -0
|
@@ -146,7 +146,6 @@ import { sanitizeKeys, normalizePrefix, getEnv, isPlainObject, resolveDropGuardE
|
|
|
146
146
|
import { buildModelOpenApi } from '../buildModelOpenApi${ext}'
|
|
147
147
|
import {
|
|
148
148
|
normalizeOperation,
|
|
149
|
-
resolveOperationVariantKey,
|
|
150
149
|
validateCountSourceWhere,
|
|
151
150
|
validateOperationConfig,
|
|
152
151
|
validateUpdateEachConfig,
|
|
@@ -158,8 +157,14 @@ import type { NormalizedOperationConfig } from '../routeConfig${ext}'
|
|
|
158
157
|
import type { OperationContext } from '../operationRuntime${ext}'
|
|
159
158
|
import { transformResult } from '../operationRuntime${ext}'
|
|
160
159
|
import { HttpError, mapError } from '../errorMapper${ext}'
|
|
161
|
-
import {
|
|
162
|
-
|
|
160
|
+
import {
|
|
161
|
+
routeOperation,
|
|
162
|
+
prepareGuardOperation,
|
|
163
|
+
settleStage,
|
|
164
|
+
classifyError,
|
|
165
|
+
memoizeContext,
|
|
166
|
+
type ArgsChannel,
|
|
167
|
+
} from '../operationPipeline${ext}'
|
|
163
168
|
import { mergePaginationConfig } from '../pagination${ext}'
|
|
164
169
|
import { acceptsNdjson, runNdjsonFindMany } from '../ndjson${ext}'
|
|
165
170
|
import {
|
|
@@ -171,7 +176,6 @@ import {
|
|
|
171
176
|
} from '../sse${ext}'
|
|
172
177
|
import { relationModels } from '../relationModels${ext}'
|
|
173
178
|
import { runAutoIncludeProgressive } from '../autoIncludeRuntime${ext}'
|
|
174
|
-
import { applyDroppedGuard } from '../projectionDefaults${ext}'
|
|
175
179
|
import type { OpKind } from '../projectionDefaults${ext}'
|
|
176
180
|
import { MODEL_FIELDS, MODEL_ENUMS } from './${modelName}Metadata${ext}'
|
|
177
181
|
|
|
@@ -220,12 +224,34 @@ type LocalsBag = {
|
|
|
220
224
|
guardShape?: Record<string, unknown>
|
|
221
225
|
guardCaller?: string
|
|
222
226
|
operationOverride?: RuntimeOperationOverride
|
|
223
|
-
|
|
227
|
+
getContext?: () => Promise<unknown>
|
|
224
228
|
guardVariantKey?: string
|
|
225
|
-
|
|
229
|
+
routeFailure?: { ok: false; failure: HttpError }
|
|
230
|
+
guardFailure?: { ok: false; failure: HttpError }
|
|
226
231
|
data?: unknown
|
|
227
232
|
}
|
|
228
233
|
|
|
234
|
+
function queryChannel(res: Response): ArgsChannel {
|
|
235
|
+
return {
|
|
236
|
+
read: () => readLocals(res).parsedQuery,
|
|
237
|
+
write: (next) => {
|
|
238
|
+
readLocals(res).parsedQuery = next
|
|
239
|
+
},
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function bodyChannel(req: Request): ArgsChannel {
|
|
244
|
+
return {
|
|
245
|
+
read: () =>
|
|
246
|
+
isPlainObject(req.body)
|
|
247
|
+
? (req.body as Record<string, unknown>)
|
|
248
|
+
: undefined,
|
|
249
|
+
write: (next) => {
|
|
250
|
+
req.body = next
|
|
251
|
+
},
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
229
255
|
function normalizeExpressOperation(
|
|
230
256
|
config: OperationConfigLike | undefined,
|
|
231
257
|
): NormalizedOp {
|
|
@@ -329,12 +355,15 @@ function variantAfterDispatcher(opConfig: NormalizedOp): RequestHandler {
|
|
|
329
355
|
|
|
330
356
|
function requireVariantKey(): RequestHandler {
|
|
331
357
|
return (_req, res, next) => {
|
|
332
|
-
const
|
|
333
|
-
|
|
334
|
-
|
|
358
|
+
const locals = readLocals(res)
|
|
359
|
+
try {
|
|
360
|
+
if (locals.routeFailure) settleStage(locals.routeFailure)
|
|
361
|
+
if (locals.guardFailure) settleStage(locals.guardFailure)
|
|
362
|
+
} catch (err) {
|
|
363
|
+
next(err)
|
|
335
364
|
return
|
|
336
365
|
}
|
|
337
|
-
next(
|
|
366
|
+
next()
|
|
338
367
|
}
|
|
339
368
|
}
|
|
340
369
|
|
|
@@ -408,7 +437,7 @@ export function ${routerFunctionName}<TCtx = unknown, TPrisma extends PrismaClie
|
|
|
408
437
|
const locals = readLocals(res)
|
|
409
438
|
return {
|
|
410
439
|
operationOverride: locals.operationOverride,
|
|
411
|
-
resolveOperationContext: locals.
|
|
440
|
+
resolveOperationContext: locals.getContext,
|
|
412
441
|
prisma: extReq.prisma,
|
|
413
442
|
postgres: extReq.postgres,
|
|
414
443
|
sqlite: extReq.sqlite,
|
|
@@ -422,11 +451,6 @@ export function ${routerFunctionName}<TCtx = unknown, TPrisma extends PrismaClie
|
|
|
422
451
|
}
|
|
423
452
|
}
|
|
424
453
|
|
|
425
|
-
const buildResolveContext = (req: Request): (() => unknown | Promise<unknown>) | undefined => {
|
|
426
|
-
if (typeof config.resolveContext !== 'function') return undefined
|
|
427
|
-
return () => (config.resolveContext as (r: Request) => unknown | Promise<unknown>)(req)
|
|
428
|
-
}
|
|
429
|
-
|
|
430
454
|
const parseQuery: RequestHandler = (req, res, next) => {
|
|
431
455
|
const rawQuery = req.query
|
|
432
456
|
if (rawQuery && Object.keys(rawQuery).length > 0) {
|
|
@@ -458,11 +482,10 @@ export function ${routerFunctionName}<TCtx = unknown, TPrisma extends PrismaClie
|
|
|
458
482
|
|
|
459
483
|
const setShape = (opConfig: NormalizedOp, opKind: OpKind): RequestHandler => {
|
|
460
484
|
return async (req, res, next) => {
|
|
485
|
+
const locals = readLocals(res)
|
|
461
486
|
try {
|
|
462
|
-
const locals = readLocals(res)
|
|
463
487
|
locals.operationOverride = opConfig.override
|
|
464
|
-
|
|
465
|
-
locals.resolveOperationContext = () => context ??= Promise.resolve(config.resolveContext?.(req))
|
|
488
|
+
locals.getContext = memoizeContext(() => config.resolveContext?.(req))
|
|
466
489
|
const merged = mergePaginationConfig(config.pagination, opConfig.pagination)
|
|
467
490
|
if (merged) {
|
|
468
491
|
locals.routeConfig = { pagination: merged }
|
|
@@ -473,51 +496,34 @@ export function ${routerFunctionName}<TCtx = unknown, TPrisma extends PrismaClie
|
|
|
473
496
|
const caller = config.guard?.resolveVariant?.(req) ?? headerValue ?? undefined
|
|
474
497
|
if (typeof caller === 'string') locals.guardCaller = caller
|
|
475
498
|
|
|
476
|
-
const
|
|
477
|
-
if (!
|
|
478
|
-
locals.
|
|
499
|
+
const routed = routeOperation({ guardRouting: opConfig.guardRouting, caller })
|
|
500
|
+
if (!routed.ok) {
|
|
501
|
+
locals.routeFailure = routed
|
|
479
502
|
next()
|
|
480
503
|
return
|
|
481
504
|
}
|
|
482
505
|
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
opKind,
|
|
498
|
-
{
|
|
499
|
-
readQuery: locals.parsedQuery,
|
|
500
|
-
writeBody: isPlainObject(req.body)
|
|
501
|
-
? (req.body as Record<string, unknown>)
|
|
502
|
-
: undefined,
|
|
503
|
-
},
|
|
504
|
-
() => {
|
|
505
|
-
if (!locals.parsedQuery) locals.parsedQuery = {}
|
|
506
|
-
return locals.parsedQuery
|
|
507
|
-
},
|
|
508
|
-
() => {
|
|
509
|
-
if (!isPlainObject(req.body)) {
|
|
510
|
-
req.body = {}
|
|
511
|
-
}
|
|
512
|
-
return req.body as Record<string, unknown>
|
|
513
|
-
},
|
|
514
|
-
)
|
|
515
|
-
}
|
|
506
|
+
if (routed.variantKey !== undefined) locals.guardVariantKey = routed.variantKey
|
|
507
|
+
|
|
508
|
+
const guard = await prepareGuardOperation(routed, {
|
|
509
|
+
guardShape: opConfig.guardShape,
|
|
510
|
+
opKind,
|
|
511
|
+
policy: { dropGuard, validateResolvedShapes: false },
|
|
512
|
+
getContext: typeof config.resolveContext === 'function' ? locals.getContext : undefined,
|
|
513
|
+
args: queryChannel(res),
|
|
514
|
+
writeArgs: bodyChannel(req),
|
|
515
|
+
})
|
|
516
|
+
if (!guard.ok) {
|
|
517
|
+
locals.guardFailure = guard
|
|
518
|
+
next()
|
|
519
|
+
return
|
|
516
520
|
}
|
|
517
521
|
|
|
522
|
+
locals.guardShape = guard.guardShape
|
|
523
|
+
|
|
518
524
|
next()
|
|
519
525
|
} catch (err) {
|
|
520
|
-
next(
|
|
526
|
+
next(classifyError(err))
|
|
521
527
|
}
|
|
522
528
|
}
|
|
523
529
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateRouter.js","sourceRoot":"","sources":["../../src/generators/generateRouter.ts"],"names":[],"mappings":";;AA8HA,
|
|
1
|
+
{"version":3,"file":"generateRouter.js","sourceRoot":"","sources":["../../src/generators/generateRouter.ts"],"names":[],"mappings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S;8DAChE,SAAS;;;OAGhE,SAAS;;;;;;2BAMW,SAAS;;;;oCAIA,SAAS;;;;;4DAKe,YAAY;;;;;;;;;;;WAW7D,SAAS;;;;4CAIwB,aAAa,oBAAoB,YAAY;;;;;;;;;WAS9E,SAAS;;;;4CAIwB,aAAa,oBAAoB,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;4BA8N7D,SAAS;8BACP,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAoFvC,YAAY;;EAEZ,aAAa;;;;+CAIgC,SAAS;;;;YAI5C,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAmCpB,CAAA;AACD,CAAC"}
|
|
@@ -104,7 +104,6 @@ import { sanitizeKeys, normalizePrefix, getEnv, isPlainObject, resolveDropGuardE
|
|
|
104
104
|
import { buildModelOpenApi } from '../buildModelOpenApi${ext}'
|
|
105
105
|
import {
|
|
106
106
|
normalizeOperation,
|
|
107
|
-
resolveOperationVariantKey,
|
|
108
107
|
validateCountSourceWhere,
|
|
109
108
|
validateOperationConfig,
|
|
110
109
|
validateUpdateEachConfig,
|
|
@@ -116,10 +115,15 @@ import type { NormalizedOperationConfig } from '../routeConfig${ext}'
|
|
|
116
115
|
import type { OperationContext } from '../operationRuntime${ext}'
|
|
117
116
|
import { transformResult } from '../operationRuntime${ext}'
|
|
118
117
|
import { HttpError, mapError } from '../errorMapper${ext}'
|
|
119
|
-
import { formatGuardVariantResolutionError } from '../guardVariantError${ext}'
|
|
120
|
-
import type { GuardVariantResolution } from '../guardVariantRouting${ext}'
|
|
121
118
|
import { mergePaginationConfig } from '../pagination${ext}'
|
|
122
|
-
import {
|
|
119
|
+
import {
|
|
120
|
+
routeOperation,
|
|
121
|
+
prepareGuardOperation,
|
|
122
|
+
settleStage,
|
|
123
|
+
classifyError,
|
|
124
|
+
memoizeContext,
|
|
125
|
+
type ArgsChannel,
|
|
126
|
+
} from '../operationPipeline${ext}'
|
|
123
127
|
import type { OpKind } from '../projectionDefaults${ext}'
|
|
124
128
|
import { MODEL_FIELDS, MODEL_ENUMS } from './${modelName}Metadata${ext}'
|
|
125
129
|
|
|
@@ -163,13 +167,36 @@ type FastifyExtended = FastifyRequest & {
|
|
|
163
167
|
guardShape?: Record<string, unknown>
|
|
164
168
|
guardCaller?: string
|
|
165
169
|
operationOverride?: RuntimeOperationOverride
|
|
166
|
-
|
|
170
|
+
getContext?: () => Promise<unknown>
|
|
167
171
|
guardVariantKey?: string
|
|
168
|
-
|
|
172
|
+
routeFailure?: { ok: false; failure: HttpError }
|
|
173
|
+
guardFailure?: { ok: false; failure: HttpError }
|
|
169
174
|
resultData?: unknown
|
|
170
175
|
resultStatus?: number
|
|
171
176
|
}
|
|
172
177
|
|
|
178
|
+
function queryChannel(request: FastifyRequest): ArgsChannel {
|
|
179
|
+
const fx = request as FastifyExtended
|
|
180
|
+
return {
|
|
181
|
+
read: () => fx.parsedQuery,
|
|
182
|
+
write: (next) => {
|
|
183
|
+
fx.parsedQuery = next
|
|
184
|
+
},
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function bodyChannel(request: FastifyRequest): ArgsChannel {
|
|
189
|
+
return {
|
|
190
|
+
read: () =>
|
|
191
|
+
isPlainObject(request.body)
|
|
192
|
+
? (request.body as Record<string, unknown>)
|
|
193
|
+
: undefined,
|
|
194
|
+
write: (next) => {
|
|
195
|
+
;(request as unknown as { body: unknown }).body = next
|
|
196
|
+
},
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
173
200
|
function normalizeFastifyOperation(
|
|
174
201
|
config: OperationConfigLike | undefined,
|
|
175
202
|
): NormalizedOp {
|
|
@@ -204,14 +231,6 @@ function parseBodyAsQueryHook(request: FastifyRequest): void {
|
|
|
204
231
|
(request as FastifyExtended).parsedQuery = sanitizeKeys(body as Record<string, unknown>)
|
|
205
232
|
}
|
|
206
233
|
|
|
207
|
-
function buildResolveContext<TCtx, TPrisma extends PrismaClientLike>(
|
|
208
|
-
config: ${modelName}RouteConfig<TCtx, TPrisma>,
|
|
209
|
-
request: FastifyRequest,
|
|
210
|
-
): (() => unknown | Promise<unknown>) | undefined {
|
|
211
|
-
if (typeof config.resolveContext !== 'function') return undefined
|
|
212
|
-
return () => (config.resolveContext as (r: FastifyRequest) => unknown | Promise<unknown>)(request)
|
|
213
|
-
}
|
|
214
|
-
|
|
215
234
|
function makeShapeHook<TCtx, TPrisma extends PrismaClientLike>(
|
|
216
235
|
config: ${modelName}RouteConfig<TCtx, TPrisma>,
|
|
217
236
|
opConfig: NormalizedOp,
|
|
@@ -231,8 +250,7 @@ function makeShapeHook<TCtx, TPrisma extends PrismaClientLike>(
|
|
|
231
250
|
return async (request: FastifyRequest) => {
|
|
232
251
|
const fx = request as FastifyExtended
|
|
233
252
|
fx.operationOverride = opConfig.override
|
|
234
|
-
|
|
235
|
-
fx.resolveOperationContext = () => context ??= Promise.resolve(config.resolveContext?.(request))
|
|
253
|
+
fx.getContext = memoizeContext(() => config.resolveContext?.(request))
|
|
236
254
|
const merged = mergePaginationConfig(config.pagination, opConfig.pagination)
|
|
237
255
|
if (merged) fx.routeConfig = { pagination: merged }
|
|
238
256
|
|
|
@@ -243,46 +261,28 @@ function makeShapeHook<TCtx, TPrisma extends PrismaClientLike>(
|
|
|
243
261
|
?? undefined
|
|
244
262
|
if (typeof caller === 'string') fx.guardCaller = caller
|
|
245
263
|
|
|
246
|
-
const
|
|
247
|
-
if (!
|
|
248
|
-
fx.
|
|
264
|
+
const routed = routeOperation({ guardRouting: opConfig.guardRouting, caller })
|
|
265
|
+
if (!routed.ok) {
|
|
266
|
+
fx.routeFailure = routed
|
|
249
267
|
return
|
|
250
268
|
}
|
|
251
269
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
buildResolveContext(config, request),
|
|
266
|
-
opKind,
|
|
267
|
-
{
|
|
268
|
-
readQuery: fx.parsedQuery,
|
|
269
|
-
writeBody: isPlainObject(request.body)
|
|
270
|
-
? (request.body as Record<string, unknown>)
|
|
271
|
-
: undefined,
|
|
272
|
-
},
|
|
273
|
-
() => {
|
|
274
|
-
if (!fx.parsedQuery) fx.parsedQuery = {}
|
|
275
|
-
return fx.parsedQuery
|
|
276
|
-
},
|
|
277
|
-
() => {
|
|
278
|
-
if (!isPlainObject(request.body)) {
|
|
279
|
-
;(request as unknown as { body: unknown }).body = {}
|
|
280
|
-
}
|
|
281
|
-
return request.body as Record<string, unknown>
|
|
282
|
-
},
|
|
283
|
-
)
|
|
284
|
-
}
|
|
270
|
+
if (routed.variantKey !== undefined) fx.guardVariantKey = routed.variantKey
|
|
271
|
+
|
|
272
|
+
const guard = await prepareGuardOperation(routed, {
|
|
273
|
+
guardShape: opConfig.guardShape,
|
|
274
|
+
opKind,
|
|
275
|
+
policy: { dropGuard, validateResolvedShapes: false },
|
|
276
|
+
getContext: typeof config.resolveContext === 'function' ? fx.getContext : undefined,
|
|
277
|
+
args: queryChannel(request),
|
|
278
|
+
writeArgs: bodyChannel(request),
|
|
279
|
+
})
|
|
280
|
+
if (!guard.ok) {
|
|
281
|
+
fx.guardFailure = guard
|
|
282
|
+
return
|
|
285
283
|
}
|
|
284
|
+
|
|
285
|
+
fx.guardShape = guard.guardShape
|
|
286
286
|
}
|
|
287
287
|
}
|
|
288
288
|
|
|
@@ -423,12 +423,8 @@ export async function ${routerFunctionName}<TCtx = unknown, TPrisma extends Pris
|
|
|
423
423
|
if (await runHooks(opConfig.operationBefore, request, reply)) return
|
|
424
424
|
|
|
425
425
|
const fx = request as FastifyExtended
|
|
426
|
-
if (fx.
|
|
427
|
-
|
|
428
|
-
400,
|
|
429
|
-
formatGuardVariantResolutionError(fx.guardVariantFailure),
|
|
430
|
-
)
|
|
431
|
-
}
|
|
426
|
+
if (fx.routeFailure) settleStage(fx.routeFailure)
|
|
427
|
+
if (fx.guardFailure) settleStage(fx.guardFailure)
|
|
432
428
|
|
|
433
429
|
const key = fx.guardVariantKey
|
|
434
430
|
const variantHooks =
|
|
@@ -455,12 +451,8 @@ export async function ${routerFunctionName}<TCtx = unknown, TPrisma extends Pris
|
|
|
455
451
|
if (await runHooks(opConfig.operationBefore, request, reply)) return
|
|
456
452
|
|
|
457
453
|
const fx = request as FastifyExtended
|
|
458
|
-
if (fx.
|
|
459
|
-
|
|
460
|
-
400,
|
|
461
|
-
formatGuardVariantResolutionError(fx.guardVariantFailure),
|
|
462
|
-
)
|
|
463
|
-
}
|
|
454
|
+
if (fx.routeFailure) settleStage(fx.routeFailure)
|
|
455
|
+
if (fx.guardFailure) settleStage(fx.guardFailure)
|
|
464
456
|
|
|
465
457
|
const key = fx.guardVariantKey
|
|
466
458
|
const variantHooks =
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateRouterFastify.js","sourceRoot":"","sources":["../../src/generators/generateRouterFastify.ts"],"names":[],"mappings":";;AAyFA,
|
|
1
|
+
{"version":3,"file":"generateRouterFastify.js","sourceRoot":"","sources":["../../src/generators/generateRouterFastify.ts"],"names":[],"mappings":";;AAyFA,sEAucC;AA/hBD,uEAAmE;AAEnE,kDAA8C;AAE9C,oDAAqE;AACrE,uEAAiE;AAEjE,SAAS,QAAQ,CAAC,MAAc;IAC9B,IAAI,CAAC,MAAM;QAAE,OAAO,iBAAiB,CAAA;IACrC,OAAO,iBA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