@adaptivestone/framework 3.0.13 → 3.0.16

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/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ ### 3.0.16
2
+
3
+ [UPDATE] update deps
4
+ [FIX] fix bug with route level middleware
5
+
6
+ ### 3.0.15
7
+
8
+ [UPDATE] update deps
9
+ [UPDATE] minimum node version 16
10
+
11
+ ### 3.0.14
12
+
13
+ [NEW] now possible to show all errors during validation (default one) by parameter controllerValidationAbortEarly
14
+ [UPDATE] update deps
15
+
1
16
  ### 3.0.13
2
17
 
3
18
  [UPDATE] bug fix with "mergeParams"
@@ -1,4 +1,3 @@
1
- /* eslint-disable no-underscore-dangle */
2
1
  const path = require('path');
3
2
  const fs = require('fs').promises;
4
3
 
@@ -1,4 +1,3 @@
1
- /* eslint-disable no-underscore-dangle */
2
1
  const AbstractCommand = require('../../modules/AbstractCommand');
3
2
 
4
3
  class Migrate extends AbstractCommand {
@@ -0,0 +1,3 @@
1
+ module.exports = {
2
+ controllerValidationAbortEarly: true, // abort validation on first error and return first error
3
+ };
package/models/User.js CHANGED
@@ -1,3 +1,4 @@
1
+ /* eslint-disable no-param-reassign */
1
2
  const bcrypt = require('bcrypt');
2
3
 
3
4
  const AbstractModel = require('../modules/AbstractModel');
@@ -99,8 +100,7 @@ class User extends AbstractModel {
99
100
  avatar: this.avatar,
100
101
  name: this.name,
101
102
  email: this.email,
102
- // eslint-disable-next-line no-underscore-dangle
103
- _id: this._id,
103
+ id: this.id,
104
104
  isVerified: this.isVerified,
105
105
  permissions: this.permissions,
106
106
  locale: this.locale,
@@ -136,7 +136,8 @@ class AbstractController extends Base {
136
136
  }
137
137
  for (const path in routes[verb]) {
138
138
  const routeAdditionalMiddlewares = routeMiddlewaresReg.filter(
139
- (middleware) => middleware.path === path,
139
+ (middleware) =>
140
+ middleware.path === path && middleware.method === verb,
140
141
  );
141
142
  let routeObject = routes[verb][path];
142
143
  routeObjectClone = merge({}, routeObject);
@@ -188,7 +189,8 @@ class AbstractController extends Base {
188
189
  new MiddlewareFunction(this.app, params).getMiddleware(),
189
190
  );
190
191
  }
191
-
192
+ const { controllerValidationAbortEarly } =
193
+ this.app.getConfig('validate');
192
194
  this.router[verb](
193
195
  path,
194
196
  additionalMiddlewares || [],
@@ -203,7 +205,9 @@ class AbstractController extends Base {
203
205
  const bodyAndQuery = merge(req.query, req.body);
204
206
 
205
207
  try {
206
- await routeObject.request.validate(bodyAndQuery);
208
+ await routeObject.request.validate(bodyAndQuery, {
209
+ abortEarly: controllerValidationAbortEarly,
210
+ });
207
211
  } catch (e) {
208
212
  let { errors } = e;
209
213
  // translate it
@@ -214,10 +218,22 @@ class AbstractController extends Base {
214
218
  `Request validation failed with message: ${e.message}. errors: ${errors}`,
215
219
  );
216
220
 
221
+ const errorAnswer = {};
222
+ if (!e.inner.length) {
223
+ errorAnswer[e.path] = errors;
224
+ } else {
225
+ e.inner.forEach((err) => {
226
+ errorAnswer[err.path] = err.errors;
227
+ if (req.i18n && err.errors) {
228
+ errorAnswer[err.path] = err.errors.map((err1) =>
229
+ req.i18n.t(err1),
230
+ );
231
+ }
232
+ });
233
+ }
234
+
217
235
  return res.status(400).json({
218
- errors: {
219
- [e.path]: errors,
220
- },
236
+ errors: errorAnswer,
221
237
  });
222
238
  }
223
239
  req.appInfo.request = routeObject.request.cast(bodyAndQuery, {
package/modules/Base.d.ts CHANGED
@@ -3,7 +3,7 @@ import Server from '../server';
3
3
 
4
4
  declare class Base {
5
5
  app: Server['app'];
6
- _realLogger: null;
6
+ #realLogger: null;
7
7
 
8
8
  constructor(app: Server['app']);
9
9
 
package/modules/Base.js CHANGED
@@ -1,11 +1,11 @@
1
- /* eslint-disable no-underscore-dangle */
2
1
  const fs = require('fs').promises;
3
2
  const { join } = require('path');
4
3
 
5
4
  class Base {
5
+ #realLogger = null;
6
+
6
7
  constructor(app) {
7
8
  this.app = app;
8
- this._realLogger = null;
9
9
  }
10
10
 
11
11
  /**
@@ -19,12 +19,12 @@ class Base {
19
19
  * Optimzation to lazy load logger. It will be inited only on request
20
20
  */
21
21
  get logger() {
22
- if (!this._realLogger) {
23
- this._realLogger = this.getLogger(
22
+ if (!this.#realLogger) {
23
+ this.#realLogger = this.getLogger(
24
24
  this.constructor.loggerGroup + this.getConstructorName(),
25
25
  );
26
26
  }
27
- return this._realLogger;
27
+ return this.#realLogger;
28
28
  }
29
29
 
30
30
  /**
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "@adaptivestone/framework",
3
- "version": "3.0.13",
3
+ "version": "3.0.16",
4
4
  "description": "Adaptive stone node js framework",
5
- "main": "index.js",
5
+ "main": "src/index.js",
6
+ "engines": {
7
+ "node": ">=16.0.0"
8
+ },
6
9
  "repository": {
7
10
  "type": "git",
8
11
  "url": "https://gitlab.com/adaptivestone/framework"
@@ -55,7 +58,7 @@
55
58
  "eslint-plugin-import": "^2.23.4",
56
59
  "eslint-plugin-jest": "^26.0.0",
57
60
  "husky": "^7.0.0",
58
- "jest": "^27.0.6",
61
+ "jest": "^28.0.0",
59
62
  "lint-staged": "^12.0.0",
60
63
  "mongodb-memory-server": "^8.0.2",
61
64
  "nodemon": "^2.0.12",