nodester 0.4.9 → 0.5.1

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.
@@ -478,17 +478,26 @@ module.exports = class NodesterApplication extends Emitter {
478
478
  /**
479
479
  * Stops server
480
480
  *
481
+ * @param {Function} callback
482
+ *
481
483
  * @access public
482
484
  */
483
- stop() {
485
+ stop(callback) {
484
486
  if (this._isListening !== true) {
485
- console.warn('Nothing to stop. Server is not listening.');
487
+ consl.warn('nothing to stop. Server is not listening.');
486
488
  return;
487
489
  }
488
490
 
489
- this.server.close();
490
- this._isListening = false;
491
+ this.server.close(() => {
492
+ this._isListening = false;
493
+ this._router.unlock();
491
494
 
492
- this._router.unlock();
495
+ if (typeof callback === 'function') {
496
+ callback();
497
+ }
498
+ else if (!!callback) {
499
+ consl.warn(`argument 'callback' in stop() must be of type: function.`);
500
+ }
501
+ });
493
502
  }
494
503
  }
@@ -30,13 +30,14 @@ function extract(body, filter=null, model) {
30
30
 
31
31
  const bodyEntries = Object.entries(body);
32
32
 
33
+ // Unwrap statics:
33
34
  const { statics } = filter;
34
-
35
+ const staticAttributes = statics?.attributes ?? {};
35
36
 
36
37
  // Result object.
37
38
  const filteredBody = {};
38
39
 
39
- for (const [key, value] of bodyEntries) {
40
+ for (const [ key, value ] of bodyEntries) {
40
41
  const isInclude = availableIncludes.indexOf(key) > -1;
41
42
  const isAttribute = modelAttributes.indexOf(key) > -1;
42
43
 
@@ -47,6 +48,14 @@ function extract(body, filter=null, model) {
47
48
  }
48
49
 
49
50
  if (isAttribute) {
51
+ // If this attribute must have a static value,
52
+ // skip it.
53
+ // The static value will be set later:
54
+ if (typeof staticAttributes[key] !== 'undefined') {
55
+ continue;
56
+ }
57
+
58
+ // Sanitize value from the body:
50
59
  const column = model.rawAttributes[key];
51
60
  const typeName = column.type.constructor.name;
52
61
  // Optional validation.
@@ -99,6 +108,12 @@ function extract(body, filter=null, model) {
99
108
  err.status = httpCodes.NOT_ACCEPTABLE;
100
109
  throw err;
101
110
  }
111
+
112
+ // Set all static attributes:
113
+ const staticAttributeEntries = Object.entries(staticAttributes);
114
+ for (const [ key, value ] of staticAttributeEntries) {
115
+ filteredBody[key] = staticAttributes[key];
116
+ }
102
117
 
103
118
  return filteredBody;
104
119
  }
@@ -8,11 +8,8 @@
8
8
  const { IncomingMessage } = require('http');
9
9
 
10
10
  const isIP = require('net').isIP;
11
- const typeis = require('type-is');
12
11
  const fresh = require('fresh');
13
- const parseRange = require('range-parser');
14
12
  const parse = require('parseurl');
15
- const proxyaddr = require('proxy-addr');
16
13
 
17
14
  const { defineGetter } = require('./utils');
18
15
 
@@ -7,7 +7,8 @@
7
7
 
8
8
 
9
9
  exports = module.exports = {
10
- error: _error
10
+ error: _error,
11
+ warn: _warn
11
12
  }
12
13
 
13
14
  /**
@@ -19,5 +20,19 @@ exports = module.exports = {
19
20
  * @access public
20
21
  */
21
22
  function _error(err) {
22
- console.error(err.stack || err.toString());
23
+ console.error(err.stack || err.toString());
24
+ }
25
+
26
+
27
+ /**
28
+ * Log warning with nodester prefix using console.warning.
29
+ *
30
+ * @param {Error} err
31
+ *
32
+ * @alias error
33
+ * @access public
34
+ */
35
+ function _warn(...args) {
36
+ const prefix = '[nodester] warning:';
37
+ console.warn(prefix, ...args);
23
38
  }
@@ -64,7 +64,7 @@ function traverse(queryNode, filter=null, model=null, association=null) {
64
64
  where: {},
65
65
  include: []
66
66
  };
67
-
67
+
68
68
  const {
69
69
  attributes,
70
70
  clauses,
@@ -184,18 +184,19 @@ function traverse(queryNode, filter=null, model=null, association=null) {
184
184
  const order = {};
185
185
 
186
186
  const clausesEntries = Object.entries(clauses);
187
- for (let [clauseName, value] of clausesEntries) {
187
+ for (const [ clauseName, value ] of clausesEntries) {
188
188
 
189
189
  // If clause is not available:
190
190
  if (filter != null) {
191
- if (filter.clauses.indexOf(clauseName) === -1)
191
+ if (filter.clauses.indexOf(clauseName) === -1) {
192
192
  continue;
193
+ }
193
194
  }
194
195
 
195
196
  switch(clauseName) {
196
197
  case 'limit': {
197
198
  const _value = _setValueWithBounds(value, 'number', filter.bounds.clauses.limit);
198
-
199
+
199
200
  // Do not set if -1:
200
201
  if (_value === -1)
201
202
  continue;
@@ -226,7 +227,7 @@ function traverse(queryNode, filter=null, model=null, association=null) {
226
227
 
227
228
  order.by = value;
228
229
  continue;
229
-
230
+
230
231
  default:
231
232
  continue;
232
233
  }
@@ -243,7 +244,7 @@ function traverse(queryNode, filter=null, model=null, association=null) {
243
244
  case 'limit':
244
245
  newQuery.limit = staticClauseValue;
245
246
  continue;
246
-
247
+
247
248
  case 'skip':
248
249
  newQuery.offset = staticClauseValue;
249
250
  continue;
@@ -11,6 +11,9 @@ const CLAUSES = require('../constants/Clauses');
11
11
  const { isModel } = require('../utils/models');
12
12
  const { ensure } = require('nodester/validators/arguments');
13
13
 
14
+ // Console:
15
+ const consl = require('nodester/loggers/console');
16
+
14
17
 
15
18
  /**
16
19
  * @class
@@ -107,12 +110,13 @@ module.exports = class NodesterFilter {
107
110
  // Empty bounds:
108
111
  if (!!includeFilter.statics.clauses.limit) {
109
112
  const msg = [
110
- `(nodester) warning: include "${ includeName }" has`,
111
- `association type of "${ associationType }", but has a filter clause "limit",`,
113
+ `include "${ includeName }" has association type of`,
114
+ `"${ associationType }", but has a filter clause "limit",`,
112
115
  `which is forbidden on any association type except for "HasMany".`,
113
- `It was automatically removed from clauses.`
116
+ `It was automatically removed from clauses.`,
117
+ `Consider also removing it from your code.`
114
118
  ].join(' ');
115
- console.warn(msg);
119
+ consl.warn(msg);
116
120
  }
117
121
  delete includeFilter.statics.clauses.limit;
118
122
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodester",
3
- "version": "0.4.9",
3
+ "version": "0.5.1",
4
4
  "description": "A versatile REST framework for Node.js",
5
5
  "directories": {
6
6
  "docs": "docs",
@@ -79,7 +79,7 @@
79
79
  "@js-temporal/polyfill": "^0.4.3",
80
80
  "body-parser": "^1.20.2",
81
81
  "common-js-file-extensions": "^1.0.4",
82
- "cookie": "^0.5.0",
82
+ "cookie": "^1.0.0",
83
83
  "cookie-signature": "^1.2.0",
84
84
  "debug": "^4.3.4",
85
85
  "etag": "^1.8.1",
@@ -92,17 +92,13 @@
92
92
  "mysql2": "^3.6.0",
93
93
  "pg": "^8.11.3",
94
94
  "pg-hstore": "^2.3.4",
95
- "proxy-addr": "^2.0.7",
96
95
  "qs": "^6.11.0",
97
- "range-parser": "^1.2.1",
98
- "sequelize": "^6.6.5",
99
- "type-is": "^1.6.18",
100
- "vary": "^1.1.2"
96
+ "sequelize": "^6.6.5"
101
97
  },
102
98
  "devDependencies": {
103
99
  "eslint": "^8.55.0",
104
100
  "eslint-plugin-jsdoc": "^46.9.0",
105
- "jest": "^29.4.2"
101
+ "jest": "^29.7.0"
106
102
  },
107
103
  "engines": {
108
104
  "node": ">= 12.17.0"
@@ -31,10 +31,10 @@ describe('nodester application', () => {
31
31
  expect(app.isListening).toBe(true);
32
32
  expect(app.middlewaresStack.length).toBe(4);
33
33
 
34
- app.stop();
35
-
36
- expect(app.isLocked).toBe(false);
37
- expect(app.isListening).toBe(false);
34
+ app.stop(() => {
35
+ expect(app.isLocked).toBe(false);
36
+ expect(app.isListening).toBe(false);
37
+ });
38
38
  });
39
39
  });
40
40
  });