prisma-generator-express 1.45.0 → 1.46.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 +155 -30
- package/dist/client/encodeQueryParams.js +4 -0
- package/dist/client/encodeQueryParams.js.map +1 -1
- package/dist/copy/misc.d.ts +4 -2
- package/dist/copy/misc.js +35 -24
- package/dist/copy/misc.js.map +1 -1
- package/dist/generators/generateFastifyHandler.js +2 -4
- package/dist/generators/generateFastifyHandler.js.map +1 -1
- package/dist/generators/generateHonoHandler.js +2 -4
- package/dist/generators/generateHonoHandler.js.map +1 -1
- package/dist/generators/generateImportPrismaStatement.d.ts +0 -1
- package/dist/generators/generateImportPrismaStatement.js +2 -20
- package/dist/generators/generateImportPrismaStatement.js.map +1 -1
- package/dist/generators/generateOperationCore.js +1 -1
- package/dist/generators/generateQueryBuilderHelper.js +9 -0
- package/dist/generators/generateQueryBuilderHelper.js.map +1 -1
- package/dist/generators/generateRelationMeta.js +0 -10
- package/dist/generators/generateRelationMeta.js.map +1 -1
- package/dist/generators/generateRouteConfigType.js +33 -12
- package/dist/generators/generateRouteConfigType.js.map +1 -1
- package/dist/generators/generateRouter.d.ts +0 -1
- package/dist/generators/generateRouter.js +75 -70
- package/dist/generators/generateRouter.js.map +1 -1
- package/dist/generators/generateRouterFastify.js +83 -89
- package/dist/generators/generateRouterFastify.js.map +1 -1
- package/dist/generators/generateRouterHono.js +257 -237
- package/dist/generators/generateRouterHono.js.map +1 -1
- package/dist/generators/generateUnifiedDocs.d.ts +2 -2
- package/dist/generators/generateUnifiedDocs.js +90 -252
- package/dist/generators/generateUnifiedDocs.js.map +1 -1
- package/dist/generators/generateUnifiedHandler.js +2 -4
- package/dist/generators/generateUnifiedHandler.js.map +1 -1
- package/dist/index.js +16 -8
- package/dist/index.js.map +1 -1
- package/dist/utils/copyFiles.js +3 -2
- package/dist/utils/copyFiles.js.map +1 -1
- package/dist/utils/strings.d.ts +0 -1
- package/dist/utils/strings.js +0 -9
- package/dist/utils/strings.js.map +1 -1
- package/package.json +1 -1
- package/src/client/encodeQueryParams.ts +7 -15
- package/src/copy/autoIncludePlanner.ts +4 -17
- package/src/copy/autoIncludeRuntime.ts +11 -19
- package/src/copy/buildModelOpenApi.ts +11 -14
- package/src/copy/docsRenderer.ts +8 -14
- package/src/copy/misc.ts +28 -23
- package/src/copy/operationRuntime.ts +61 -43
- package/src/copy/parseQueryParams.ts +5 -14
- package/src/copy/routeConfig.express.ts +24 -18
- package/src/copy/routeConfig.fastify.ts +1 -1
- package/src/copy/routeConfig.hono.ts +34 -6
- package/src/copy/routeConfig.ts +2 -2
- package/src/generators/generateFastifyHandler.ts +2 -5
- package/src/generators/generateHonoHandler.ts +2 -5
- package/src/generators/generateImportPrismaStatement.ts +3 -35
- package/src/generators/generateOperationCore.ts +1 -1
- package/src/generators/generateQueryBuilderHelper.ts +9 -0
- package/src/generators/generateRelationMeta.ts +0 -10
- package/src/generators/generateRouteConfigType.ts +34 -10
- package/src/generators/generateRouter.ts +75 -71
- package/src/generators/generateRouterFastify.ts +83 -89
- package/src/generators/generateRouterHono.ts +257 -237
- package/src/generators/generateUnifiedDocs.ts +89 -267
- package/src/generators/generateUnifiedHandler.ts +2 -4
- package/src/index.ts +45 -14
- package/src/utils/copyFiles.ts +2 -2
- package/src/utils/strings.ts +0 -8
- package/src/copy/createOutputValidatorMiddleware.ts +0 -47
- package/src/copy/createValidatorMiddleware.ts +0 -62
- package/src/copy/transformZod.ts +0 -139
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { Request, Response, NextFunction } from 'express'
|
|
2
|
-
import { ZodSchema } from 'zod'
|
|
3
|
-
import { allow, forbid } from './transformZod'
|
|
4
|
-
|
|
5
|
-
export interface ValidatorOptions {
|
|
6
|
-
schema: ZodSchema<any>
|
|
7
|
-
allowedPaths?: string[]
|
|
8
|
-
forbiddenPaths?: string[]
|
|
9
|
-
target?: 'body' | 'query'
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Creates a middleware for validating request data using a Zod schema.
|
|
14
|
-
* @param {ValidatorOptions} options - The validation options.
|
|
15
|
-
* @param {ZodSchema<any>} options.schema - The Zod schema to validate the request data.
|
|
16
|
-
* @param {string[]} [options.allowedPaths] - Paths that are allowed in the request data. For example [`where.user.id`, `select.id`]
|
|
17
|
-
* @param {string[]} [options.forbiddenPaths] - Paths that are forbidden in the request data.
|
|
18
|
-
* @param {'body' | 'query'} [options.target='body'] - The part of the request to validate ('body' or 'query').
|
|
19
|
-
* @returns {function} Express middleware function.
|
|
20
|
-
*/
|
|
21
|
-
export function createValidatorMiddleware({
|
|
22
|
-
schema,
|
|
23
|
-
allowedPaths,
|
|
24
|
-
forbiddenPaths,
|
|
25
|
-
target = 'body',
|
|
26
|
-
}: ValidatorOptions) {
|
|
27
|
-
if (allowedPaths) {
|
|
28
|
-
schema = allow(schema, allowedPaths)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (forbiddenPaths) {
|
|
32
|
-
schema = forbid(schema, forbiddenPaths)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return (req: Request, res: Response, next: NextFunction) => {
|
|
36
|
-
let validationResult
|
|
37
|
-
|
|
38
|
-
if (target === 'query') {
|
|
39
|
-
validationResult = schema.safeParse(req.query)
|
|
40
|
-
} else {
|
|
41
|
-
validationResult = schema.safeParse(req.body)
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (!validationResult.success) {
|
|
45
|
-
const errors = validationResult.error.issues
|
|
46
|
-
return next({
|
|
47
|
-
status: 400,
|
|
48
|
-
message: 'Validation failed',
|
|
49
|
-
errors,
|
|
50
|
-
})
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
next()
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function removeTrailingSlash(path: string): string {
|
|
58
|
-
if (path === '/') {
|
|
59
|
-
return path
|
|
60
|
-
}
|
|
61
|
-
return path.replace(/\/+$/, '')
|
|
62
|
-
}
|
package/src/copy/transformZod.ts
DELETED
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import { get } from 'lodash'
|
|
2
|
-
import {
|
|
3
|
-
output,
|
|
4
|
-
z,
|
|
5
|
-
ZodError,
|
|
6
|
-
ZodObject,
|
|
7
|
-
ZodType,
|
|
8
|
-
} from 'zod'
|
|
9
|
-
|
|
10
|
-
function startsWith(str: string, prefix: string): boolean {
|
|
11
|
-
return str.slice(0, prefix.length) === prefix
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function every<T>(
|
|
15
|
-
array: T[],
|
|
16
|
-
callback: (value: T, index: number, array: T[]) => boolean,
|
|
17
|
-
): boolean {
|
|
18
|
-
for (let i = 0; i < array.length; i++) {
|
|
19
|
-
if (!callback(array[i], i, array)) {
|
|
20
|
-
return false
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return true
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function isKeyAllowed(key: string, allowedPaths: string[]): boolean {
|
|
27
|
-
return !every(
|
|
28
|
-
allowedPaths,
|
|
29
|
-
(path) =>
|
|
30
|
-
!startsWith(key.replace(/\[\d+\]/g, ''), path.replace(/\[\d+\]/g, '')) &&
|
|
31
|
-
!startsWith(path.replace(/\[\d+\]/g, ''), key.replace(/\[\d+\]/g, '')),
|
|
32
|
-
)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function allow<T extends ZodType>(
|
|
36
|
-
schema: T,
|
|
37
|
-
allowedPaths: string[],
|
|
38
|
-
) {
|
|
39
|
-
const schemaToTransform = schema instanceof z.ZodObject ? schema.strict() : schema
|
|
40
|
-
const rootSchema = schemaToTransform instanceof z.ZodObject ? schemaToTransform : undefined
|
|
41
|
-
|
|
42
|
-
return schemaToTransform.transform((data) => {
|
|
43
|
-
const flatData = flattenObject(data, '', rootSchema)
|
|
44
|
-
|
|
45
|
-
const disallowedPaths: string[] = []
|
|
46
|
-
|
|
47
|
-
for (const key of Object.keys(flatData)) {
|
|
48
|
-
if (!isKeyAllowed(key, allowedPaths)) {
|
|
49
|
-
disallowedPaths.push(key)
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (disallowedPaths.length > 0) {
|
|
54
|
-
throw createZodErrorFromPaths(disallowedPaths, 'Field is not allowed:')
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return data
|
|
58
|
-
})
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export function forbid<T extends ZodType>(
|
|
62
|
-
schema: T,
|
|
63
|
-
forbiddenPaths: string[],
|
|
64
|
-
) {
|
|
65
|
-
return schema.transform((data) => {
|
|
66
|
-
const forbiddenMatches: string[] = []
|
|
67
|
-
|
|
68
|
-
for (const forbiddenPath of forbiddenPaths) {
|
|
69
|
-
const value = get(data, forbiddenPath)
|
|
70
|
-
|
|
71
|
-
if (value !== undefined) {
|
|
72
|
-
forbiddenMatches.push(forbiddenPath)
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (forbiddenMatches.length > 0) {
|
|
77
|
-
throw createZodErrorFromPaths(forbiddenMatches, 'Field is forbidden:')
|
|
78
|
-
}
|
|
79
|
-
return data
|
|
80
|
-
})
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export function flattenObject<T>(
|
|
84
|
-
obj: Record<string, any> | output<T>,
|
|
85
|
-
prefix = '',
|
|
86
|
-
schema?: ZodObject<any>,
|
|
87
|
-
): Record<string, any> {
|
|
88
|
-
const result: Record<string, any> = {}
|
|
89
|
-
|
|
90
|
-
function flatten(current: any, prop: string, schema?: ZodObject<any>) {
|
|
91
|
-
if (Object(current) !== current) {
|
|
92
|
-
result[prop] = current
|
|
93
|
-
} else if (Array.isArray(current)) {
|
|
94
|
-
current.forEach((item, index) => {
|
|
95
|
-
flatten(item, `${prop}[]`, schema)
|
|
96
|
-
})
|
|
97
|
-
} else {
|
|
98
|
-
let isEmpty = true
|
|
99
|
-
for (const key in current) {
|
|
100
|
-
if (current.hasOwnProperty(key)) {
|
|
101
|
-
isEmpty = false
|
|
102
|
-
const currentSchema = schema?.shape[key]
|
|
103
|
-
if (
|
|
104
|
-
currentSchema instanceof z.ZodOptional &&
|
|
105
|
-
current[key] === undefined
|
|
106
|
-
) {
|
|
107
|
-
continue
|
|
108
|
-
}
|
|
109
|
-
flatten(
|
|
110
|
-
current[key],
|
|
111
|
-
prop ? `${prop}.${key}` : key,
|
|
112
|
-
currentSchema instanceof ZodObject ? currentSchema : undefined,
|
|
113
|
-
)
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
if (isEmpty) {
|
|
117
|
-
result[prop] = {}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
flatten(obj, prefix, schema)
|
|
123
|
-
return result
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function createZodErrorFromPaths(
|
|
127
|
-
disallowedPaths: string[],
|
|
128
|
-
errorMessage: string,
|
|
129
|
-
): ZodError {
|
|
130
|
-
const errors: z.core.$ZodIssue[] = []
|
|
131
|
-
for (const path of disallowedPaths) {
|
|
132
|
-
errors.push({
|
|
133
|
-
code: 'custom',
|
|
134
|
-
message: `${errorMessage} '${path}'`,
|
|
135
|
-
path: path.split('.'),
|
|
136
|
-
})
|
|
137
|
-
}
|
|
138
|
-
return new ZodError(errors)
|
|
139
|
-
}
|