prisma-generator-express 1.12.0 → 1.13.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 +51 -3
- package/dist/generator.js +2 -12
- package/dist/generator.js.map +1 -1
- package/dist/helpers/generateAggregate.js +13 -13
- package/dist/helpers/generateAggregate.js.map +1 -1
- package/dist/helpers/generateCount.js +12 -13
- package/dist/helpers/generateCount.js.map +1 -1
- package/dist/helpers/generateCreate.js +13 -15
- package/dist/helpers/generateCreate.js.map +1 -1
- package/dist/helpers/generateCreateMany.js +13 -15
- package/dist/helpers/generateCreateMany.js.map +1 -1
- package/dist/helpers/generateDelete.js +12 -15
- package/dist/helpers/generateDelete.js.map +1 -1
- package/dist/helpers/generateDeleteMany.js +13 -14
- package/dist/helpers/generateDeleteMany.js.map +1 -1
- package/dist/helpers/generateFindFirst.js +10 -15
- package/dist/helpers/generateFindFirst.js.map +1 -1
- package/dist/helpers/generateFindMany.js +10 -15
- package/dist/helpers/generateFindMany.js.map +1 -1
- package/dist/helpers/generateFindUnique.js +10 -15
- package/dist/helpers/generateFindUnique.js.map +1 -1
- package/dist/helpers/generateGroupBy.js +12 -13
- package/dist/helpers/generateGroupBy.js.map +1 -1
- package/dist/helpers/generateRouteFile.js +68 -35
- package/dist/helpers/generateRouteFile.js.map +1 -1
- package/dist/helpers/generateUpdate.js +12 -15
- package/dist/helpers/generateUpdate.js.map +1 -1
- package/dist/helpers/generateUpdateMany.js +12 -15
- package/dist/helpers/generateUpdateMany.js.map +1 -1
- package/dist/helpers/generateUpsert.js +12 -15
- package/dist/helpers/generateUpsert.js.map +1 -1
- package/dist/utils/copyFiles.js +26 -0
- package/dist/utils/copyFiles.js.map +1 -0
- package/package.json +4 -1
- package/src/copy/createOutputValidatorMiddleware.ts +44 -0
- package/src/copy/createValidatorMiddleware.ts +55 -0
- package/src/copy/encodeQueryParams.spec.ts +303 -0
- package/src/copy/encodeQueryParams.ts +44 -0
- package/src/copy/misc.spec.ts +62 -0
- package/src/copy/misc.ts +25 -0
- package/src/copy/parseQueryParams.spec.ts +187 -0
- package/src/copy/parseQueryParams.ts +42 -0
- package/src/copy/routeConfig.ts +34 -0
- package/src/copy/transformZod.spec.ts +556 -0
- package/src/copy/transformZod.ts +119 -0
- package/src/generator.ts +3 -13
- package/src/helpers/generateAggregate.ts +13 -13
- package/src/helpers/generateCount.ts +12 -13
- package/src/helpers/generateCreate.ts +14 -15
- package/src/helpers/generateCreateMany.ts +13 -15
- package/src/helpers/generateDelete.ts +13 -15
- package/src/helpers/generateDeleteMany.ts +14 -14
- package/src/helpers/generateFindFirst.ts +10 -15
- package/src/helpers/generateFindMany.ts +10 -15
- package/src/helpers/generateFindUnique.ts +10 -15
- package/src/helpers/generateGroupBy.ts +12 -13
- package/src/helpers/generateRouteFile.ts +68 -35
- package/src/helpers/generateUpdate.ts +13 -15
- package/src/helpers/generateUpdateMany.ts +13 -15
- package/src/helpers/generateUpsert.ts +13 -15
- package/src/utils/copyFiles.ts +27 -0
- package/dist/helpers/generateQsParser.js +0 -56
- package/dist/helpers/generateQsParser.js.map +0 -1
- package/dist/helpers/generateRouteConfigType.js +0 -34
- package/dist/helpers/generateRouteConfigType.js.map +0 -1
- package/src/helpers/generateQsParser.ts +0 -51
- package/src/helpers/generateRouteConfigType.ts +0 -29
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { encodeQueryParams } from './encodeQueryParams'
|
|
2
|
+
|
|
3
|
+
describe('Query Params Encoding', () => {
|
|
4
|
+
it('should encode simple key-value pairs correctly', () => {
|
|
5
|
+
const params = {
|
|
6
|
+
id: 1,
|
|
7
|
+
name: 'test',
|
|
8
|
+
}
|
|
9
|
+
const result = encodeQueryParams(params)
|
|
10
|
+
expect(result).toBe('id=1&name=test')
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it('should encode nested objects correctly', () => {
|
|
14
|
+
const params = {
|
|
15
|
+
select: {
|
|
16
|
+
id: true,
|
|
17
|
+
name: true,
|
|
18
|
+
},
|
|
19
|
+
}
|
|
20
|
+
const result = encodeQueryParams(params)
|
|
21
|
+
expect(result).toBe('select%5Bid%5D=true&select%5Bname%5D=true')
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('should encode arrays correctly', () => {
|
|
25
|
+
const params = {
|
|
26
|
+
ids: [1, 2, 3],
|
|
27
|
+
}
|
|
28
|
+
const result = encodeQueryParams(params)
|
|
29
|
+
expect(result).toBe('ids%5B0%5D=1&ids%5B1%5D=2&ids%5B2%5D=3')
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('should encode complex nested objects correctly', () => {
|
|
33
|
+
const params = {
|
|
34
|
+
select: {
|
|
35
|
+
id: true,
|
|
36
|
+
project_id: true,
|
|
37
|
+
list_id: true,
|
|
38
|
+
user_assignments: {
|
|
39
|
+
select: {
|
|
40
|
+
user: true,
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
tags_mappings: {
|
|
44
|
+
select: {
|
|
45
|
+
tag: true,
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
attachments: {
|
|
49
|
+
select: {
|
|
50
|
+
attachment: true,
|
|
51
|
+
},
|
|
52
|
+
where: {
|
|
53
|
+
is_image: true,
|
|
54
|
+
},
|
|
55
|
+
take: 100,
|
|
56
|
+
orderBy: {
|
|
57
|
+
created_at: 'desc',
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
rendered_description: true,
|
|
61
|
+
description: true,
|
|
62
|
+
created_at: true,
|
|
63
|
+
start_date: true,
|
|
64
|
+
reactions: true,
|
|
65
|
+
intervals: true,
|
|
66
|
+
column_id: true,
|
|
67
|
+
priority: true,
|
|
68
|
+
due_date: true,
|
|
69
|
+
column: true,
|
|
70
|
+
title: true,
|
|
71
|
+
order: true,
|
|
72
|
+
color: true,
|
|
73
|
+
},
|
|
74
|
+
where: {
|
|
75
|
+
id: 'task_id',
|
|
76
|
+
AND: [
|
|
77
|
+
{
|
|
78
|
+
OR: [{ to_delete: false }, { to_delete: null }],
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
},
|
|
82
|
+
}
|
|
83
|
+
const result = encodeQueryParams(params)
|
|
84
|
+
expect(result).toBe(
|
|
85
|
+
'select%5Bid%5D=true&select%5Bproject_id%5D=true&select%5Blist_id%5D=true&select%5Buser_assignments%5D%5Bselect%5D%5Buser%5D=true&select%5Btags_mappings%5D%5Bselect%5D%5Btag%5D=true&select%5Battachments%5D%5Bselect%5D%5Battachment%5D=true&select%5Battachments%5D%5Bwhere%5D%5Bis_image%5D=true&select%5Battachments%5D%5Btake%5D=100&select%5Battachments%5D%5BorderBy%5D%5Bcreated_at%5D=desc&select%5Brendered_description%5D=true&select%5Bdescription%5D=true&select%5Bcreated_at%5D=true&select%5Bstart_date%5D=true&select%5Breactions%5D=true&select%5Bintervals%5D=true&select%5Bcolumn_id%5D=true&select%5Bpriority%5D=true&select%5Bdue_date%5D=true&select%5Bcolumn%5D=true&select%5Btitle%5D=true&select%5Border%5D=true&select%5Bcolor%5D=true&where%5Bid%5D=task_id&where%5BAND%5D%5B0%5D%5BOR%5D%5B0%5D%5Bto_delete%5D=false&where%5BAND%5D%5B0%5D%5BOR%5D%5B1%5D%5Bto_delete%5D=null',
|
|
86
|
+
)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('should handle empty objects correctly', () => {
|
|
90
|
+
const params = {}
|
|
91
|
+
const result = encodeQueryParams(params)
|
|
92
|
+
expect(result).toBe('')
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it('should handle null values correctly', () => {
|
|
96
|
+
const params = {
|
|
97
|
+
id: null,
|
|
98
|
+
name: 'test',
|
|
99
|
+
}
|
|
100
|
+
const result = encodeQueryParams(params)
|
|
101
|
+
expect(result).toBe('id=null&name=test')
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('should handle boolean values correctly', () => {
|
|
105
|
+
const params = {
|
|
106
|
+
isActive: true,
|
|
107
|
+
isDeleted: false,
|
|
108
|
+
}
|
|
109
|
+
const result = encodeQueryParams(params)
|
|
110
|
+
expect(result).toBe('isActive=true&isDeleted=false')
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
it('should handle nested arrays correctly', () => {
|
|
114
|
+
const params = {
|
|
115
|
+
filters: [
|
|
116
|
+
{
|
|
117
|
+
key: 'age',
|
|
118
|
+
values: [20, 30, 40],
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
}
|
|
122
|
+
const result = encodeQueryParams(params)
|
|
123
|
+
expect(result).toBe(
|
|
124
|
+
'filters%5B0%5D%5Bkey%5D=age&filters%5B0%5D%5Bvalues%5D%5B0%5D=20&filters%5B0%5D%5Bvalues%5D%5B1%5D=30&filters%5B0%5D%5Bvalues%5D%5B2%5D=40',
|
|
125
|
+
)
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
it('should handle complex nested arrays and objects', () => {
|
|
129
|
+
const params = {
|
|
130
|
+
select: {
|
|
131
|
+
id: true,
|
|
132
|
+
name: true,
|
|
133
|
+
details: {
|
|
134
|
+
address: {
|
|
135
|
+
street: 'Main St',
|
|
136
|
+
city: 'Metropolis',
|
|
137
|
+
},
|
|
138
|
+
phoneNumbers: [
|
|
139
|
+
{
|
|
140
|
+
type: 'home',
|
|
141
|
+
number: '123-456-7890',
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
type: 'work',
|
|
145
|
+
number: '098-765-4321',
|
|
146
|
+
},
|
|
147
|
+
],
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
}
|
|
151
|
+
const result = encodeQueryParams(params)
|
|
152
|
+
expect(result).toBe(
|
|
153
|
+
'select%5Bid%5D=true&select%5Bname%5D=true&select%5Bdetails%5D%5Baddress%5D%5Bstreet%5D=Main%20St&select%5Bdetails%5D%5Baddress%5D%5Bcity%5D=Metropolis&select%5Bdetails%5D%5BphoneNumbers%5D%5B0%5D%5Btype%5D=home&select%5Bdetails%5D%5BphoneNumbers%5D%5B0%5D%5Bnumber%5D=123-456-7890&select%5Bdetails%5D%5BphoneNumbers%5D%5B1%5D%5Btype%5D=work&select%5Bdetails%5D%5BphoneNumbers%5D%5B1%5D%5Bnumber%5D=098-765-4321',
|
|
154
|
+
)
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it('should handle edge cases correctly', () => {
|
|
158
|
+
const params = {
|
|
159
|
+
select: {
|
|
160
|
+
id: '',
|
|
161
|
+
name: null,
|
|
162
|
+
details: {
|
|
163
|
+
address: {
|
|
164
|
+
street: undefined,
|
|
165
|
+
city: 'Metropolis',
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
}
|
|
170
|
+
const result = encodeQueryParams(params)
|
|
171
|
+
expect(result).toBe(
|
|
172
|
+
'select%5Bid%5D=&select%5Bname%5D=null&select%5Bdetails%5D%5Baddress%5D%5Bcity%5D=Metropolis',
|
|
173
|
+
)
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
it('should handle multiple nested levels correctly', () => {
|
|
177
|
+
const params = {
|
|
178
|
+
level1: {
|
|
179
|
+
level2: {
|
|
180
|
+
level3: {
|
|
181
|
+
level4: {
|
|
182
|
+
key: 'value',
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
}
|
|
188
|
+
const result = encodeQueryParams(params)
|
|
189
|
+
expect(result).toBe(
|
|
190
|
+
'level1%5Blevel2%5D%5Blevel3%5D%5Blevel4%5D%5Bkey%5D=value',
|
|
191
|
+
)
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
it('should handle empty strings and null values correctly', () => {
|
|
195
|
+
const params = {
|
|
196
|
+
key1: '',
|
|
197
|
+
key2: null,
|
|
198
|
+
key3: 'value',
|
|
199
|
+
}
|
|
200
|
+
const result = encodeQueryParams(params)
|
|
201
|
+
expect(result).toBe('key1=&key2=null&key3=value')
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
it('should encode special characters correctly', () => {
|
|
205
|
+
const params = {
|
|
206
|
+
name: "O'Reilly & Sons",
|
|
207
|
+
description: '20% discount!',
|
|
208
|
+
}
|
|
209
|
+
const result = encodeQueryParams(params)
|
|
210
|
+
expect(result).toBe(
|
|
211
|
+
'name=O%27Reilly%20%26%20Sons&description=20%25%20discount%21',
|
|
212
|
+
)
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
it('should handle empty arrays correctly', () => {
|
|
216
|
+
const params = {
|
|
217
|
+
filters: [],
|
|
218
|
+
}
|
|
219
|
+
const result = encodeQueryParams(params)
|
|
220
|
+
expect(result).toBe('filters%5B%5D=')
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
it('should encode only special characters correctly', () => {
|
|
224
|
+
const params = {
|
|
225
|
+
special: '!@#$%^&*()_+{}|:"<>?[]\\;\',./`~',
|
|
226
|
+
}
|
|
227
|
+
const result = encodeQueryParams(params)
|
|
228
|
+
expect(result).toBe(
|
|
229
|
+
'special=%21%40%23%24%25%5E%26%2A%28%29_%2B%7B%7D%7C%3A%22%3C%3E%3F%5B%5D%5C%3B%27%2C.%2F%60%7E',
|
|
230
|
+
)
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
it('should encode repeated special characters correctly', () => {
|
|
234
|
+
const params = {
|
|
235
|
+
special: '!!!@@@###$$$',
|
|
236
|
+
}
|
|
237
|
+
const result = encodeQueryParams(params)
|
|
238
|
+
expect(result).toBe('special=%21%21%21%40%40%40%23%23%23%24%24%24')
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
it('should encode special characters in keys correctly', () => {
|
|
242
|
+
const params = {
|
|
243
|
+
'sp!ci@l': 'value',
|
|
244
|
+
'key&with=special?chars': 'another value',
|
|
245
|
+
}
|
|
246
|
+
const result = encodeQueryParams(params)
|
|
247
|
+
expect(result).toBe(
|
|
248
|
+
'sp%21ci%40l=value&key%26with%3Dspecial%3Fchars=another%20value',
|
|
249
|
+
)
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
it('should handle arrays with mixed types correctly', () => {
|
|
253
|
+
const params = {
|
|
254
|
+
mixedArray: [1, 'two', { three: 3 }],
|
|
255
|
+
}
|
|
256
|
+
const result = encodeQueryParams(params)
|
|
257
|
+
expect(result).toBe(
|
|
258
|
+
'mixedArray%5B0%5D=1&mixedArray%5B1%5D=two&mixedArray%5B2%5D%5Bthree%5D=3',
|
|
259
|
+
)
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
it('should handle boolean and null values in nested objects correctly', () => {
|
|
263
|
+
const params = {
|
|
264
|
+
nested: {
|
|
265
|
+
flag: true,
|
|
266
|
+
notSet: null,
|
|
267
|
+
},
|
|
268
|
+
}
|
|
269
|
+
const result = encodeQueryParams(params)
|
|
270
|
+
expect(result).toBe('nested%5Bflag%5D=true&nested%5BnotSet%5D=null')
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
it('should handle empty strings in nested objects correctly', () => {
|
|
274
|
+
const params = {
|
|
275
|
+
nested: {
|
|
276
|
+
emptyString: '',
|
|
277
|
+
validString: 'value',
|
|
278
|
+
},
|
|
279
|
+
}
|
|
280
|
+
const result = encodeQueryParams(params)
|
|
281
|
+
expect(result).toBe(
|
|
282
|
+
'nested%5BemptyString%5D=&nested%5BvalidString%5D=value',
|
|
283
|
+
)
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
it('should handle deeply nested structures correctly', () => {
|
|
287
|
+
const params = {
|
|
288
|
+
level1: {
|
|
289
|
+
level2: {
|
|
290
|
+
level3: {
|
|
291
|
+
level4: {
|
|
292
|
+
level5: 'value',
|
|
293
|
+
},
|
|
294
|
+
},
|
|
295
|
+
},
|
|
296
|
+
},
|
|
297
|
+
}
|
|
298
|
+
const result = encodeQueryParams(params)
|
|
299
|
+
expect(result).toBe(
|
|
300
|
+
'level1%5Blevel2%5D%5Blevel3%5D%5Blevel4%5D%5Blevel5%5D=value',
|
|
301
|
+
)
|
|
302
|
+
})
|
|
303
|
+
})
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { isObject } from './misc'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Encodes query parameters recursively, skipping undefined values.
|
|
5
|
+
* @param {Record<string, unknown>} params - The query parameters to encode.
|
|
6
|
+
* @returns {string} The encoded query string.
|
|
7
|
+
*/
|
|
8
|
+
export const encodeQueryParams = (params: Record<string, unknown>): string => {
|
|
9
|
+
const customEncodeURIComponent = (str: string): string => {
|
|
10
|
+
return encodeURIComponent(str)
|
|
11
|
+
.replace(
|
|
12
|
+
/[!'()*~]/g,
|
|
13
|
+
(c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`,
|
|
14
|
+
)
|
|
15
|
+
.replace(/%20/g, '%20') // Encode spaces as %20
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const encode = (key: string, value: unknown): string => {
|
|
19
|
+
if (value === undefined) {
|
|
20
|
+
return ''
|
|
21
|
+
}
|
|
22
|
+
if (Array.isArray(value)) {
|
|
23
|
+
if (value.length === 0) {
|
|
24
|
+
return `${customEncodeURIComponent(key)}%5B%5D=`
|
|
25
|
+
}
|
|
26
|
+
return value
|
|
27
|
+
.map((v, i) => encode(`${key}[${i}]`, v))
|
|
28
|
+
.filter(Boolean)
|
|
29
|
+
.join('&')
|
|
30
|
+
}
|
|
31
|
+
if (isObject(value)) {
|
|
32
|
+
return Object.entries(value)
|
|
33
|
+
.map(([k, v]) => encode(`${key}[${k}]`, v))
|
|
34
|
+
.filter(Boolean)
|
|
35
|
+
.join('&')
|
|
36
|
+
}
|
|
37
|
+
return `${customEncodeURIComponent(key)}=${customEncodeURIComponent(String(value))}`
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return Object.entries(params)
|
|
41
|
+
.map(([k, v]) => encode(k, v))
|
|
42
|
+
.filter(Boolean)
|
|
43
|
+
.join('&')
|
|
44
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { isJsonString, safeJSONparse, isObject } from './misc'
|
|
2
|
+
|
|
3
|
+
describe('misc.ts', () => {
|
|
4
|
+
describe('isJsonString', () => {
|
|
5
|
+
it('should return true for valid JSON string', () => {
|
|
6
|
+
expect(isJsonString('{"name":"John"}')).toBe(true)
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
it('should return false for invalid JSON string', () => {
|
|
10
|
+
expect(isJsonString('{"name": "John"')).toBe(false) // Missing closing brace
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it('should return false for non-string input', () => {
|
|
14
|
+
expect(isJsonString(123)).toBe(false)
|
|
15
|
+
expect(isJsonString({ name: 'John' })).toBe(false)
|
|
16
|
+
expect(isJsonString(null)).toBe(false)
|
|
17
|
+
expect(isJsonString(undefined)).toBe(false)
|
|
18
|
+
})
|
|
19
|
+
})
|
|
20
|
+
describe('safeJSONparse', () => {
|
|
21
|
+
it('should parse valid JSON string', () => {
|
|
22
|
+
expect(safeJSONparse('{"name":"John"}')).toEqual({ name: 'John' })
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('should return false for string "false"', () => {
|
|
26
|
+
expect(safeJSONparse('false')).toBe(false)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('should return undefined for string "undefined"', () => {
|
|
30
|
+
expect(safeJSONparse('undefined')).toBeUndefined()
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('should return null for string "null"', () => {
|
|
34
|
+
expect(safeJSONparse('null')).toBeNull()
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('should return original data for non-JSON string', () => {
|
|
38
|
+
expect(safeJSONparse('Hello World')).toBe('Hello World')
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('should return original data for non-string input', () => {
|
|
42
|
+
expect(safeJSONparse(123)).toBe(123)
|
|
43
|
+
expect(safeJSONparse({ name: 'John' })).toEqual({ name: 'John' })
|
|
44
|
+
expect(safeJSONparse(null)).toBe(null)
|
|
45
|
+
expect(safeJSONparse(undefined)).toBe(undefined)
|
|
46
|
+
})
|
|
47
|
+
})
|
|
48
|
+
describe('isObject', () => {
|
|
49
|
+
it('should return true for objects', () => {
|
|
50
|
+
expect(isObject({})).toBe(true)
|
|
51
|
+
expect(isObject({ name: 'John' })).toBe(true)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('should return false for non-objects', () => {
|
|
55
|
+
expect(isObject('Hello World')).toBe(false)
|
|
56
|
+
expect(isObject(123)).toBe(false)
|
|
57
|
+
expect(isObject(null)).toBe(false)
|
|
58
|
+
expect(isObject(undefined)).toBe(false)
|
|
59
|
+
expect(isObject([])).toBe(false) // Arrays are not considered objects in this context
|
|
60
|
+
})
|
|
61
|
+
})
|
|
62
|
+
})
|
package/src/copy/misc.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export function isJsonString(str: string | unknown): boolean {
|
|
2
|
+
if (typeof str !== 'string') {
|
|
3
|
+
return false
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
try {
|
|
7
|
+
JSON.parse(str)
|
|
8
|
+
} catch (e: unknown) {
|
|
9
|
+
return false
|
|
10
|
+
}
|
|
11
|
+
return true
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function safeJSONparse<T>(
|
|
15
|
+
data: unknown,
|
|
16
|
+
): T | boolean | undefined | null {
|
|
17
|
+
if (data === 'false') return false
|
|
18
|
+
if (data === 'undefined') return undefined
|
|
19
|
+
if (data === 'null') return null
|
|
20
|
+
return isJsonString(data) ? JSON.parse(data as string) : data
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const isObject = (value: unknown): value is Record<string, unknown> => {
|
|
24
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
25
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { parseQueryParams } from './parseQueryParams'
|
|
2
|
+
|
|
3
|
+
describe('parseQueryParams', () => {
|
|
4
|
+
it('should parse basic field selection correctly', () => {
|
|
5
|
+
const params = {
|
|
6
|
+
select: {
|
|
7
|
+
id: 'true',
|
|
8
|
+
name: 'true',
|
|
9
|
+
},
|
|
10
|
+
}
|
|
11
|
+
const result = parseQueryParams(params)
|
|
12
|
+
expect(result).toEqual({
|
|
13
|
+
select: {
|
|
14
|
+
id: true,
|
|
15
|
+
name: true,
|
|
16
|
+
},
|
|
17
|
+
})
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('should parse nested field selection correctly', () => {
|
|
21
|
+
const params = {
|
|
22
|
+
select: {
|
|
23
|
+
user: {
|
|
24
|
+
select: {
|
|
25
|
+
profile: 'true',
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
}
|
|
30
|
+
const result = parseQueryParams(params)
|
|
31
|
+
expect(result).toEqual({
|
|
32
|
+
select: {
|
|
33
|
+
user: {
|
|
34
|
+
select: {
|
|
35
|
+
profile: true,
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
})
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('should handle deeply nested logical operators correctly', () => {
|
|
43
|
+
const params = {
|
|
44
|
+
where: {
|
|
45
|
+
AND: [
|
|
46
|
+
{ id: '123' },
|
|
47
|
+
{
|
|
48
|
+
OR: [{ active: 'true' }, { active: 'false' }],
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
},
|
|
52
|
+
}
|
|
53
|
+
const result = parseQueryParams(params)
|
|
54
|
+
expect(result).toEqual({
|
|
55
|
+
where: {
|
|
56
|
+
AND: [
|
|
57
|
+
{ id: 123 },
|
|
58
|
+
{
|
|
59
|
+
OR: [{ active: true }, { active: false }],
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
},
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it('should handle complex nested arrays correctly', () => {
|
|
67
|
+
const params = {
|
|
68
|
+
where: {
|
|
69
|
+
id: 'Vxdu42',
|
|
70
|
+
AND: [
|
|
71
|
+
{
|
|
72
|
+
OR: [{ to_delete: 'false' }, { to_delete: 'null' }],
|
|
73
|
+
},
|
|
74
|
+
],
|
|
75
|
+
},
|
|
76
|
+
}
|
|
77
|
+
const result = parseQueryParams(params)
|
|
78
|
+
expect(result).toEqual({
|
|
79
|
+
where: {
|
|
80
|
+
id: 'Vxdu42',
|
|
81
|
+
AND: [
|
|
82
|
+
{
|
|
83
|
+
OR: [{ to_delete: false }, { to_delete: null }],
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
},
|
|
87
|
+
})
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('should handle mixed types and conditions', () => {
|
|
91
|
+
const params = {
|
|
92
|
+
select: {
|
|
93
|
+
id: 'true',
|
|
94
|
+
project_id: 'true',
|
|
95
|
+
user_assignments: {
|
|
96
|
+
select: {
|
|
97
|
+
user: 'true',
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
tags_mappings: {
|
|
101
|
+
select: {
|
|
102
|
+
tag: 'true',
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
attachments: {
|
|
106
|
+
select: {
|
|
107
|
+
attachment: 'true',
|
|
108
|
+
},
|
|
109
|
+
where: {
|
|
110
|
+
is_image: 'true',
|
|
111
|
+
},
|
|
112
|
+
take: '100',
|
|
113
|
+
orderBy: {
|
|
114
|
+
created_at: 'desc',
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
where: {
|
|
119
|
+
id: 'Vxdu42',
|
|
120
|
+
AND: [
|
|
121
|
+
{
|
|
122
|
+
OR: [{ to_delete: 'false' }, { to_delete: 'null' }],
|
|
123
|
+
},
|
|
124
|
+
],
|
|
125
|
+
},
|
|
126
|
+
}
|
|
127
|
+
const result = parseQueryParams(params)
|
|
128
|
+
expect(result).toEqual({
|
|
129
|
+
select: {
|
|
130
|
+
id: true,
|
|
131
|
+
project_id: true,
|
|
132
|
+
user_assignments: {
|
|
133
|
+
select: {
|
|
134
|
+
user: true,
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
tags_mappings: {
|
|
138
|
+
select: {
|
|
139
|
+
tag: true,
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
attachments: {
|
|
143
|
+
select: {
|
|
144
|
+
attachment: true,
|
|
145
|
+
},
|
|
146
|
+
where: {
|
|
147
|
+
is_image: true,
|
|
148
|
+
},
|
|
149
|
+
take: 100,
|
|
150
|
+
orderBy: {
|
|
151
|
+
created_at: 'desc',
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
where: {
|
|
156
|
+
id: 'Vxdu42',
|
|
157
|
+
AND: [
|
|
158
|
+
{
|
|
159
|
+
OR: [{ to_delete: false }, { to_delete: null }],
|
|
160
|
+
},
|
|
161
|
+
],
|
|
162
|
+
},
|
|
163
|
+
})
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it('should handle special cases correctly', () => {
|
|
167
|
+
const params = {
|
|
168
|
+
emptyObject: {},
|
|
169
|
+
emptyArray: [],
|
|
170
|
+
unexpectedType: '123',
|
|
171
|
+
}
|
|
172
|
+
const result = parseQueryParams(params)
|
|
173
|
+
expect(result).toEqual({
|
|
174
|
+
emptyObject: {},
|
|
175
|
+
emptyArray: [],
|
|
176
|
+
unexpectedType: 123,
|
|
177
|
+
})
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
it('should parse string values correctly', () => {
|
|
181
|
+
expect(parseQueryParams('true')).toBe(true)
|
|
182
|
+
expect(parseQueryParams('false')).toBe(false)
|
|
183
|
+
expect(parseQueryParams('null')).toBe(null)
|
|
184
|
+
expect(parseQueryParams('123')).toBe(123)
|
|
185
|
+
expect(parseQueryParams('test')).toBe('test')
|
|
186
|
+
})
|
|
187
|
+
})
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { isObject } from './misc'
|
|
2
|
+
import { ParsedQs } from 'qs'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Type definition for possible query parameter types.
|
|
6
|
+
*/
|
|
7
|
+
type QueryParams = string | ParsedQs | string[] | ParsedQs[] | undefined
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Parses a query value to convert strings to their respective types.
|
|
11
|
+
* @param {string} value - The query value to parse.
|
|
12
|
+
* @returns {unknown} The parsed value.
|
|
13
|
+
*/
|
|
14
|
+
const parseQueryValue = (value: string): unknown => {
|
|
15
|
+
if (value === 'true') return true
|
|
16
|
+
if (value === 'false') return false
|
|
17
|
+
if (value === 'null') return null
|
|
18
|
+
if (!isNaN(Number(value))) return Number(value)
|
|
19
|
+
return value
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Recursively parses query parameters to convert strings to their respective types.
|
|
24
|
+
* @param {QueryParams} params - The query parameters to parse.
|
|
25
|
+
* @returns {unknown} The parsed query parameters.
|
|
26
|
+
*/
|
|
27
|
+
export const parseQueryParams = (params: QueryParams): unknown => {
|
|
28
|
+
if (typeof params === 'string') {
|
|
29
|
+
return parseQueryValue(params)
|
|
30
|
+
}
|
|
31
|
+
if (Array.isArray(params)) {
|
|
32
|
+
return params.map(parseQueryParams)
|
|
33
|
+
}
|
|
34
|
+
if (isObject(params)) {
|
|
35
|
+
const parsedParams: Record<string, unknown> = {}
|
|
36
|
+
for (const key in params) {
|
|
37
|
+
parsedParams[key] = parseQueryParams(params[key])
|
|
38
|
+
}
|
|
39
|
+
return parsedParams
|
|
40
|
+
}
|
|
41
|
+
return params
|
|
42
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { RequestHandler } from 'express'
|
|
2
|
+
import { ZodTypeAny } from 'zod'
|
|
3
|
+
|
|
4
|
+
export interface ValidatorConfig {
|
|
5
|
+
allow?: string[]
|
|
6
|
+
forbid?: string[]
|
|
7
|
+
schema: ZodTypeAny
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface MiddlewareConfig<M> {
|
|
11
|
+
before?: M[]
|
|
12
|
+
after?: RequestHandler[]
|
|
13
|
+
input?: ValidatorConfig
|
|
14
|
+
output?: ValidatorConfig
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface RouteConfig<M> {
|
|
18
|
+
findFirst?: MiddlewareConfig<M>
|
|
19
|
+
findMany?: MiddlewareConfig<M>
|
|
20
|
+
findUnique?: MiddlewareConfig<M>
|
|
21
|
+
create?: MiddlewareConfig<M>
|
|
22
|
+
createMany?: MiddlewareConfig<M>
|
|
23
|
+
update?: MiddlewareConfig<M>
|
|
24
|
+
updateMany?: MiddlewareConfig<M>
|
|
25
|
+
upsert?: MiddlewareConfig<M>
|
|
26
|
+
delete?: MiddlewareConfig<M>
|
|
27
|
+
deleteMany?: MiddlewareConfig<M>
|
|
28
|
+
aggregate?: MiddlewareConfig<M>
|
|
29
|
+
count?: MiddlewareConfig<M>
|
|
30
|
+
groupBy?: MiddlewareConfig<M>
|
|
31
|
+
addModelPrefix?: boolean
|
|
32
|
+
enableAll?: boolean
|
|
33
|
+
customUrlPrefix?: string
|
|
34
|
+
}
|