nodester 0.0.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.
Files changed (63) hide show
  1. package/LICENSE +21 -0
  2. package/Readme.md +125 -0
  3. package/docs/App.md +13 -0
  4. package/docs/Queries.md +61 -0
  5. package/docs/Readme.md +2 -0
  6. package/docs/Routing.md +34 -0
  7. package/examples/goal/index.js +23 -0
  8. package/examples/rest/index.js +25 -0
  9. package/examples/rest/node_modules/.package-lock.json +40 -0
  10. package/examples/rest/package-lock.json +72 -0
  11. package/examples/rest/package.json +14 -0
  12. package/lib/application/MiddlewareStack.js +125 -0
  13. package/lib/application/http/request.js +462 -0
  14. package/lib/application/http/response.js +1107 -0
  15. package/lib/application/http/utils.js +254 -0
  16. package/lib/application/index.js +292 -0
  17. package/lib/constants/ConstantsEnum.js +13 -0
  18. package/lib/constants/ResponseFormats.js +7 -0
  19. package/lib/controllers/Controller.js +474 -0
  20. package/lib/controllers/JWTController.js +240 -0
  21. package/lib/controllers/ServiceController.js +109 -0
  22. package/lib/controllers/WebController.js +75 -0
  23. package/lib/facades/Facade.js +388 -0
  24. package/lib/facades/FacadeParams.js +11 -0
  25. package/lib/facades/ServiceFacade.js +17 -0
  26. package/lib/facades/jwt.facade.js +273 -0
  27. package/lib/factories/errors/CustomError.js +22 -0
  28. package/lib/factories/errors/index.js +9 -0
  29. package/lib/factories/responses/api.js +90 -0
  30. package/lib/factories/responses/html.js +55 -0
  31. package/lib/logger/console.js +24 -0
  32. package/lib/models/DisabledRefreshToken.js +68 -0
  33. package/lib/models/Extractor.js +320 -0
  34. package/lib/models/define.js +62 -0
  35. package/lib/models/mixins.js +369 -0
  36. package/lib/policies/Role.js +77 -0
  37. package/lib/policies/RoleExtracting.js +97 -0
  38. package/lib/preprocessors/BodyPreprocessor.js +61 -0
  39. package/lib/preprocessors/IncludesPreprocessor.js +55 -0
  40. package/lib/preprocessors/QueryPreprocessor.js +64 -0
  41. package/lib/routers/Default/index.js +143 -0
  42. package/lib/routers/Default/layer.js +50 -0
  43. package/lib/routers/Main/index.js +10 -0
  44. package/lib/routers/Roles/index.js +81 -0
  45. package/lib/services/includes.service.js +79 -0
  46. package/lib/services/jwt.service.js +147 -0
  47. package/lib/tools/sql.tool.js +82 -0
  48. package/lib/utils/dates.util.js +23 -0
  49. package/lib/utils/forms.util.js +22 -0
  50. package/lib/utils/json.util.js +49 -0
  51. package/lib/utils/mappers/Routes/index.js +100 -0
  52. package/lib/utils/mappers/Routes/utils.js +20 -0
  53. package/lib/utils/modelAssociations.util.js +44 -0
  54. package/lib/utils/objects.util.js +69 -0
  55. package/lib/utils/params.util.js +19 -0
  56. package/lib/utils/path.util.js +26 -0
  57. package/lib/utils/queries.util.js +240 -0
  58. package/lib/utils/sanitizations.util.js +111 -0
  59. package/lib/utils/sql.util.js +78 -0
  60. package/lib/utils/strings.util.js +43 -0
  61. package/lib/utils/types.util.js +26 -0
  62. package/package.json +63 -0
  63. package/tests/index.test.js +35 -0
@@ -0,0 +1,78 @@
1
+ // Sequelize.
2
+ const { QueryTypes } = require('sequelize');
3
+
4
+ // Utils:
5
+ const fs = require('fs');
6
+ const { promisify } = require('util');
7
+ const readFile = promisify(fs.readFile);
8
+
9
+
10
+ module.exports = {
11
+ parseSQLFileContents: _parseSQLFileContents,
12
+
13
+ rawUpdate: _rawUpdate,
14
+ rawInsert: _rawInsert
15
+ }
16
+
17
+ async function _parseSQLFileContents(filePath) {
18
+ try {
19
+ const fileContent = await readFile(filePath, 'utf8');
20
+ const sqlCommands = fileContent.toString()
21
+ .split('\n')
22
+ .filter(command => command !== '');
23
+
24
+ const output = { commands: sqlCommands };
25
+ return Promise.resolve(output);
26
+ }
27
+ catch(error) {
28
+ return Promise.reject(error);
29
+ }
30
+ }
31
+
32
+ function _rawUpdate(db, tableName, where, data) {
33
+ const dataKeys = Object.keys(data);
34
+
35
+ let sqlAttributes = '';
36
+
37
+ for (let i=0; i < dataKeys.length; i++) {
38
+ const key = dataKeys[i];
39
+ const val = data[key];
40
+
41
+ sqlAttributes += `${ key }='${ val }'`;
42
+
43
+ if (i < dataKeys.length-1) {
44
+ sqlAttributes += ', ';
45
+ }
46
+ }
47
+
48
+ const sql = `UPDATE \`${ tableName }\` SET ${ sqlAttributes } where ${ where };`;
49
+
50
+ return db.query(sql, {
51
+ type: QueryTypes.INSERT
52
+ });
53
+ }
54
+
55
+ function _rawInsert(db, tableName, data) {
56
+ const dataKeys = Object.keys(data);
57
+ const dataValues = Object.values(data);
58
+
59
+ let values = '';
60
+ for (let i=0; i < dataValues.length-1; i++) {
61
+ const val = dataValues[i];
62
+
63
+ if (val === undefined || val === null)
64
+ values += 'null, ';
65
+ else if (typeof val === 'object')
66
+ values += `'${ JSON.stringify(val) }', `;
67
+ // values += `'{}', `;
68
+ else
69
+ values += `'${val}', `;
70
+ }
71
+ values += `'${ dataValues[dataValues.length-1] }'`;
72
+
73
+ const sql = `INSERT INTO \`${ tableName }\` (${ dataKeys.join(', ') }) VALUES (${ values });`;
74
+
75
+ return db.query(sql, {
76
+ type: QueryTypes.INSERT
77
+ });
78
+ }
@@ -0,0 +1,43 @@
1
+ // Utils.
2
+ const inflection = require('inflection');
3
+
4
+
5
+ module.exports = {
6
+ lowerCase: _lowerCase,
7
+ lowerCaseFirstLetter: _lowerCaseFirstLetter,
8
+
9
+ splitByComma: _splitByComma,
10
+ splitByDot: _splitByDot,
11
+ splitByAmpersand: _splitByAmpersand,
12
+
13
+ pluralize: _pluralize,
14
+ underscore: _underscore,
15
+ }
16
+
17
+ function _lowerCase(string='') {
18
+ return string.toLowerCase();
19
+ }
20
+
21
+ function _lowerCaseFirstLetter(string='') {
22
+ return string.charAt(0).toLowerCase() + string.slice(1);
23
+ }
24
+
25
+ function _splitByComma(string='') {
26
+ return string.split(',');
27
+ }
28
+
29
+ function _splitByDot(string='') {
30
+ return string.split('.');
31
+ }
32
+
33
+ function _splitByAmpersand(string='') {
34
+ return string.split('&');
35
+ }
36
+
37
+ function _pluralize(string='') {
38
+ return inflection.pluralize(string);
39
+ }
40
+
41
+ function _underscore(string='') {
42
+ return inflection.underscore(string);
43
+ }
@@ -0,0 +1,26 @@
1
+ const objectRegExp = /^\[object (\S+)\]$/;
2
+
3
+
4
+ module.exports = {
5
+ typeOf: _typeOf
6
+ }
7
+
8
+
9
+ /**
10
+ *
11
+ * @alias typeOf
12
+ * @public
13
+ */
14
+ function _typeOf(obj) {
15
+ const type = typeof obj;
16
+
17
+ if (type !== 'object') {
18
+ return type;
19
+ }
20
+
21
+ // Inspect [[Class]] for objects:
22
+ return Object.prototype
23
+ .toString
24
+ .call(obj)
25
+ .replace(objectRegExp, '$1');
26
+ }
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "nodester",
3
+ "version": "0.0.1",
4
+ "description": "A boilerplate framework for Node.js",
5
+ "exports": {
6
+ ".": "./lib/application/index.js"
7
+ },
8
+ "directories": {
9
+ "doc": "docs"
10
+ },
11
+ "source": [
12
+ "lib"
13
+ ],
14
+ "scripts": {
15
+ "examples:rest": "node ./examples/rest/index.js",
16
+ "test": "jest"
17
+ },
18
+ "author": "Mark Khramko <markkhramko@gmail.com>",
19
+ "license": "MIT",
20
+ "repository": "MarkKhramko/nodester",
21
+ "private": false,
22
+ "bugs": {
23
+ "url": "https://github.com/MarkKhramko/nodester/issues"
24
+ },
25
+ "homepage": "https://github.com/MarkKhramko/nodester#readme",
26
+ "keywords": [
27
+ "nodester",
28
+ "framework",
29
+ "web",
30
+ "http",
31
+ "rest",
32
+ "restful",
33
+ "router",
34
+ "app",
35
+ "api"
36
+ ],
37
+ "dependencies": {
38
+ "accepts": "^1.3.8",
39
+ "content-disposition": "^0.5.4",
40
+ "content-type": "^1.0.5",
41
+ "cookie": "^0.5.0",
42
+ "cookie-signature": "^1.2.0",
43
+ "debug": "^4.3.4",
44
+ "finalhandler": "^1.2.0",
45
+ "formidable": "^1.2.6",
46
+ "fresh": "^0.5.2",
47
+ "http-errors": "^2.0.0",
48
+ "inflection": "^2.0.1",
49
+ "proxy-addr": "^2.0.7",
50
+ "qs": "^6.11.0",
51
+ "range-parser": "^1.2.1",
52
+ "send": "^0.18.0",
53
+ "sequelize": "^6.6.5",
54
+ "type-is": "^1.6.18",
55
+ "vary": "^1.1.2"
56
+ },
57
+ "engines": {
58
+ "node": ">= 12.17.0"
59
+ },
60
+ "devDependencies": {
61
+ "jest": "^29.4.2"
62
+ }
63
+ }
@@ -0,0 +1,35 @@
1
+ // Test utils:
2
+ const {
3
+ describe,
4
+ it,
5
+ expect,
6
+ test
7
+ } = require('@jest/globals');
8
+
9
+ // Configs.
10
+ const PORT = 8080;
11
+
12
+ // Our lib.
13
+ const Nodester = require('../lib/application');
14
+
15
+
16
+ describe('nodester application', () => {
17
+
18
+ // Init.
19
+ const app = new Nodester();
20
+ it('construct', () => {
21
+ expect(app).toBeDefined();
22
+ });
23
+
24
+ // app.setDatabase();
25
+ // app.set.database();
26
+
27
+ test('listening port', () => {
28
+ app.listen(PORT, function() {
29
+ expect(app.port).toBe(PORT);
30
+
31
+ app.stop();
32
+ expect(app.isListening).toBe(false);
33
+ });
34
+ });
35
+ });