nodester 0.2.9 → 0.3.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.
@@ -0,0 +1,3 @@
1
+ const mysql2 = require('mysql2');
2
+
3
+ module.exports = mysql2;
@@ -0,0 +1,3 @@
1
+ const pg = require('pg');
2
+
3
+ module.exports = pg;
@@ -0,0 +1,3 @@
1
+ const sequelize = require('sequelize');
2
+
3
+ module.exports = sequelize;
@@ -74,7 +74,7 @@ function _parseRouteHandler(routeHandler={}) {
74
74
  result.actionName = `${ action }`;
75
75
  }
76
76
 
77
- if (typeOf(before) === 'function') {
77
+ if (!!before) {
78
78
  result.before = before;
79
79
  }
80
80
 
@@ -331,10 +331,26 @@ module.exports = class NodesterRouter {
331
331
 
332
332
  const handlerType = typeOf(handler);
333
333
 
334
- if (handlerType === 'Object' && !this.paths.controllers && !this.paths.providers) {
335
- const msg = `Please set 'controllersPath' or 'providersPath' during Router initialization.`;
336
- const err = new TypeError(msg);
337
- throw err;
334
+ if (handlerType === 'Object') {
335
+ if (!this.paths.controllers && !this.paths.providers) {
336
+ const msg = `Please set 'controllersPath' or 'providersPath' during Router initialization.`;
337
+ const err = new TypeError(msg);
338
+ throw err;
339
+ }
340
+
341
+ if (!!handler.before) {
342
+ ensure(handler.before, 'array|function', 'handler.before');
343
+
344
+ if ( Array.isArray(handler.before) ) {
345
+ const middlewares = [ ...handler.before ];
346
+
347
+ handler.before = new MiddlewaresStack();
348
+
349
+ for (let middlewareBefore of middlewares) {
350
+ handler.before.add(middlewareBefore);
351
+ }
352
+ }
353
+ }
338
354
  }
339
355
 
340
356
  const wrapped = wrapRouteHandler.call(this, parsedRoute, handler);
@@ -412,8 +428,15 @@ module.exports = class NodesterRouter {
412
428
  const markerFn = this._markers.get(markerName);
413
429
 
414
430
  return {
415
- route: (route, fn) => MarkerMethods.onlyRoute.call(self, route, fn, markerFn),
416
- use: (fnOrRouter) => MarkerMethods.onlyUse.call(self, fnOrRouter, markerFn),
431
+ route: (route, fn) => {
432
+ ensure(route, 'object,required', 'route');
433
+ ensure(fn, 'object,required', 'fn');
434
+ return MarkerMethods.onlyRoute.call(self, route, fn, markerFn)
435
+ },
436
+ use: (fnOrRouter) => {
437
+ ensure(fnOrRouter, 'function|object,required', 'fnOrRouter');
438
+ return MarkerMethods.onlyUse.call(self, fnOrRouter, markerFn);
439
+ }
417
440
  }
418
441
  }
419
442
  catch(error) {
@@ -53,7 +53,7 @@ function _onlyRoute(route, fn, markerFn) {
53
53
 
54
54
  /**
55
55
  *
56
- * @param {Function} fnOrRouter
56
+ * @param {Function|NodesterRouter} fnOrRouter
57
57
  * @param {Function} markerFn
58
58
  *
59
59
  */
@@ -67,7 +67,7 @@ function _onlyUse(fnOrRouter, markerFn) {
67
67
  return next();
68
68
  }
69
69
 
70
- const isRouter = fnOrRouter.constructor.name === 'NodesterRouter';
70
+ const isRouter = !!fnOrRouter.constructor && fnOrRouter.constructor?.name === 'NodesterRouter';
71
71
 
72
72
  const fn = isRouter ? fnOrRouter.handle.bind(fnOrRouter) : fnOrRouter;
73
73
  await fn.call(this, req, res, next);
@@ -5,6 +5,8 @@
5
5
 
6
6
  'use strict';
7
7
 
8
+ const MiddlewaresStack = require('nodester/stacks/middlewares');
9
+
8
10
  // Utils:
9
11
  const { typeOf } = require('nodester/utils/types');
10
12
  const { parseRouteHandler } = require('./handlers.util');
@@ -38,6 +40,33 @@ function _validateParsedRouteMethood(parsedRoute) {
38
40
  function _wrapRouteHandler(routeInstance, handler) {
39
41
  const handlerType = typeOf(handler);
40
42
 
43
+ let parsedHandler = null;
44
+ let providedAction = null;
45
+
46
+ if (handlerType === 'Object') {
47
+ parsedHandler = parseRouteHandler(handler);
48
+
49
+ // If Controller:
50
+ if (parsedHandler.controllerName !== undefined) {
51
+ // Get method (action) from Controller:
52
+ const controller = this._controllers.get(parsedHandler.controllerName);
53
+ const controllerAction = controller[parsedHandler.actionName];
54
+
55
+ providedAction = controllerAction;
56
+ }
57
+ // If Controller\
58
+
59
+ // If Provider:
60
+ else {
61
+ // Get method (action) from Provider:
62
+ const provider = this._providers.get(parsedHandler.providerName);
63
+ const providerAction = provider[parsedHandler.actionName];
64
+
65
+ providedAction = providerAction;
66
+ }
67
+ // If Provider\
68
+ }
69
+
41
70
  const wrapped = async (req, res, next) => {
42
71
  // Get route without SearchParams.
43
72
  const route = req.url.split('?')[0];
@@ -57,34 +86,16 @@ function _wrapRouteHandler(routeInstance, handler) {
57
86
  }
58
87
  // If handler is an Object:
59
88
  else {
60
- const parsedHandler = parseRouteHandler(handler);
61
-
62
- let providedAction = null;
63
-
64
- // If Controller:
65
- if (parsedHandler.controllerName !== undefined) {
66
- // Get method (action) from Controller:
67
- const controller = this._controllers.get(parsedHandler.controllerName);
68
- const controllerAction = controller[parsedHandler.actionName];
69
-
70
- providedAction = controllerAction;
71
- }
72
- // If Controller\
73
-
74
- // If Provider:
75
- else {
76
- // Get method (action) from Provider:
77
- const provider = this._providers.get(parsedHandler.providerName);
78
- const providerAction = provider[parsedHandler.actionName];
79
-
80
- providedAction = providerAction;
81
- }
82
- // If Provider\
83
-
84
89
  // If User set any middleware before:
85
- if (typeOf(parsedHandler.before) === 'function') {
90
+ if (!!parsedHandler.before) {
86
91
  const _next = () => providedAction(req, res);
87
- await parsedHandler.before(req, res, _next);
92
+
93
+ if (typeOf(parsedHandler.before) === 'function') {
94
+ await parsedHandler.before(req, res, _next);
95
+ }
96
+ else if (parsedHandler.before instanceof MiddlewaresStack) {
97
+ await parsedHandler.before.process(req, res, _next);
98
+ }
88
99
  }
89
100
  // If response was not sent,
90
101
  // perform action
@@ -82,7 +82,7 @@ function _ensure(argument, rules, argumentName) {
82
82
  }
83
83
 
84
84
  if (mismatchedTypesCount === types.length && argument !== undefined) {
85
- const err = new TypeError(`${ name } must be of type ${ types.join('|') }.`);
85
+ const err = new TypeError(`${ name } must be of type: ${ types.join(' or ') }.`);
86
86
  Error.captureStackTrace(err, _ensure);
87
87
  throw err;
88
88
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodester",
3
- "version": "0.2.9",
3
+ "version": "0.3.0",
4
4
  "description": "A versatile REST framework for Node.js",
5
5
  "directories": {
6
6
  "docs": "docs",
@@ -19,6 +19,10 @@
19
19
 
20
20
  "./controllers/methods": "./lib/controllers/methods/index.js",
21
21
  "./controllers/mixins": "./lib/controllers/mixins/index.js",
22
+
23
+ "./dependencies/mysql2": "./dependencies/mysql2.js",
24
+ "./dependencies/pg": "./dependencies/pg.js",
25
+ "./dependencies/sequelize": "./dependencies/sequelize.js",
22
26
 
23
27
  "./database/connection": "./lib/database/connection.js",
24
28
  "./database/migration": "./lib/database/migration.js",
@@ -61,6 +65,7 @@
61
65
  "./stacks/markers": "./lib/stacks/MarkersStack.js",
62
66
  "./stacks/middlewares": "./lib/stacks/MiddlewaresStack.js",
63
67
 
68
+ "./utils/dates": "./lib/utils/dates.js",
64
69
  "./utils/sql": "./lib/utils/sql.util.js",
65
70
  "./utils/strings": "./lib/utils/strings.util.js",
66
71
  "./utils/sanitizations": "./lib/utils/sanitizations.util.js",
File without changes