prisma-generator-express 1.16.2 → 1.16.4

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.
@@ -21,7 +21,7 @@ import { ${modelName}DeleteMany } from './${modelName}DeleteMany';
21
21
  import { ${modelName}Aggregate } from './${modelName}Aggregate';
22
22
  import { ${modelName}Count } from './${modelName}Count';
23
23
  import { ${modelName}GroupBy } from './${modelName}GroupBy';
24
- import { createValidatorMiddleware } from '../createValidatorMiddleware'
24
+ import { createValidatorMiddleware, removeTrailingSlash } from '../createValidatorMiddleware'
25
25
  import { RouteConfig, ValidatorConfig } from '../routeConfig'
26
26
  import { parseQueryParams } from "../parseQueryParams";
27
27
 
@@ -40,7 +40,8 @@ const defaultBeforeAfter = {
40
40
  */
41
41
  export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
42
42
  const router = express.Router();
43
- const basePath = (config.customUrlPrefix || '') + (config.addModelPrefix ? '/${modelName.toLowerCase()}' : '');
43
+ const basePath = removeTrailingSlash(config.customUrlPrefix || '') +
44
+ removeTrailingSlash(config.addModelPrefix !== false ? '/${modelName.toLowerCase()}' : '');
44
45
 
45
46
  const setupRoute = (
46
47
  path: string,
@@ -84,7 +85,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
84
85
  const { before = [], after = [], inputValidator, outputValidator } = config.findFirst || defaultBeforeAfter;
85
86
  setupRoute('/first', 'get', before, ${modelName}FindFirst as RequestHandler, inputValidator, outputValidator);
86
87
  if (after.length) {
87
- router.use(basePath + '/first', ...after);
88
+ router.use(removeTrailingSlash(basePath) + '/first', ...after);
88
89
  }
89
90
  }
90
91
 
@@ -92,7 +93,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
92
93
  const { before = [], after = [], inputValidator, outputValidator } = config.findMany || defaultBeforeAfter;
93
94
  setupRoute('/', 'get', before, ${modelName}FindMany as RequestHandler, inputValidator, outputValidator);
94
95
  if (after.length) {
95
- router.use(basePath + '/', ...after);
96
+ router.use(removeTrailingSlash(basePath + '/'), ...after);
96
97
  }
97
98
  }
98
99
 
@@ -100,7 +101,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
100
101
  const { before = [], after = [], inputValidator, outputValidator } = config.findUnique || defaultBeforeAfter;
101
102
  setupRoute('/:id', 'get', before, ${modelName}FindUnique as any, inputValidator, outputValidator);
102
103
  if (after.length) {
103
- router.use(basePath + '/:id', ...after);
104
+ router.use(removeTrailingSlash(basePath) + '/:id', ...after);
104
105
  }
105
106
  }
106
107
 
@@ -108,7 +109,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
108
109
  const { before = [], after = [], inputValidator, outputValidator } = config.create || defaultBeforeAfter;
109
110
  setupRoute('/', 'post', before, ${modelName}Create as RequestHandler, inputValidator, outputValidator);
110
111
  if (after.length) {
111
- router.use(basePath + '/', ...after);
112
+ router.use(removeTrailingSlash(basePath + '/'), ...after);
112
113
  }
113
114
  }
114
115
 
@@ -116,7 +117,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
116
117
  const { before = [], after = [], inputValidator, outputValidator } = config.createMany || defaultBeforeAfter;
117
118
  setupRoute('/many', 'post', before, ${modelName}CreateMany as RequestHandler, inputValidator, outputValidator);
118
119
  if (after.length) {
119
- router.use(basePath + '/many', ...after);
120
+ router.use(removeTrailingSlash(basePath) + '/many', ...after);
120
121
  }
121
122
  }
122
123
 
@@ -124,7 +125,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
124
125
  const { before = [], after = [], inputValidator, outputValidator } = config.update || defaultBeforeAfter;
125
126
  setupRoute('/', 'put', before, ${modelName}Update as RequestHandler, inputValidator, outputValidator);
126
127
  if (after.length) {
127
- router.use(basePath + '/', ...after);
128
+ router.use(removeTrailingSlash(basePath + '/'), ...after);
128
129
  }
129
130
  }
130
131
 
@@ -132,7 +133,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
132
133
  const { before = [], after = [], inputValidator, outputValidator } = config.updateMany || defaultBeforeAfter;
133
134
  setupRoute('/many', 'put', before, ${modelName}UpdateMany as RequestHandler, inputValidator, outputValidator);
134
135
  if (after.length) {
135
- router.use(basePath + '/many', ...after);
136
+ router.use(removeTrailingSlash(basePath) + '/many', ...after);
136
137
  }
137
138
  }
138
139
 
@@ -140,7 +141,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
140
141
  const { before = [], after = [], inputValidator, outputValidator } = config.upsert || defaultBeforeAfter;
141
142
  setupRoute('/', 'patch', before, ${modelName}Upsert as RequestHandler, inputValidator, outputValidator);
142
143
  if (after.length) {
143
- router.use(basePath + '/', ...after);
144
+ router.use(removeTrailingSlash(basePath + '/'), ...after);
144
145
  }
145
146
  }
146
147
 
@@ -148,7 +149,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
148
149
  const { before = [], after = [], inputValidator, outputValidator } = config.delete || defaultBeforeAfter;
149
150
  setupRoute('/', 'delete', before, ${modelName}Delete as RequestHandler, inputValidator, outputValidator);
150
151
  if (after.length) {
151
- router.use(basePath + '/', ...after);
152
+ router.use(removeTrailingSlash(basePath + '/'), ...after);
152
153
  }
153
154
  }
154
155
 
@@ -156,7 +157,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
156
157
  const { before = [], after = [], inputValidator, outputValidator } = config.deleteMany || defaultBeforeAfter;
157
158
  setupRoute('/many', 'delete', before, ${modelName}DeleteMany as RequestHandler, inputValidator, outputValidator);
158
159
  if (after.length) {
159
- router.use(basePath + '/many', ...after);
160
+ router.use(removeTrailingSlash(basePath) + '/many', ...after);
160
161
  }
161
162
  }
162
163
 
@@ -164,7 +165,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
164
165
  const { before = [], after = [], inputValidator, outputValidator } = config.aggregate || defaultBeforeAfter;
165
166
  setupRoute('/aggregate', 'get', before, ${modelName}Aggregate as RequestHandler, inputValidator, outputValidator);
166
167
  if (after.length) {
167
- router.use(basePath + '/aggregate', ...after);
168
+ router.use(removeTrailingSlash(basePath) + '/aggregate', ...after);
168
169
  }
169
170
  }
170
171
 
@@ -172,7 +173,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
172
173
  const { before = [], after = [], inputValidator, outputValidator } = config.count || defaultBeforeAfter;
173
174
  setupRoute('/count', 'get', before, ${modelName}Count as RequestHandler, inputValidator, outputValidator);
174
175
  if (after.length) {
175
- router.use(basePath + '/count', ...after);
176
+ router.use(removeTrailingSlash(basePath) + '/count', ...after);
176
177
  }
177
178
  }
178
179
 
@@ -180,7 +181,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
180
181
  const { before = [], after = [], inputValidator, outputValidator } = config.groupBy || defaultBeforeAfter;
181
182
  setupRoute('/groupby', 'get', before, ${modelName}GroupBy as RequestHandler, inputValidator, outputValidator);
182
183
  if (after.length) {
183
- router.use(basePath + '/groupby', ...after);
184
+ router.use(removeTrailingSlash(basePath) + '/groupby', ...after);
184
185
  }
185
186
  }
186
187
 
@@ -1 +1 @@
1
- {"version":3,"file":"generateRouteFile.js","sourceRoot":"","sources":["../../src/helpers/generateRouteFile.ts"],"names":[],"mappings":";;;AAEA,SAAgB,sBAAsB,CAAC,EACrC,KAAK,GAGN;IACC,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAA;IAC5B,MAAM,kBAAkB,GAAG,GAAG,SAAS,QAAQ,CAAA;IAE/C,OAAO;;;;WAIE,SAAS,uBAAuB,SAAS;WACzC,SAAS,sBAAsB,SAAS;WACxC,SAAS,wBAAwB,SAAS;WAC1C,SAAS,oBAAoB,SAAS;WACtC,SAAS,wBAAwB,SAAS;WAC1C,SAAS,oBAAoB,SAAS;WACtC,SAAS,wBAAwB,SAAS;WAC1C,SAAS,oBAAoB,SAAS;WACtC,SAAS,oBAAoB,SAAS;WACtC,SAAS,wBAAwB,SAAS;WAC1C,SAAS,uBAAuB,SAAS;WACzC,SAAS,mBAAmB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S;;;;;;;;qCAQd,SAAS;;;;;;;;wCAQN,SAAS;;;;;;;;sCAQX,SAAS;;;;;;;;0CAQL,SAAS;;;;;;;;qCAQd,SAAS;;;;;;;;yCAQL,SAAS;;;;;;;;uCAQX,SAAS;;;;;;;;wCAQR,SAAS;;;;;;;;4CAQL,SAAS;;;;;;;;8CAQP,SAAS;;;;;;;;0CAQb,SAAS;;;;;;;;4CAQP,SAAS;;;;;;;;CAQpD,CAAA;AACD,CAAC;AAhMD,wDAgMC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "prisma-generator-express",
3
3
  "description": "Prisma generator of Express CRUD API",
4
- "version": "1.16.2",
4
+ "version": "1.16.4",
5
5
  "main": "dist/generator.js",
6
6
  "license": "MIT",
7
7
  "bin": {
@@ -30,7 +30,13 @@ export function createOutputValidatorMiddleware({
30
30
  const validationResult = schema.safeParse(data)
31
31
  if (!validationResult.success) {
32
32
  const errors = validationResult.error.errors
33
- return next({
33
+ next({
34
+ status: 400,
35
+ message: 'Output validation failed',
36
+ errors,
37
+ })
38
+
39
+ return res.status(400).json({
34
40
  status: 400,
35
41
  message: 'Output validation failed',
36
42
  errors,
@@ -53,3 +53,10 @@ export function createValidatorMiddleware({
53
53
  next()
54
54
  }
55
55
  }
56
+
57
+ export function removeTrailingSlash(path: string): string {
58
+ if (path === '/') {
59
+ return path
60
+ }
61
+ return path.replace(/\/+$/, '')
62
+ }
@@ -25,7 +25,7 @@ import { ${modelName}DeleteMany } from './${modelName}DeleteMany';
25
25
  import { ${modelName}Aggregate } from './${modelName}Aggregate';
26
26
  import { ${modelName}Count } from './${modelName}Count';
27
27
  import { ${modelName}GroupBy } from './${modelName}GroupBy';
28
- import { createValidatorMiddleware } from '../createValidatorMiddleware'
28
+ import { createValidatorMiddleware, removeTrailingSlash } from '../createValidatorMiddleware'
29
29
  import { RouteConfig, ValidatorConfig } from '../routeConfig'
30
30
  import { parseQueryParams } from "../parseQueryParams";
31
31
 
@@ -44,7 +44,8 @@ const defaultBeforeAfter = {
44
44
  */
45
45
  export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
46
46
  const router = express.Router();
47
- const basePath = (config.customUrlPrefix || '') + (config.addModelPrefix ? '/${modelName.toLowerCase()}' : '');
47
+ const basePath = removeTrailingSlash(config.customUrlPrefix || '') +
48
+ removeTrailingSlash(config.addModelPrefix !== false ? '/${modelName.toLowerCase()}' : '');
48
49
 
49
50
  const setupRoute = (
50
51
  path: string,
@@ -88,7 +89,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
88
89
  const { before = [], after = [], inputValidator, outputValidator } = config.findFirst || defaultBeforeAfter;
89
90
  setupRoute('/first', 'get', before, ${modelName}FindFirst as RequestHandler, inputValidator, outputValidator);
90
91
  if (after.length) {
91
- router.use(basePath + '/first', ...after);
92
+ router.use(removeTrailingSlash(basePath) + '/first', ...after);
92
93
  }
93
94
  }
94
95
 
@@ -96,7 +97,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
96
97
  const { before = [], after = [], inputValidator, outputValidator } = config.findMany || defaultBeforeAfter;
97
98
  setupRoute('/', 'get', before, ${modelName}FindMany as RequestHandler, inputValidator, outputValidator);
98
99
  if (after.length) {
99
- router.use(basePath + '/', ...after);
100
+ router.use(removeTrailingSlash(basePath + '/'), ...after);
100
101
  }
101
102
  }
102
103
 
@@ -104,7 +105,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
104
105
  const { before = [], after = [], inputValidator, outputValidator } = config.findUnique || defaultBeforeAfter;
105
106
  setupRoute('/:id', 'get', before, ${modelName}FindUnique as any, inputValidator, outputValidator);
106
107
  if (after.length) {
107
- router.use(basePath + '/:id', ...after);
108
+ router.use(removeTrailingSlash(basePath) + '/:id', ...after);
108
109
  }
109
110
  }
110
111
 
@@ -112,7 +113,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
112
113
  const { before = [], after = [], inputValidator, outputValidator } = config.create || defaultBeforeAfter;
113
114
  setupRoute('/', 'post', before, ${modelName}Create as RequestHandler, inputValidator, outputValidator);
114
115
  if (after.length) {
115
- router.use(basePath + '/', ...after);
116
+ router.use(removeTrailingSlash(basePath + '/'), ...after);
116
117
  }
117
118
  }
118
119
 
@@ -120,7 +121,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
120
121
  const { before = [], after = [], inputValidator, outputValidator } = config.createMany || defaultBeforeAfter;
121
122
  setupRoute('/many', 'post', before, ${modelName}CreateMany as RequestHandler, inputValidator, outputValidator);
122
123
  if (after.length) {
123
- router.use(basePath + '/many', ...after);
124
+ router.use(removeTrailingSlash(basePath) + '/many', ...after);
124
125
  }
125
126
  }
126
127
 
@@ -128,7 +129,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
128
129
  const { before = [], after = [], inputValidator, outputValidator } = config.update || defaultBeforeAfter;
129
130
  setupRoute('/', 'put', before, ${modelName}Update as RequestHandler, inputValidator, outputValidator);
130
131
  if (after.length) {
131
- router.use(basePath + '/', ...after);
132
+ router.use(removeTrailingSlash(basePath + '/'), ...after);
132
133
  }
133
134
  }
134
135
 
@@ -136,7 +137,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
136
137
  const { before = [], after = [], inputValidator, outputValidator } = config.updateMany || defaultBeforeAfter;
137
138
  setupRoute('/many', 'put', before, ${modelName}UpdateMany as RequestHandler, inputValidator, outputValidator);
138
139
  if (after.length) {
139
- router.use(basePath + '/many', ...after);
140
+ router.use(removeTrailingSlash(basePath) + '/many', ...after);
140
141
  }
141
142
  }
142
143
 
@@ -144,7 +145,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
144
145
  const { before = [], after = [], inputValidator, outputValidator } = config.upsert || defaultBeforeAfter;
145
146
  setupRoute('/', 'patch', before, ${modelName}Upsert as RequestHandler, inputValidator, outputValidator);
146
147
  if (after.length) {
147
- router.use(basePath + '/', ...after);
148
+ router.use(removeTrailingSlash(basePath + '/'), ...after);
148
149
  }
149
150
  }
150
151
 
@@ -152,7 +153,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
152
153
  const { before = [], after = [], inputValidator, outputValidator } = config.delete || defaultBeforeAfter;
153
154
  setupRoute('/', 'delete', before, ${modelName}Delete as RequestHandler, inputValidator, outputValidator);
154
155
  if (after.length) {
155
- router.use(basePath + '/', ...after);
156
+ router.use(removeTrailingSlash(basePath + '/'), ...after);
156
157
  }
157
158
  }
158
159
 
@@ -160,7 +161,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
160
161
  const { before = [], after = [], inputValidator, outputValidator } = config.deleteMany || defaultBeforeAfter;
161
162
  setupRoute('/many', 'delete', before, ${modelName}DeleteMany as RequestHandler, inputValidator, outputValidator);
162
163
  if (after.length) {
163
- router.use(basePath + '/many', ...after);
164
+ router.use(removeTrailingSlash(basePath) + '/many', ...after);
164
165
  }
165
166
  }
166
167
 
@@ -168,7 +169,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
168
169
  const { before = [], after = [], inputValidator, outputValidator } = config.aggregate || defaultBeforeAfter;
169
170
  setupRoute('/aggregate', 'get', before, ${modelName}Aggregate as RequestHandler, inputValidator, outputValidator);
170
171
  if (after.length) {
171
- router.use(basePath + '/aggregate', ...after);
172
+ router.use(removeTrailingSlash(basePath) + '/aggregate', ...after);
172
173
  }
173
174
  }
174
175
 
@@ -176,7 +177,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
176
177
  const { before = [], after = [], inputValidator, outputValidator } = config.count || defaultBeforeAfter;
177
178
  setupRoute('/count', 'get', before, ${modelName}Count as RequestHandler, inputValidator, outputValidator);
178
179
  if (after.length) {
179
- router.use(basePath + '/count', ...after);
180
+ router.use(removeTrailingSlash(basePath) + '/count', ...after);
180
181
  }
181
182
  }
182
183
 
@@ -184,7 +185,7 @@ export function ${routerFunctionName}(config: RouteConfig<RequestHandler>) {
184
185
  const { before = [], after = [], inputValidator, outputValidator } = config.groupBy || defaultBeforeAfter;
185
186
  setupRoute('/groupby', 'get', before, ${modelName}GroupBy as RequestHandler, inputValidator, outputValidator);
186
187
  if (after.length) {
187
- router.use(basePath + '/groupby', ...after);
188
+ router.use(removeTrailingSlash(basePath) + '/groupby', ...after);
188
189
  }
189
190
  }
190
191