codehooks-js 1.2.12 → 1.2.14

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.
@@ -15,7 +15,6 @@ let _query = null;
15
15
  let _eventHooks = null;
16
16
 
17
17
  export default async function crudlify(app, schema = {}, options = { schema: "yup", query: "q2m", prefix: "" }) {
18
- console.log('Init crudlify', schema, options)
19
18
  _schema = schema;
20
19
  _opt = options;
21
20
  if (_opt.prefix === undefined || _opt.prefix === '/') {
@@ -111,6 +110,7 @@ async function createFunc(req, res) {
111
110
  if (_schema[collection] != undefined) {
112
111
 
113
112
  if (_schema[collection] != null) {
113
+ console.debug('validate', document)
114
114
  _validate(_schema[collection], document, _opt)
115
115
  .then(async function (value) {
116
116
  document = _cast(_schema[collection], value)
@@ -213,8 +213,7 @@ async function updateFunc(req, res) {
213
213
  document._id = ID; // put back id that was removed for validation
214
214
  const conn = await Datastore.open();
215
215
  await _eventHooks.fireBefore(collection, 'PUT', document);
216
- const result = await conn.replaceOne(collection, ID, document, {});
217
- console.debug('replaceOne result 1', result)
216
+ const result = await conn.replaceOne(collection, ID, document, {});
218
217
  await _eventHooks.fireAfter(collection, 'PUT', result);
219
218
  res.json(result);
220
219
  })
@@ -226,16 +225,20 @@ async function updateFunc(req, res) {
226
225
  document._id = ID; // put back id that was removed for validation
227
226
  const conn = await Datastore.open();
228
227
  await _eventHooks.fireBefore(collection, 'PUT', document);
229
- const result = await conn.replaceOne(collection, ID, document, {});
230
- console.debug('replaceOne result 2', result)
228
+ const result = await conn.replaceOne(collection, ID, document, {});
231
229
  await _eventHooks.fireAfter(collection, 'PUT', result);
232
230
  res.json(result);
233
231
  }
234
232
  } catch (e) {
235
- console.error(e.message)
236
- res
233
+ if (e.schemaError) {
234
+ res
235
+ .status(400) // validator
236
+ .end(e);
237
+ } else {
238
+ res
237
239
  .status(404) // not found
238
240
  .end(e.message);
241
+ }
239
242
  }
240
243
  }
241
244
 
@@ -1,20 +1,26 @@
1
1
  const debug = console.debug;
2
2
 
3
3
  export const validate = (schema, document) => {
4
- debug('Validate Yup', document, schema)
4
+
5
5
  return new Promise(async (resolve, reject) => {
6
+ debug('Validate Yup', document)
6
7
  if (schema == null) {
7
8
  debug('Null validator')
8
- resolve(true)
9
+ resolve(document)
9
10
  }
10
- const valid = await schema.validate(document);
11
-
12
- if (valid) {
13
- debug('Validate ok', valid)
14
- resolve(valid)
15
- } else {
16
- debug('Validate error', valid)
17
- resolve(valid)
11
+ try {
12
+ const valid = await schema.validate(document);
13
+ console.debug('validated result', valid)
14
+ if (valid) {
15
+ debug('Validate ok', valid)
16
+ resolve(document)
17
+ } else {
18
+ debug('Validate error', valid)
19
+ reject(valid)
20
+ }
21
+ } catch (error) {
22
+ console.error('Yup error', error)
23
+ reject(error)
18
24
  }
19
25
  });
20
26
  }
package/index.js CHANGED
@@ -22,7 +22,8 @@ class Codehooks {
22
22
  datastore = null;
23
23
  listeners = [];
24
24
  workers = {};
25
- realtime = {}
25
+ realtime = {};
26
+ startup = {};
26
27
 
27
28
  post = (path, ...hook) => {
28
29
  this.routes[`POST ${createRoute(path)}`] = hook;
@@ -118,7 +119,7 @@ class Codehooks {
118
119
  })
119
120
  }
120
121
 
121
- init = () => {
122
+ init = (...hook) => {
122
123
  const manifest = {
123
124
  workerhooks: this.workers,
124
125
  routehooks: this.routes,
@@ -131,8 +132,13 @@ class Codehooks {
131
132
  realtime: this.realtime,
132
133
  app: this
133
134
  };
135
+ if (hook) {
136
+ manifest.startup = hook;
137
+ }
134
138
  return manifest;
135
139
  };
140
+ // alias
141
+ start = this.init;
136
142
 
137
143
  useExpress = async (express, options) => {
138
144
  cronjob = import("./cronjob.mjs")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codehooks-js",
3
- "version": "1.2.12",
3
+ "version": "1.2.14",
4
4
  "type": "module",
5
5
  "description": "Codehooks.io official library - provides express.JS like syntax",
6
6
  "main": "index.js",
package/types/index.d.ts CHANGED
@@ -894,7 +894,14 @@ declare class Codehooks {
894
894
  * export default app.init();
895
895
  * @returns {Object} App instance
896
896
  */
897
- init: () => any;
897
+ init: (cb?: any) => any;
898
+ /**
899
+ * Alias for app.init()
900
+ * @example
901
+ * export default app.start();
902
+ * @returns {Object} App instance
903
+ */
904
+ start: (cb?: any) => any;
898
905
  /**
899
906
  * Use Node Express app to run standalone
900
907
  * All Codehooks routes will be applied as Express Routes