codehooks-js 1.2.3 → 1.2.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.
- package/crudlify/index.mjs +53 -36
- package/package.json +1 -1
package/crudlify/index.mjs
CHANGED
|
@@ -20,7 +20,7 @@ let _eventHooks = null;
|
|
|
20
20
|
* @see {@link https://codehooks.io/docs/database-rest-api}
|
|
21
21
|
* @param {object} app - Codehooks app ( npm package codehooks-js)
|
|
22
22
|
* @param {object} schema - Yup schema object
|
|
23
|
-
* @param {object} options -
|
|
23
|
+
* @param {object} options - {prefix: "/crud", schema: "yup"}
|
|
24
24
|
* @returns {Promise} EventHooks for REST operations
|
|
25
25
|
*/
|
|
26
26
|
export default async function crudlify(app, schema = {}, options = { schema: "yup", query: "q2m", prefix: "" }) {
|
|
@@ -52,35 +52,44 @@ export default async function crudlify(app, schema = {}, options = { schema: "yu
|
|
|
52
52
|
|
|
53
53
|
console.debug('Query lang', _query)
|
|
54
54
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
55
|
+
try {
|
|
56
|
+
// load schema validators
|
|
57
|
+
console.debug('load validators')
|
|
58
|
+
_validate = _validateYup;
|
|
59
|
+
_cast = _castYup;
|
|
60
|
+
_prepare = _prepareYup;
|
|
61
|
+
|
|
62
|
+
// schema provider other than Yup?
|
|
63
|
+
for (const property in schema) {
|
|
64
|
+
//console.log(`${property}: ${schema[property].toString()}`);
|
|
65
|
+
if (schema[property] !== undefined) {
|
|
66
|
+
if (schema[property].parse) {
|
|
67
|
+
_opt.schema = 'zod';
|
|
68
|
+
} else if (schema[property].properties) {
|
|
69
|
+
_opt.schema = 'json-schema';
|
|
70
|
+
}
|
|
71
|
+
} else {
|
|
72
|
+
console.error(`${property} is undefined`)
|
|
73
|
+
//schema[property] = null;
|
|
74
|
+
}
|
|
68
75
|
}
|
|
76
|
+
|
|
77
|
+
if (new String(_opt.schema).toLowerCase() === 'json-schema') {
|
|
78
|
+
_validate = _validateJSON;
|
|
79
|
+
_cast = _castJSON;
|
|
80
|
+
_prepare = _prepareJSON;
|
|
81
|
+
}
|
|
82
|
+
if (new String(_opt.schema).toLowerCase() === 'zod') {
|
|
83
|
+
_validate = _validateZod;
|
|
84
|
+
_cast = _castZod;
|
|
85
|
+
_prepare = _prepareZod;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// prep schemas
|
|
89
|
+
_schema = _prepare(_schema, _opt);
|
|
90
|
+
} catch (ex) {
|
|
91
|
+
throw new Error(ex)
|
|
69
92
|
}
|
|
70
|
-
|
|
71
|
-
if (new String(_opt.schema).toLowerCase() === 'json-schema') {
|
|
72
|
-
_validate = _validateJSON;
|
|
73
|
-
_cast = _castJSON;
|
|
74
|
-
_prepare = _prepareJSON;
|
|
75
|
-
}
|
|
76
|
-
if (new String(_opt.schema).toLowerCase() === 'zod') {
|
|
77
|
-
_validate = _validateZod;
|
|
78
|
-
_cast = _castZod;
|
|
79
|
-
_prepare = _prepareZod;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// prep schemas
|
|
83
|
-
_schema = _prepare(_schema, _opt);
|
|
84
93
|
|
|
85
94
|
console.debug('Apply routes to app');
|
|
86
95
|
// App API routes
|
|
@@ -150,7 +159,8 @@ async function createFunc(req, res) {
|
|
|
150
159
|
res.status(400).send(ex);
|
|
151
160
|
}
|
|
152
161
|
} else {
|
|
153
|
-
|
|
162
|
+
console.error('schema for' ,collection, 'is not valid:', _schema[collection])
|
|
163
|
+
return res.status(400).json({ "error": `Collection schema not found: ${collection}` });
|
|
154
164
|
}
|
|
155
165
|
|
|
156
166
|
}
|
|
@@ -161,7 +171,8 @@ async function readManyFunc(req, res) {
|
|
|
161
171
|
try {
|
|
162
172
|
const { collection } = req.params;
|
|
163
173
|
if (Object.keys(_schema).length > 0 && _schema[collection] === undefined) {
|
|
164
|
-
|
|
174
|
+
console.error('Invalid schema for', collection, _schema[collection])
|
|
175
|
+
return res.status(404).json({error: `Schema error for collection ${collection}`})
|
|
165
176
|
}
|
|
166
177
|
|
|
167
178
|
const mongoQuery = _query(req.query, req.headers);
|
|
@@ -181,7 +192,8 @@ async function readOneFunc(req, res) {
|
|
|
181
192
|
const conn = await Datastore.open();
|
|
182
193
|
try {
|
|
183
194
|
if (Object.keys(_schema).length > 0 && _schema[collection] === undefined) {
|
|
184
|
-
|
|
195
|
+
console.error('Invalid schema for', collection, _schema[collection])
|
|
196
|
+
return res.status(404).json({error: `Schema error for collection ${collection}`})
|
|
185
197
|
}
|
|
186
198
|
console.log(req.headers['x-real-ip'], req.method, req.originalUrl);
|
|
187
199
|
const result = await conn.getOne(collection, ID);
|
|
@@ -198,7 +210,8 @@ async function updateFunc(req, res) {
|
|
|
198
210
|
|
|
199
211
|
try {
|
|
200
212
|
if (Object.keys(_schema).length > 0 && _schema[collection] === undefined) {
|
|
201
|
-
|
|
213
|
+
console.error('Invalid schema for', collection, _schema[collection])
|
|
214
|
+
return res.status(404).json({error: `Schema error for collection ${collection}`})
|
|
202
215
|
}
|
|
203
216
|
const document = req.body;
|
|
204
217
|
delete document._id; // avoid schema validation error
|
|
@@ -237,7 +250,8 @@ async function patchFunc(req, res) {
|
|
|
237
250
|
const { collection, ID } = req.params;
|
|
238
251
|
try {
|
|
239
252
|
if (Object.keys(_schema).length > 0 && _schema[collection] === undefined) {
|
|
240
|
-
|
|
253
|
+
console.error('Invalid schema for', collection, _schema[collection])
|
|
254
|
+
return res.status(404).json({error: `Schema error for collection ${collection}`})
|
|
241
255
|
}
|
|
242
256
|
const document = req.body;
|
|
243
257
|
console.log(req.headers['x-real-ip'], req.method, req.originalUrl);
|
|
@@ -257,7 +271,8 @@ async function patchFunc(req, res) {
|
|
|
257
271
|
async function patchManyFunc(req, res) {
|
|
258
272
|
const { collection } = req.params;
|
|
259
273
|
if (Object.keys(_schema).length > 0 && _schema[collection] === undefined) {
|
|
260
|
-
|
|
274
|
+
console.error('Invalid schema for', collection, _schema[collection])
|
|
275
|
+
return res.status(404).json({error: `Schema error for collection ${collection}`})
|
|
261
276
|
}
|
|
262
277
|
|
|
263
278
|
try {
|
|
@@ -280,7 +295,8 @@ async function patchManyFunc(req, res) {
|
|
|
280
295
|
async function deleteFunc(req, res) {
|
|
281
296
|
const { collection, ID } = req.params;
|
|
282
297
|
if (Object.keys(_schema).length > 0 && _schema[collection] === undefined) {
|
|
283
|
-
|
|
298
|
+
console.error('Invalid schema for', collection, _schema[collection])
|
|
299
|
+
return res.status(404).json({error: `Schema error for collection ${collection}`})
|
|
284
300
|
}
|
|
285
301
|
try {
|
|
286
302
|
console.log(req.headers['x-real-ip'], req.method, req.originalUrl);
|
|
@@ -300,7 +316,8 @@ async function deleteFunc(req, res) {
|
|
|
300
316
|
async function deleteManyFunc(req, res) {
|
|
301
317
|
const { collection } = req.params;
|
|
302
318
|
if (Object.keys(_schema).length > 0 && _schema[collection] === undefined) {
|
|
303
|
-
|
|
319
|
+
console.error('Invalid schema for', collection, _schema[collection])
|
|
320
|
+
return res.status(404).json({error: `Schema error for collection ${collection}`})
|
|
304
321
|
}
|
|
305
322
|
|
|
306
323
|
try {
|