prisma-generator-express 1.66.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 +188 -3432
- package/dist/client/encodeQueryParams.js +3 -1
- package/dist/client/encodeQueryParams.js.map +1 -1
- package/dist/copy/misc.d.ts +1 -0
- package/dist/copy/misc.js +14 -0
- package/dist/copy/misc.js.map +1 -1
- package/dist/copy/operationDefinitions.d.ts +4 -1
- package/dist/copy/operationDefinitions.js +133 -9
- package/dist/copy/operationDefinitions.js.map +1 -1
- package/dist/generators/generateFastifyHandler.d.ts +2 -2
- package/dist/generators/generateFastifyHandler.js +68 -12
- package/dist/generators/generateFastifyHandler.js.map +1 -1
- package/dist/generators/generateHonoHandler.d.ts +2 -2
- package/dist/generators/generateHonoHandler.js +53 -6
- 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/generateImportPrismaStatement.d.ts +1 -0
- package/dist/generators/generateImportPrismaStatement.js +6 -0
- package/dist/generators/generateImportPrismaStatement.js.map +1 -1
- 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 +29 -15
- package/dist/generators/generateModelMetadata.js.map +1 -1
- package/dist/generators/generateOperationCore.d.ts +3 -3
- package/dist/generators/generateOperationCore.js +51 -16
- package/dist/generators/generateOperationCore.js.map +1 -1
- package/dist/generators/generateRelationMeta.js +2 -2
- package/dist/generators/generateRelationMeta.js.map +1 -1
- package/dist/generators/generateRouteConfigType.d.ts +2 -2
- package/dist/generators/generateRouteConfigType.js +29 -11
- package/dist/generators/generateRouteConfigType.js.map +1 -1
- package/dist/generators/generateRouter.d.ts +7 -4
- package/dist/generators/generateRouter.js +164 -65
- package/dist/generators/generateRouter.js.map +1 -1
- package/dist/generators/generateRouterFastify.d.ts +7 -4
- package/dist/generators/generateRouterFastify.js +106 -86
- package/dist/generators/generateRouterFastify.js.map +1 -1
- package/dist/generators/generateRouterHono.d.ts +7 -4
- package/dist/generators/generateRouterHono.js +59 -414
- 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/generateUnifiedDocs.d.ts +1 -1
- package/dist/generators/generateUnifiedDocs.js +15 -8
- package/dist/generators/generateUnifiedDocs.js.map +1 -1
- package/dist/generators/generateUnifiedHandler.d.ts +2 -2
- package/dist/generators/generateUnifiedHandler.js +42 -18
- package/dist/generators/generateUnifiedHandler.js.map +1 -1
- package/dist/guardOptions.js +12 -7
- package/dist/guardOptions.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +123 -4
- package/dist/index.js.map +1 -1
- package/dist/utils/copyFiles.d.ts +1 -1
- package/dist/utils/copyFiles.js +42 -7
- package/dist/utils/copyFiles.js.map +1 -1
- package/dist/utils/importExt.d.ts +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/pathCasing.d.ts +4 -0
- package/dist/utils/pathCasing.js +40 -0
- package/dist/utils/pathCasing.js.map +1 -0
- package/dist/utils/resolveImportStyle.js +13 -5
- package/dist/utils/resolveImportStyle.js.map +1 -1
- package/dist/utils/writeFileSafely.js +6 -0
- package/dist/utils/writeFileSafely.js.map +1 -1
- package/package.json +22 -5
- package/src/bin.ts +1 -1
- package/src/client/encodeQueryParams.ts +39 -35
- package/src/constants.ts +1 -1
- package/src/copy/autoIncludePlanner.ts +93 -27
- package/src/copy/autoIncludePlannerGuarded.ts +171 -48
- package/src/copy/autoIncludeRuntimeGuarded.ts +25 -7
- package/src/copy/buildModelOpenApi.ts +229 -176
- package/src/copy/concurrency.ts +10 -8
- package/src/copy/docsRenderer.ts +14 -15
- package/src/copy/errorMapper.ts +68 -19
- package/src/copy/guardHelpers.ts +30 -9
- package/src/copy/materializedCount.ts +17 -5
- package/src/copy/mcpRuntime.ts +1017 -0
- package/src/copy/misc.ts +88 -68
- package/src/copy/ndjson.ts +72 -0
- package/src/copy/operationDefinitions.ts +177 -25
- package/src/copy/operationPipeline.ts +267 -0
- package/src/copy/operationRuntime.ts +124 -26
- package/src/copy/operationSchemas.ts +2773 -0
- package/src/copy/pagination.ts +35 -13
- package/src/copy/parseQueryParams.ts +47 -48
- package/src/copy/projectionDefaults.ts +6 -4
- package/src/copy/routeConfig.express.ts +16 -4
- package/src/copy/routeConfig.fastify.ts +16 -2
- package/src/copy/routeConfig.hono.ts +46 -14
- package/src/copy/routeConfig.ts +175 -40
- package/src/copy/routerParts.ts +524 -0
- package/src/copy/scalarTypes.ts +7 -2
- package/src/copy/sse.ts +72 -19
- package/src/generators/generateFastifyHandler.ts +77 -20
- package/src/generators/generateHonoHandler.ts +62 -14
- package/src/generators/generateHonoOpenApi.ts +93 -0
- package/src/generators/generateImportPrismaStatement.ts +93 -81
- package/src/generators/generateMcpApp.ts +133 -0
- package/src/generators/generateModelMcp.ts +208 -0
- package/src/generators/generateModelMetadata.ts +40 -22
- package/src/generators/generateOperationCore.ts +81 -33
- package/src/generators/generateRelationMeta.ts +23 -11
- package/src/generators/generateRouteConfigType.ts +39 -13
- package/src/generators/generateRouter.ts +171 -68
- package/src/generators/generateRouterFastify.ts +117 -89
- package/src/generators/generateRouterHono.ts +84 -464
- package/src/generators/generateRouterPartsHono.ts +220 -0
- package/src/generators/generateUnifiedDocs.ts +29 -8
- package/src/generators/generateUnifiedHandler.ts +47 -24
- package/src/generators/generateUnifiedScalarUI.ts +1 -1
- package/src/guardOptions.ts +105 -92
- package/src/index.ts +161 -6
- package/src/utils/copyFiles.ts +235 -168
- package/src/utils/importExt.ts +2 -2
- package/src/utils/mcpGate.ts +157 -0
- package/src/utils/pathCasing.ts +42 -0
- package/src/utils/resolveImportStyle.ts +37 -15
- package/src/utils/strings.ts +2 -2
- package/src/utils/writeFileSafely.ts +12 -2
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.generateFastifyRouterFunction = generateFastifyRouterFunction;
|
|
4
4
|
const generateRouteConfigType_1 = require("./generateRouteConfigType");
|
|
5
5
|
const importExt_1 = require("../utils/importExt");
|
|
6
|
+
const pathCasing_1 = require("../utils/pathCasing");
|
|
6
7
|
const operationDefinitions_1 = require("../copy/operationDefinitions");
|
|
7
8
|
function pathExpr(suffix) {
|
|
8
9
|
if (!suffix)
|
|
@@ -49,11 +50,11 @@ function emitReadOp(meta, modelName) {
|
|
|
49
50
|
const opKind = opKindFor(meta.name);
|
|
50
51
|
const postReadLine = meta.supportsPostRead
|
|
51
52
|
? meta.name === 'findMany'
|
|
52
|
-
? ` if (
|
|
53
|
+
? ` if (resolvePostReadsEnabled(config.disablePostReads, opConfig.disablePostReads)) {
|
|
53
54
|
const postPath = basePath ? \`\${basePath}/read\` : '/read'
|
|
54
55
|
instance.post(postPath, handleRead(opConfig, ${handlerName}, parseBodyAsQueryHook, '${opKind}'))
|
|
55
56
|
}`
|
|
56
|
-
: ` if (
|
|
57
|
+
: ` if (resolvePostReadsEnabled(config.disablePostReads, opConfig.disablePostReads)) instance.post(path, handleRead(opConfig, ${handlerName}, parseBodyAsQueryHook, '${opKind}'))`
|
|
57
58
|
: '';
|
|
58
59
|
return ` if (isEnabled(config.${meta.configKey})) {
|
|
59
60
|
const opConfig = opFor('${meta.configKey}')
|
|
@@ -73,19 +74,18 @@ function emitWriteOp(meta, modelName) {
|
|
|
73
74
|
instance.${meta.method}(path, handleWrite(opConfig, ${handlerName}, '${opKind}'))
|
|
74
75
|
}`;
|
|
75
76
|
}
|
|
76
|
-
function generateFastifyRouterFunction({ model, enums, guardShapesImport, importStyle, writeStrategy, findManyPaginatedMode, dropGuard, }) {
|
|
77
|
+
function generateFastifyRouterFunction({ model, enums, guardShapesImport, clientImport, importStyle, writeStrategy, findManyPaginatedMode, dropGuard, pathCase, }) {
|
|
77
78
|
const ext = (0, importExt_1.importExt)(importStyle);
|
|
78
79
|
const modelName = model.name;
|
|
79
|
-
const
|
|
80
|
-
const routerFunctionName = `${
|
|
81
|
-
const handlerImports = operationDefinitions_1.OPERATION_METADATA
|
|
82
|
-
.map((m) => ` ${modelName}${m.name.charAt(0).toUpperCase() + m.name.slice(1)},`)
|
|
83
|
-
.join('\n');
|
|
80
|
+
const modelSegment = (0, pathCasing_1.modelPathSegment)(model.name, pathCase);
|
|
81
|
+
const routerFunctionName = `${model.name}Router`;
|
|
82
|
+
const handlerImports = operationDefinitions_1.OPERATION_METADATA.map((m) => ` ${modelName}${m.name.charAt(0).toUpperCase() + m.name.slice(1)},`).join('\n');
|
|
84
83
|
const readOps = operationDefinitions_1.OPERATION_METADATA.filter((m) => m.kind === 'read');
|
|
85
|
-
const writeOps = operationDefinitions_1.OPERATION_METADATA.filter((m) => m.kind === 'write' || m.kind === 'batch')
|
|
86
|
-
.filter((m) => m.name !== 'updateEach');
|
|
84
|
+
const writeOps = operationDefinitions_1.OPERATION_METADATA.filter((m) => m.kind === 'write' || m.kind === 'batch').filter((m) => m.name !== 'updateEach');
|
|
87
85
|
const readOpBlocks = readOps.map((m) => emitReadOp(m, modelName)).join('\n\n');
|
|
88
|
-
const writeOpBlocks = writeOps
|
|
86
|
+
const writeOpBlocks = writeOps
|
|
87
|
+
.map((m) => emitWriteOp(m, modelName))
|
|
88
|
+
.join('\n\n');
|
|
89
89
|
return `import type { FastifyInstance, FastifyRequest, FastifyReply, FastifyError } from 'fastify'
|
|
90
90
|
import { startQueryBuilder } from '../queryBuilder${ext}'
|
|
91
91
|
import {
|
|
@@ -96,35 +96,49 @@ import type {
|
|
|
96
96
|
FastifyHookHandler,
|
|
97
97
|
FindManyPaginatedMode,
|
|
98
98
|
PaginationConfig,
|
|
99
|
+
PrismaClientLike,
|
|
100
|
+
QueryBuilderConfig,
|
|
99
101
|
} from '../routeConfig.target${ext}'
|
|
100
102
|
import { parseQueryParams } from '../parseQueryParams${ext}'
|
|
101
|
-
import { sanitizeKeys, normalizePrefix, getEnv, isPlainObject } from '../misc${ext}'
|
|
103
|
+
import { sanitizeKeys, normalizePrefix, getEnv, isPlainObject, resolveDropGuardEnv } from '../misc${ext}'
|
|
102
104
|
import { buildModelOpenApi } from '../buildModelOpenApi${ext}'
|
|
103
105
|
import {
|
|
104
106
|
normalizeOperation,
|
|
105
|
-
resolveOperationVariantKey,
|
|
106
107
|
validateCountSourceWhere,
|
|
107
108
|
validateOperationConfig,
|
|
108
109
|
validateUpdateEachConfig,
|
|
110
|
+
resolvePostReadsEnabled,
|
|
111
|
+
warnIfUnguardedRoutes,
|
|
109
112
|
} from '../routeConfig${ext}'
|
|
113
|
+
import type { RuntimeOperationOverride } from '../operationRuntime${ext}'
|
|
110
114
|
import type { NormalizedOperationConfig } from '../routeConfig${ext}'
|
|
111
115
|
import type { OperationContext } from '../operationRuntime${ext}'
|
|
112
116
|
import { transformResult } from '../operationRuntime${ext}'
|
|
113
117
|
import { HttpError, mapError } from '../errorMapper${ext}'
|
|
114
|
-
import { formatGuardVariantResolutionError } from '../guardVariantError${ext}'
|
|
115
|
-
import type { GuardVariantResolution } from '../guardVariantRouting${ext}'
|
|
116
118
|
import { mergePaginationConfig } from '../pagination${ext}'
|
|
117
|
-
import {
|
|
119
|
+
import {
|
|
120
|
+
routeOperation,
|
|
121
|
+
prepareGuardOperation,
|
|
122
|
+
settleStage,
|
|
123
|
+
classifyError,
|
|
124
|
+
memoizeContext,
|
|
125
|
+
type ArgsChannel,
|
|
126
|
+
} from '../operationPipeline${ext}'
|
|
118
127
|
import type { OpKind } from '../projectionDefaults${ext}'
|
|
119
128
|
import { MODEL_FIELDS, MODEL_ENUMS } from './${modelName}Metadata${ext}'
|
|
120
129
|
|
|
121
|
-
${(0, generateRouteConfigType_1.generateRouteConfigType)(modelName, 'FastifyHookHandler', guardShapesImport, importStyle, 'fastify')}
|
|
130
|
+
${(0, generateRouteConfigType_1.generateRouteConfigType)(modelName, 'FastifyHookHandler', guardShapesImport, importStyle, 'fastify', clientImport)}
|
|
122
131
|
const _env = getEnv()
|
|
123
132
|
|
|
124
133
|
const FIND_MANY_PAGINATED_MODE: FindManyPaginatedMode = '${findManyPaginatedMode}'
|
|
125
|
-
|
|
134
|
+
// Fixed at generation time. The environment bypass is resolved per hook,
|
|
135
|
+
// because it requires the configuration's explicit \`allowE2EGuardBypass: true\`:
|
|
136
|
+
// false or omitted means the environment cannot drop guards for this target.
|
|
137
|
+
const DROP_GUARD = ${dropGuard}
|
|
126
138
|
|
|
127
139
|
type OperationConfigLike = {
|
|
140
|
+
authorize?: FastifyHookHandler
|
|
141
|
+
override?: RuntimeOperationOverride
|
|
128
142
|
before?: FastifyHookHandler[]
|
|
129
143
|
after?: FastifyHookHandler[]
|
|
130
144
|
shape?: unknown
|
|
@@ -152,26 +166,51 @@ type FastifyExtended = FastifyRequest & {
|
|
|
152
166
|
routeConfig?: { pagination?: PaginationConfig }
|
|
153
167
|
guardShape?: Record<string, unknown>
|
|
154
168
|
guardCaller?: string
|
|
169
|
+
operationOverride?: RuntimeOperationOverride
|
|
170
|
+
getContext?: () => Promise<unknown>
|
|
155
171
|
guardVariantKey?: string
|
|
156
|
-
|
|
172
|
+
routeFailure?: { ok: false; failure: HttpError }
|
|
173
|
+
guardFailure?: { ok: false; failure: HttpError }
|
|
157
174
|
resultData?: unknown
|
|
158
175
|
resultStatus?: number
|
|
159
176
|
}
|
|
160
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
|
+
|
|
161
200
|
function normalizeFastifyOperation(
|
|
162
201
|
config: OperationConfigLike | undefined,
|
|
163
202
|
): NormalizedOp {
|
|
164
203
|
return normalizeOperation<FastifyHookHandler, FastifyHookHandler>(config)
|
|
165
204
|
}
|
|
166
205
|
|
|
167
|
-
function isQueryBuilderEnabled(config:
|
|
206
|
+
function isQueryBuilderEnabled(config: { queryBuilder?: QueryBuilderConfig | false }): boolean {
|
|
168
207
|
if (config.queryBuilder === false) return false
|
|
169
208
|
if (typeof config.queryBuilder === 'object' && config.queryBuilder.enabled === false) return false
|
|
170
209
|
if (_env.NODE_ENV === 'production') return false
|
|
171
210
|
return true
|
|
172
211
|
}
|
|
173
212
|
|
|
174
|
-
function getQueryBuilderConfig(config:
|
|
213
|
+
function getQueryBuilderConfig(config: { queryBuilder?: QueryBuilderConfig | false }) {
|
|
175
214
|
if (config.queryBuilder === false) return null
|
|
176
215
|
if (typeof config.queryBuilder === 'object') return config.queryBuilder
|
|
177
216
|
return {}
|
|
@@ -192,21 +231,26 @@ function parseBodyAsQueryHook(request: FastifyRequest): void {
|
|
|
192
231
|
(request as FastifyExtended).parsedQuery = sanitizeKeys(body as Record<string, unknown>)
|
|
193
232
|
}
|
|
194
233
|
|
|
195
|
-
function
|
|
196
|
-
config: ${modelName}RouteConfig,
|
|
197
|
-
request: FastifyRequest,
|
|
198
|
-
): (() => unknown | Promise<unknown>) | undefined {
|
|
199
|
-
if (typeof config.resolveContext !== 'function') return undefined
|
|
200
|
-
return () => (config.resolveContext as (r: FastifyRequest) => unknown | Promise<unknown>)(request)
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
function makeShapeHook(
|
|
204
|
-
config: ${modelName}RouteConfig,
|
|
234
|
+
function makeShapeHook<TCtx, TPrisma extends PrismaClientLike>(
|
|
235
|
+
config: ${modelName}RouteConfig<TCtx, TPrisma>,
|
|
205
236
|
opConfig: NormalizedOp,
|
|
206
237
|
opKind: OpKind,
|
|
207
238
|
): (request: FastifyRequest) => Promise<void> {
|
|
239
|
+
/**
|
|
240
|
+
* The environment bypass, and it is OPT-IN for this target.
|
|
241
|
+
*
|
|
242
|
+
* \`PGE_DROP_GUARD=true\` (deprecated alias \`E2E=true\`) downgrading enforcement
|
|
243
|
+
* in a deployed environment is a real hazard: on most platforms it is an
|
|
244
|
+
* ordinary config var, set on staging, copied forward, flagged as
|
|
245
|
+
* security-relevant nowhere. It is honoured only when this configuration
|
|
246
|
+
* states \`allowE2EGuardBypass: true\` — false or omitted means the environment
|
|
247
|
+
* cannot drop guards. The generation-time \`DROP_GUARD\` literal is unchanged.
|
|
248
|
+
*/
|
|
249
|
+
const dropGuard = DROP_GUARD || (config.allowE2EGuardBypass === true && resolveDropGuardEnv(_env))
|
|
208
250
|
return async (request: FastifyRequest) => {
|
|
209
251
|
const fx = request as FastifyExtended
|
|
252
|
+
fx.operationOverride = opConfig.override
|
|
253
|
+
fx.getContext = memoizeContext(() => config.resolveContext?.(request))
|
|
210
254
|
const merged = mergePaginationConfig(config.pagination, opConfig.pagination)
|
|
211
255
|
if (merged) fx.routeConfig = { pagination: merged }
|
|
212
256
|
|
|
@@ -217,46 +261,28 @@ function makeShapeHook(
|
|
|
217
261
|
?? undefined
|
|
218
262
|
if (typeof caller === 'string') fx.guardCaller = caller
|
|
219
263
|
|
|
220
|
-
const
|
|
221
|
-
if (!
|
|
222
|
-
fx.
|
|
264
|
+
const routed = routeOperation({ guardRouting: opConfig.guardRouting, caller })
|
|
265
|
+
if (!routed.ok) {
|
|
266
|
+
fx.routeFailure = routed
|
|
223
267
|
return
|
|
224
268
|
}
|
|
225
269
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
buildResolveContext(config, request),
|
|
240
|
-
opKind,
|
|
241
|
-
{
|
|
242
|
-
readQuery: fx.parsedQuery,
|
|
243
|
-
writeBody: isPlainObject(request.body)
|
|
244
|
-
? (request.body as Record<string, unknown>)
|
|
245
|
-
: undefined,
|
|
246
|
-
},
|
|
247
|
-
() => {
|
|
248
|
-
if (!fx.parsedQuery) fx.parsedQuery = {}
|
|
249
|
-
return fx.parsedQuery
|
|
250
|
-
},
|
|
251
|
-
() => {
|
|
252
|
-
if (!isPlainObject(request.body)) {
|
|
253
|
-
;(request as unknown as { body: unknown }).body = {}
|
|
254
|
-
}
|
|
255
|
-
return request.body as Record<string, unknown>
|
|
256
|
-
},
|
|
257
|
-
)
|
|
258
|
-
}
|
|
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
|
|
259
283
|
}
|
|
284
|
+
|
|
285
|
+
fx.guardShape = guard.guardShape
|
|
260
286
|
}
|
|
261
287
|
}
|
|
262
288
|
|
|
@@ -289,7 +315,7 @@ function sendError(reply: FastifyReply, error: unknown): void {
|
|
|
289
315
|
reply.code(httpError.status).send({ message: httpError.message })
|
|
290
316
|
}
|
|
291
317
|
|
|
292
|
-
export async function ${routerFunctionName}<TCtx = unknown, TPrisma =
|
|
318
|
+
export async function ${routerFunctionName}<TCtx = unknown, TPrisma extends PrismaClientLike = PrismaClientLike>(
|
|
293
319
|
fastify: FastifyInstance,
|
|
294
320
|
config: ${modelName}RouteConfig<TCtx, TPrisma> = {},
|
|
295
321
|
) {
|
|
@@ -301,6 +327,7 @@ export async function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(
|
|
|
301
327
|
|
|
302
328
|
await fastify.register(async (instance) => {
|
|
303
329
|
const isEnabled = (value: unknown): boolean => value !== false && !!(config.enableAll || value)
|
|
330
|
+
warnIfUnguardedRoutes('${modelName}', ['findMany', 'findUnique', 'findUniqueOrThrow', 'findFirst', 'findFirstOrThrow', 'findManyPaginated', 'count', 'aggregate', 'groupBy', 'create', 'createMany', 'createManyAndReturn', 'update', 'updateMany', 'updateManyAndReturn', 'upsert', 'delete', 'deleteMany'], config, isEnabled)
|
|
304
331
|
|
|
305
332
|
const opFor = (key: string): NormalizedOp => {
|
|
306
333
|
const raw = (config as unknown as Record<string, unknown>)[key] as OperationConfigLike | undefined
|
|
@@ -309,7 +336,7 @@ export async function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(
|
|
|
309
336
|
}
|
|
310
337
|
|
|
311
338
|
const customPrefix = normalizePrefix(config.customUrlPrefix || '')
|
|
312
|
-
const modelPrefix = config.addModelPrefix !== false ? '/${
|
|
339
|
+
const modelPrefix = config.addModelPrefix !== false ? '/${modelSegment}' : ''
|
|
313
340
|
const basePath = customPrefix + modelPrefix
|
|
314
341
|
|
|
315
342
|
const openApiDisabled = config.disableOpenApi === true
|
|
@@ -318,7 +345,6 @@ export async function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(
|
|
|
318
345
|
|| _env.DISABLE_OPENAPI === 'true'
|
|
319
346
|
))
|
|
320
347
|
|
|
321
|
-
const postReadsEnabled = !config.disablePostReads
|
|
322
348
|
|
|
323
349
|
let _openApiJsonCache: unknown = undefined
|
|
324
350
|
const getOpenApiJson = (): unknown => {
|
|
@@ -327,8 +353,8 @@ export async function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(
|
|
|
327
353
|
'${modelName}',
|
|
328
354
|
MODEL_FIELDS as unknown as Parameters<typeof buildModelOpenApi>[1],
|
|
329
355
|
MODEL_ENUMS as unknown as Parameters<typeof buildModelOpenApi>[2],
|
|
330
|
-
config,
|
|
331
|
-
{ format: 'json', writeStrategy: '${writeStrategy}' },
|
|
356
|
+
config as unknown as Parameters<typeof buildModelOpenApi>[3],
|
|
357
|
+
{ format: 'json', writeStrategy: '${writeStrategy}', pathSegment: '${modelSegment}' },
|
|
332
358
|
)
|
|
333
359
|
}
|
|
334
360
|
return _openApiJsonCache
|
|
@@ -340,8 +366,8 @@ export async function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(
|
|
|
340
366
|
'${modelName}',
|
|
341
367
|
MODEL_FIELDS as unknown as Parameters<typeof buildModelOpenApi>[1],
|
|
342
368
|
MODEL_ENUMS as unknown as Parameters<typeof buildModelOpenApi>[2],
|
|
343
|
-
config,
|
|
344
|
-
{ format: 'yaml', writeStrategy: '${writeStrategy}' },
|
|
369
|
+
config as unknown as Parameters<typeof buildModelOpenApi>[3],
|
|
370
|
+
{ format: 'yaml', writeStrategy: '${writeStrategy}', pathSegment: '${modelSegment}' },
|
|
345
371
|
) as string
|
|
346
372
|
}
|
|
347
373
|
return _openApiYamlCache
|
|
@@ -391,17 +417,14 @@ export async function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(
|
|
|
391
417
|
opKind: OpKind,
|
|
392
418
|
) => async (request: FastifyRequest, reply: FastifyReply) => {
|
|
393
419
|
try {
|
|
420
|
+
if (await runHooks(opConfig.authorize ? [opConfig.authorize] : [], request, reply)) return
|
|
394
421
|
parseFn(request)
|
|
395
422
|
await makeShapeHook(config, opConfig, opKind)(request)
|
|
396
423
|
if (await runHooks(opConfig.operationBefore, request, reply)) return
|
|
397
424
|
|
|
398
425
|
const fx = request as FastifyExtended
|
|
399
|
-
if (fx.
|
|
400
|
-
|
|
401
|
-
400,
|
|
402
|
-
formatGuardVariantResolutionError(fx.guardVariantFailure),
|
|
403
|
-
)
|
|
404
|
-
}
|
|
426
|
+
if (fx.routeFailure) settleStage(fx.routeFailure)
|
|
427
|
+
if (fx.guardFailure) settleStage(fx.guardFailure)
|
|
405
428
|
|
|
406
429
|
const key = fx.guardVariantKey
|
|
407
430
|
const variantHooks =
|
|
@@ -423,16 +446,13 @@ export async function ${routerFunctionName}<TCtx = unknown, TPrisma = any>(
|
|
|
423
446
|
opKind: OpKind,
|
|
424
447
|
) => async (request: FastifyRequest, reply: FastifyReply) => {
|
|
425
448
|
try {
|
|
449
|
+
if (await runHooks(opConfig.authorize ? [opConfig.authorize] : [], request, reply)) return
|
|
426
450
|
await makeShapeHook(config, opConfig, opKind)(request)
|
|
427
451
|
if (await runHooks(opConfig.operationBefore, request, reply)) return
|
|
428
452
|
|
|
429
453
|
const fx = request as FastifyExtended
|
|
430
|
-
if (fx.
|
|
431
|
-
|
|
432
|
-
400,
|
|
433
|
-
formatGuardVariantResolutionError(fx.guardVariantFailure),
|
|
434
|
-
)
|
|
435
|
-
}
|
|
454
|
+
if (fx.routeFailure) settleStage(fx.routeFailure)
|
|
455
|
+
if (fx.guardFailure) settleStage(fx.guardFailure)
|
|
436
456
|
|
|
437
457
|
const key = fx.guardVariantKey
|
|
438
458
|
const variantHooks =
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateRouterFastify.js","sourceRoot":"","sources":["../../src/generators/generateRouterFastify.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"generateRouterFastify.js","sourceRoot":"","sources":["../../src/generators/generateRouterFastify.ts"],"names":[],"mappings":";;A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C0C,GAAG;yDAC9C,GAAG;;;;;;;;wBAQpC,GAAG;oEACyC,GAAG;gEACP,GAAG;4DACP,GAAG;sDACT,GAAG;qDACJ,GAAG;sDACF,GAAG;;;;;;;;8BAQ3B,GAAG;oDACmB,GAAG;+CACR,SAAS,WAAW,GAAG;;EAEpE,IAAA,iDAAuB,EAAC,SAAS,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,WAAW,EAAE,SAAS,EAAE,YAAY,CAAC;;;2DAGxD,qBAAqB;;;;qBAI3D,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;YAkGlB,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAmFG,kBAAkB;;YAE9B,SAAS;;8DAEyC,SAAS;;;OAGhE,SAAS;;;;;2BAKW,SAAS;;;;sCAIE,SAAS;;;;;8DAKe,YAAY;;;;;;;;;;;;;;aAc7D,SAAS;;;;8CAIwB,aAAa,oBAAoB,YAAY;;;;;;;;;aAS9E,SAAS;;;;8CAIwB,aAAa,oBAAoB,YAAY;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAqGzF,YAAY;;EAEZ,aAAa;;;;iDAIkC,SAAS;;;;cAI5C,SAAS;;;;;;;;;;;;kBAYL,SAAS;;;;;;;;;;CAU1B,CAAA;AACD,CAAC"}
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import { DMMF } from '@prisma/generator-helper';
|
|
2
|
-
import { ImportStyle } from '../utils/resolveImportStyle';
|
|
3
|
-
import { WriteStrategy } from '../constants';
|
|
4
|
-
|
|
1
|
+
import type { DMMF } from '@prisma/generator-helper';
|
|
2
|
+
import type { ImportStyle } from '../utils/resolveImportStyle';
|
|
3
|
+
import type { WriteStrategy } from '../constants';
|
|
4
|
+
import { type PathCase } from '../utils/pathCasing';
|
|
5
|
+
export declare function generateHonoRouterFunction({ model, enums, guardShapesImport, clientImport, importStyle, writeStrategy, dropGuard, pathCase, }: {
|
|
5
6
|
model: DMMF.Model;
|
|
6
7
|
enums: DMMF.DatamodelEnum[];
|
|
7
8
|
guardShapesImport: string | null;
|
|
9
|
+
clientImport?: string;
|
|
8
10
|
importStyle: ImportStyle;
|
|
9
11
|
writeStrategy: WriteStrategy;
|
|
10
12
|
dropGuard: boolean;
|
|
13
|
+
pathCase: PathCase;
|
|
11
14
|
}): string;
|