@adaptivestone/framework 5.0.0-beta.4 → 5.0.0-beta.40
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 +1107 -528
- package/LICENCE +1 -1
- package/dist/Cli.d.ts +7 -0
- package/dist/Cli.js +19 -0
- package/dist/Cli.js.map +1 -0
- package/dist/cluster.d.ts +1 -0
- package/dist/cluster.js +18 -0
- package/dist/cluster.js.map +1 -0
- package/dist/commands/CreateUser.d.ts +32 -0
- package/dist/commands/CreateUser.js +83 -0
- package/dist/commands/CreateUser.js.map +1 -0
- package/dist/commands/Documentation.d.ts +5 -0
- package/dist/commands/Documentation.js +15 -0
- package/dist/commands/Documentation.js.map +1 -0
- package/dist/commands/DropIndex.d.ts +16 -0
- package/dist/commands/DropIndex.js +30 -0
- package/dist/commands/DropIndex.js.map +1 -0
- package/dist/commands/GenerateRandomBytes.d.ts +7 -0
- package/dist/commands/GenerateRandomBytes.js +18 -0
- package/dist/commands/GenerateRandomBytes.js.map +1 -0
- package/dist/commands/GenerateTypes.d.ts +11 -0
- package/dist/commands/GenerateTypes.js +52 -0
- package/dist/commands/GenerateTypes.js.map +1 -0
- package/dist/commands/GetOpenApiJson.d.ts +17 -0
- package/dist/commands/GetOpenApiJson.js +272 -0
- package/dist/commands/GetOpenApiJson.js.map +1 -0
- package/dist/commands/SyncIndexes.d.ts +6 -0
- package/dist/commands/SyncIndexes.js +31 -0
- package/dist/commands/SyncIndexes.js.map +1 -0
- package/dist/commands/migration/Create.d.ts +18 -0
- package/dist/commands/migration/Create.js +57 -0
- package/dist/commands/migration/Create.js.map +1 -0
- package/dist/commands/migration/Migrate.d.ts +6 -0
- package/dist/commands/migration/Migrate.js +43 -0
- package/dist/commands/migration/Migrate.js.map +1 -0
- package/dist/config/auth.d.ts +6 -0
- package/dist/config/auth.js +7 -0
- package/dist/config/auth.js.map +1 -0
- package/dist/config/http.d.ts +8 -0
- package/dist/config/http.js +10 -0
- package/dist/config/http.js.map +1 -0
- package/dist/config/i18n.d.ts +13 -0
- package/dist/config/i18n.js +13 -0
- package/dist/config/i18n.js.map +1 -0
- package/dist/config/ipDetector.d.ts +5 -0
- package/dist/config/ipDetector.js +15 -0
- package/dist/config/ipDetector.js.map +1 -0
- package/dist/config/log.d.ts +11 -0
- package/dist/config/log.js +20 -0
- package/dist/config/log.js.map +1 -0
- package/dist/config/mongo.d.ts +4 -0
- package/dist/config/mongo.js +4 -0
- package/dist/config/mongo.js.map +1 -0
- package/dist/config/rateLimiter.d.ts +13 -0
- package/dist/config/rateLimiter.js +17 -0
- package/dist/config/rateLimiter.js.map +1 -0
- package/dist/config/redis.d.ts +5 -0
- package/dist/config/redis.js +5 -0
- package/dist/config/redis.js.map +1 -0
- package/dist/config/validate.d.ts +4 -0
- package/dist/config/validate.js +4 -0
- package/dist/config/validate.js.map +1 -0
- package/dist/controllers/Auth.d.ts +131 -0
- package/dist/controllers/Auth.js +186 -0
- package/dist/controllers/Auth.js.map +1 -0
- package/dist/controllers/Home.d.ts +15 -0
- package/dist/controllers/Home.js +22 -0
- package/dist/controllers/Home.js.map +1 -0
- package/dist/controllers/index.d.ts +16 -0
- package/dist/controllers/index.js +59 -0
- package/dist/controllers/index.js.map +1 -0
- package/dist/folderConfig.d.ts +34 -0
- package/dist/folderConfig.js +14 -0
- package/dist/folderConfig.js.map +1 -0
- package/dist/helpers/appInstance.d.ts +3 -0
- package/dist/helpers/appInstance.js +8 -0
- package/dist/helpers/appInstance.js.map +1 -0
- package/dist/helpers/crypto.d.ts +3 -0
- package/dist/helpers/crypto.js +17 -0
- package/dist/helpers/crypto.js.map +1 -0
- package/dist/helpers/files.d.ts +16 -0
- package/dist/helpers/files.js +76 -0
- package/dist/helpers/files.js.map +1 -0
- package/dist/helpers/logger.d.ts +4 -0
- package/dist/helpers/logger.js +20 -0
- package/dist/helpers/logger.js.map +1 -0
- package/dist/helpers/redis/clearNamespace.d.ts +2 -0
- package/dist/helpers/redis/clearNamespace.js +14 -0
- package/dist/helpers/redis/clearNamespace.js.map +1 -0
- package/dist/helpers/yup.d.ts +13 -0
- package/dist/helpers/yup.js +21 -0
- package/dist/helpers/yup.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/models/Lock.d.ts +90 -0
- package/dist/models/Lock.js +97 -0
- package/dist/models/Lock.js.map +1 -0
- package/dist/models/Migration.d.ts +13 -0
- package/dist/models/Migration.js +14 -0
- package/dist/models/Migration.js.map +1 -0
- package/dist/models/Sequence.d.ts +28 -0
- package/dist/models/Sequence.js +19 -0
- package/dist/models/Sequence.js.map +1 -0
- package/dist/models/User.d.ts +656 -0
- package/dist/models/User.js +291 -0
- package/dist/models/User.js.map +1 -0
- package/dist/models/UserOld.d.ts +179 -0
- package/dist/models/UserOld.js +230 -0
- package/dist/models/UserOld.js.map +1 -0
- package/dist/modules/AbstractCommand.d.ts +51 -0
- package/dist/modules/AbstractCommand.js +60 -0
- package/dist/modules/AbstractCommand.js.map +1 -0
- package/dist/modules/AbstractConnector.d.ts +5 -0
- package/dist/modules/AbstractConnector.js +8 -0
- package/dist/modules/AbstractConnector.js.map +1 -0
- package/dist/modules/AbstractController.d.ts +94 -0
- package/dist/modules/AbstractController.js +323 -0
- package/dist/modules/AbstractController.js.map +1 -0
- package/dist/modules/AbstractModel.d.ts +29 -0
- package/dist/modules/AbstractModel.js +42 -0
- package/dist/modules/AbstractModel.js.map +1 -0
- package/dist/modules/Base.d.ts +29 -0
- package/dist/modules/Base.js +58 -0
- package/dist/modules/Base.js.map +1 -0
- package/dist/modules/BaseCli.d.ts +25 -0
- package/dist/modules/BaseCli.js +147 -0
- package/dist/modules/BaseCli.js.map +1 -0
- package/dist/modules/BaseModel.d.ts +55 -0
- package/dist/modules/BaseModel.js +37 -0
- package/dist/modules/BaseModel.js.map +1 -0
- package/dist/server.d.ts +123 -0
- package/dist/server.js +468 -0
- package/dist/server.js.map +1 -0
- package/dist/services/cache/Cache.d.ts +31 -0
- package/dist/services/cache/Cache.js +113 -0
- package/dist/services/cache/Cache.js.map +1 -0
- package/dist/services/documentation/DocumentationGenerator.d.ts +11 -0
- package/dist/services/documentation/DocumentationGenerator.js +130 -0
- package/dist/services/documentation/DocumentationGenerator.js.map +1 -0
- package/dist/services/http/HttpServer.d.ts +35 -0
- package/dist/services/http/HttpServer.js +70 -0
- package/dist/services/http/HttpServer.js.map +1 -0
- package/dist/services/http/middleware/AbstractMiddleware.d.ts +25 -0
- package/dist/services/http/middleware/AbstractMiddleware.js +41 -0
- package/dist/services/http/middleware/AbstractMiddleware.js.map +1 -0
- package/dist/services/http/middleware/Auth.d.ts +9 -0
- package/dist/services/http/middleware/Auth.js +18 -0
- package/dist/services/http/middleware/Auth.js.map +1 -0
- package/dist/services/http/middleware/Cors.d.ts +12 -0
- package/dist/services/http/middleware/Cors.js +36 -0
- package/dist/services/http/middleware/Cors.js.map +1 -0
- package/dist/services/http/middleware/GetUserByToken.d.ts +20 -0
- package/dist/services/http/middleware/GetUserByToken.js +39 -0
- package/dist/services/http/middleware/GetUserByToken.js.map +1 -0
- package/dist/services/http/middleware/I18n.d.ts +23 -0
- package/dist/services/http/middleware/I18n.js +61 -0
- package/dist/services/http/middleware/I18n.js.map +1 -0
- package/dist/services/http/middleware/IpDetector.d.ts +14 -0
- package/dist/services/http/middleware/IpDetector.js +55 -0
- package/dist/services/http/middleware/IpDetector.js.map +1 -0
- package/dist/services/http/middleware/Pagination.d.ts +27 -0
- package/dist/services/http/middleware/Pagination.js +46 -0
- package/dist/services/http/middleware/Pagination.js.map +1 -0
- package/dist/services/http/middleware/PrepareAppInfo.d.ts +8 -0
- package/dist/services/http/middleware/PrepareAppInfo.js +17 -0
- package/dist/services/http/middleware/PrepareAppInfo.js.map +1 -0
- package/dist/services/http/middleware/RateLimiter.d.ts +26 -0
- package/dist/services/http/middleware/RateLimiter.js +118 -0
- package/dist/services/http/middleware/RateLimiter.js.map +1 -0
- package/dist/services/http/middleware/RequestLogger.d.ts +8 -0
- package/dist/services/http/middleware/RequestLogger.js +18 -0
- package/dist/services/http/middleware/RequestLogger.js.map +1 -0
- package/dist/services/http/middleware/RequestParser.d.ts +8 -0
- package/dist/services/http/middleware/RequestParser.js +35 -0
- package/dist/services/http/middleware/RequestParser.js.map +1 -0
- package/dist/services/http/middleware/Role.d.ts +12 -0
- package/dist/services/http/middleware/Role.js +24 -0
- package/dist/services/http/middleware/Role.js.map +1 -0
- package/dist/services/i18n/I18n.d.ts +15 -0
- package/dist/services/i18n/I18n.js +58 -0
- package/dist/services/i18n/I18n.js.map +1 -0
- package/dist/services/logging/SentryTransport.d.ts +14 -0
- package/dist/services/logging/SentryTransport.js +57 -0
- package/dist/services/logging/SentryTransport.js.map +1 -0
- package/dist/services/validate/ValidateService.d.ts +31 -0
- package/dist/services/validate/ValidateService.js +95 -0
- package/dist/services/validate/ValidateService.js.map +1 -0
- package/dist/services/validate/drivers/AbstractValidator.d.ts +14 -0
- package/dist/services/validate/drivers/AbstractValidator.js +29 -0
- package/dist/services/validate/drivers/AbstractValidator.js.map +1 -0
- package/dist/services/validate/drivers/CustomValidator.d.ts +14 -0
- package/dist/services/validate/drivers/CustomValidator.js +48 -0
- package/dist/services/validate/drivers/CustomValidator.js.map +1 -0
- package/dist/services/validate/drivers/YupValidator.d.ts +13 -0
- package/dist/services/validate/drivers/YupValidator.js +86 -0
- package/dist/services/validate/drivers/YupValidator.js.map +1 -0
- package/dist/tests/frameworkVitestSetup.d.ts +1 -0
- package/dist/tests/frameworkVitestSetup.js +9 -0
- package/dist/tests/frameworkVitestSetup.js.map +1 -0
- package/dist/tests/globalSetupVitest.d.ts +3 -0
- package/dist/tests/globalSetupVitest.js +29 -0
- package/dist/tests/globalSetupVitest.js.map +1 -0
- package/dist/tests/setupVitest.d.ts +1 -0
- package/dist/tests/setupVitest.js +91 -0
- package/dist/tests/setupVitest.js.map +1 -0
- package/dist/tests/testHelpers.d.ts +340 -0
- package/dist/tests/testHelpers.js +48 -0
- package/dist/tests/testHelpers.js.map +1 -0
- package/package.json +45 -39
- package/Cli.js +0 -22
- package/cluster.js +0 -27
- package/commands/CreateUser.js +0 -75
- package/commands/Documentation.js +0 -17
- package/commands/DropIndex.js +0 -29
- package/commands/GenerateRandomBytes.js +0 -21
- package/commands/GetOpenApiJson.js +0 -325
- package/commands/SyncIndexes.js +0 -39
- package/commands/migration/Create.js +0 -61
- package/commands/migration/Migrate.js +0 -55
- package/config/auth.js +0 -9
- package/config/http.js +0 -9
- package/config/i18n.js +0 -12
- package/config/ipDetector.js +0 -14
- package/config/log.js +0 -22
- package/config/mail.js +0 -29
- package/config/mongo.js +0 -3
- package/config/rateLimiter.js +0 -16
- package/config/redis.js +0 -4
- package/config/validate.js +0 -3
- package/controllers/Auth.js +0 -210
- package/controllers/Home.js +0 -28
- package/controllers/index.js +0 -60
- package/folderConfig.js +0 -14
- package/helpers/files.js +0 -79
- package/helpers/logger.js +0 -17
- package/helpers/redis/clearNamespace.js +0 -14
- package/helpers/yup.js +0 -24
- package/index.js +0 -8
- package/jsconfig.json +0 -9
- package/locales/en/translation.json +0 -27
- package/locales/ru/translation.json +0 -27
- package/migrations/.gitkeep +0 -0
- package/models/Migration.js +0 -15
- package/models/Sequence.js +0 -22
- package/models/User.js +0 -263
- package/modules/AbstractCommand.js +0 -43
- package/modules/AbstractConnector.js +0 -9
- package/modules/AbstractController.js +0 -413
- package/modules/AbstractModel.d.ts +0 -48
- package/modules/AbstractModel.js +0 -92
- package/modules/Base.d.ts +0 -37
- package/modules/Base.js +0 -63
- package/modules/BaseCli.js +0 -97
- package/server.d.ts +0 -98
- package/server.js +0 -438
- package/services/cache/Cache.d.ts +0 -35
- package/services/cache/Cache.js +0 -124
- package/services/documentation/DocumentationGenerator.js +0 -169
- package/services/http/HttpServer.js +0 -96
- package/services/http/middleware/AbstractMiddleware.js +0 -51
- package/services/http/middleware/Auth.js +0 -20
- package/services/http/middleware/Cors.js +0 -46
- package/services/http/middleware/GetUserByToken.js +0 -47
- package/services/http/middleware/I18n.js +0 -117
- package/services/http/middleware/IpDetector.js +0 -59
- package/services/http/middleware/Pagination.js +0 -57
- package/services/http/middleware/PrepareAppInfo.js +0 -18
- package/services/http/middleware/RateLimiter.js +0 -134
- package/services/http/middleware/RequestLogger.js +0 -22
- package/services/http/middleware/RequestParser.js +0 -40
- package/services/http/middleware/Role.js +0 -29
- package/services/messaging/email/index.js +0 -217
- package/services/messaging/email/resources/.gitkeep +0 -1
- package/services/messaging/email/templates/emptyTemplate/html.pug +0 -9
- package/services/messaging/email/templates/emptyTemplate/subject.pug +0 -1
- package/services/messaging/email/templates/emptyTemplate/text.pug +0 -1
- package/services/messaging/email/templates/recovery/html.pug +0 -8
- package/services/messaging/email/templates/recovery/subject.pug +0 -2
- package/services/messaging/email/templates/recovery/text.pug +0 -3
- package/services/messaging/email/templates/verification/html.pug +0 -10
- package/services/messaging/email/templates/verification/subject.pug +0 -1
- package/services/messaging/email/templates/verification/text.pug +0 -1
- package/services/messaging/index.js +0 -3
- package/services/validate/ValidateService.js +0 -157
- package/services/validate/drivers/AbstractValidator.js +0 -37
- package/services/validate/drivers/CustomValidator.js +0 -51
- package/services/validate/drivers/YupValidator.js +0 -103
- package/tests/globalSetupVitest.js +0 -35
- package/tests/setup.js +0 -118
- package/tests/setupVitest.js +0 -109
- package/types/Expand.d.ts +0 -11
- package/types/TFoldersConfig.d.ts +0 -17
- package/views/404.pug +0 -3
- package/views/home.pug +0 -3
- package/views/layouts/base.pug +0 -39
- package/vitest.config.js +0 -16
- /package/{commands → dist/migrations}/.gitkeep +0 -0
|
@@ -1,413 +0,0 @@
|
|
|
1
|
-
/* eslint-disable no-restricted-syntax */
|
|
2
|
-
/* eslint-disable guard-for-in */
|
|
3
|
-
import express from 'express';
|
|
4
|
-
|
|
5
|
-
import Base from './Base.js';
|
|
6
|
-
import GetUserByToken from '../services/http/middleware/GetUserByToken.js';
|
|
7
|
-
import Auth from '../services/http/middleware/Auth.js';
|
|
8
|
-
import ValidateService from '../services/validate/ValidateService.js';
|
|
9
|
-
import DocumentationGenerator from '../services/documentation/DocumentationGenerator.js';
|
|
10
|
-
/**
|
|
11
|
-
* Abstract controller. You should extend any controller from them.
|
|
12
|
-
* Place you cintroller into controller folder and it be inited in auto way.
|
|
13
|
-
* By default name of route will be controller name not file name. But please name it in same ways.
|
|
14
|
-
* You can overwrite base controllers byt creating controllers with tha same file name (yes file name, not class name)
|
|
15
|
-
* In most cases you will want to have a 'home' route that not include controller name. For this case please check ' getHttpPath'
|
|
16
|
-
*/
|
|
17
|
-
class AbstractController extends Base {
|
|
18
|
-
constructor(app, prefix, isExpressMergeParams = false) {
|
|
19
|
-
const time = Date.now();
|
|
20
|
-
super(app);
|
|
21
|
-
this.prefix = prefix;
|
|
22
|
-
this.router = express.Router({
|
|
23
|
-
mergeParams: isExpressMergeParams,
|
|
24
|
-
});
|
|
25
|
-
const { routes } = this;
|
|
26
|
-
let httpPath = this.getHttpPath();
|
|
27
|
-
|
|
28
|
-
// @ts-ignore
|
|
29
|
-
if (this.getExpressPath) {
|
|
30
|
-
this.logger.warn(
|
|
31
|
-
`getExpressPath deprecated. Please use getHttpPath instead. Will be removed on v5`,
|
|
32
|
-
);
|
|
33
|
-
// @ts-ignore
|
|
34
|
-
httpPath = this.getExpressPath();
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Grab route middleware onlo one Map
|
|
39
|
-
*/
|
|
40
|
-
const routeMiddlewares = new Map();
|
|
41
|
-
Object.entries(routes).forEach(([method, methodRoutes]) => {
|
|
42
|
-
Object.entries(methodRoutes).forEach(([route, routeParam]) => {
|
|
43
|
-
if (routeParam?.middleware) {
|
|
44
|
-
const fullRoute = method.toUpperCase() + route;
|
|
45
|
-
|
|
46
|
-
if (!routeMiddlewares.has(fullRoute)) {
|
|
47
|
-
routeMiddlewares.set(fullRoute, []);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
routeMiddlewares.set(fullRoute, [
|
|
51
|
-
...routeMiddlewares.get(fullRoute),
|
|
52
|
-
...routeParam.middleware,
|
|
53
|
-
]);
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
const routeMiddlewaresReg = this.parseMiddlewares(
|
|
59
|
-
routeMiddlewares,
|
|
60
|
-
httpPath,
|
|
61
|
-
);
|
|
62
|
-
const middlewaresInfo = this.parseMiddlewares(
|
|
63
|
-
// @ts-ignore
|
|
64
|
-
this.constructor.middleware,
|
|
65
|
-
httpPath,
|
|
66
|
-
);
|
|
67
|
-
|
|
68
|
-
const routesInfo = [];
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Register controller middleware
|
|
72
|
-
*/
|
|
73
|
-
for (const middleware of middlewaresInfo) {
|
|
74
|
-
this.router[middleware.method](
|
|
75
|
-
middleware.path,
|
|
76
|
-
new middleware.MiddlewareFunction(
|
|
77
|
-
this.app,
|
|
78
|
-
middleware.params,
|
|
79
|
-
).getMiddleware(),
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Register routes itself
|
|
85
|
-
*/
|
|
86
|
-
for (const verb in routes) {
|
|
87
|
-
if (typeof this.router[verb] !== 'function') {
|
|
88
|
-
this.logger.error(
|
|
89
|
-
`Method ${verb} not exist for router. Please check your codebase`,
|
|
90
|
-
);
|
|
91
|
-
// eslint-disable-next-line no-continue
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
for (const path in routes[verb]) {
|
|
95
|
-
const routeAdditionalMiddlewares = routeMiddlewaresReg.filter(
|
|
96
|
-
(middleware) =>
|
|
97
|
-
middleware.path === path && middleware.method === verb,
|
|
98
|
-
);
|
|
99
|
-
|
|
100
|
-
let routeObject = routes[verb][path];
|
|
101
|
-
if (Object.prototype.toString.call(routeObject) !== '[object Object]') {
|
|
102
|
-
routeObject = {
|
|
103
|
-
handler: routeObject,
|
|
104
|
-
request: null,
|
|
105
|
-
query: null,
|
|
106
|
-
middleware: null,
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
if (typeof routeObject.handler !== 'function') {
|
|
110
|
-
this.logger.error(
|
|
111
|
-
`Can't resolve function '${
|
|
112
|
-
routeObject.handler
|
|
113
|
-
}' for controller '${this.getConstructorName()}'`,
|
|
114
|
-
);
|
|
115
|
-
// eslint-disable-next-line no-continue
|
|
116
|
-
continue;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
let fnName = routeObject.handler;
|
|
121
|
-
if (typeof fnName === 'function') {
|
|
122
|
-
fnName = fnName.name;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
const fullPath = `/${httpPath}/${path}`
|
|
126
|
-
.split('//')
|
|
127
|
-
.join('/')
|
|
128
|
-
.split('//')
|
|
129
|
-
.join('/');
|
|
130
|
-
|
|
131
|
-
routesInfo.push({
|
|
132
|
-
name: fnName,
|
|
133
|
-
description: routeObject?.description,
|
|
134
|
-
method: verb.toUpperCase(),
|
|
135
|
-
fields: routeObject?.request?.fields,
|
|
136
|
-
queryFields: routeObject?.query?.fields,
|
|
137
|
-
path,
|
|
138
|
-
fullPath,
|
|
139
|
-
});
|
|
140
|
-
// this.logger.verbose(
|
|
141
|
-
// `Controller '${this.getConstructorName()}' register function '${fnName}' for method '${verb}' and path '${path}' Full path '${fullPath}'`,
|
|
142
|
-
// );
|
|
143
|
-
|
|
144
|
-
let additionalMiddlewares;
|
|
145
|
-
|
|
146
|
-
if (routeAdditionalMiddlewares.length > 0) {
|
|
147
|
-
additionalMiddlewares = Array.from(
|
|
148
|
-
routeAdditionalMiddlewares,
|
|
149
|
-
({ MiddlewareFunction, params }) =>
|
|
150
|
-
new MiddlewareFunction(this.app, params).getMiddleware(),
|
|
151
|
-
);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
this.router[verb](
|
|
155
|
-
path,
|
|
156
|
-
additionalMiddlewares || [],
|
|
157
|
-
async (req, res, next) => {
|
|
158
|
-
const requestObj = {
|
|
159
|
-
query: req.query,
|
|
160
|
-
body: req.body,
|
|
161
|
-
appInfo: req.appInfo,
|
|
162
|
-
};
|
|
163
|
-
try {
|
|
164
|
-
req.appInfo.request = await new ValidateService(
|
|
165
|
-
this.app,
|
|
166
|
-
routeObject?.request,
|
|
167
|
-
).validateReqData(requestObj, {
|
|
168
|
-
selectedReqData: req.body,
|
|
169
|
-
additionalMiddlewareFieldsData: {
|
|
170
|
-
middlewaresInfo,
|
|
171
|
-
routeMiddlewaresReg,
|
|
172
|
-
options: {
|
|
173
|
-
method: verb,
|
|
174
|
-
path: fullPath,
|
|
175
|
-
prefix: 'request',
|
|
176
|
-
},
|
|
177
|
-
},
|
|
178
|
-
});
|
|
179
|
-
req.appInfo.query = await new ValidateService(
|
|
180
|
-
this.app,
|
|
181
|
-
routeObject?.query,
|
|
182
|
-
).validateReqData(requestObj, {
|
|
183
|
-
selectedReqData: req.query,
|
|
184
|
-
additionalMiddlewareFieldsData: {
|
|
185
|
-
middlewaresInfo,
|
|
186
|
-
routeMiddlewaresReg,
|
|
187
|
-
options: {
|
|
188
|
-
method: verb,
|
|
189
|
-
path: fullPath,
|
|
190
|
-
prefix: 'query',
|
|
191
|
-
},
|
|
192
|
-
},
|
|
193
|
-
});
|
|
194
|
-
} catch (err) {
|
|
195
|
-
return res.status(400).json({
|
|
196
|
-
errors: err.message,
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
// req.body = new Proxy(req.body, {
|
|
200
|
-
// get: (target, prop) => {
|
|
201
|
-
// this.logger.warn(
|
|
202
|
-
// 'Please not use "req.body" directly. Implement "request" and use "req.appInfo.request" ',
|
|
203
|
-
// );
|
|
204
|
-
// return target[prop];
|
|
205
|
-
// },
|
|
206
|
-
// });
|
|
207
|
-
// req.query = new Proxy(req.query, {
|
|
208
|
-
// get: (target, prop) => {
|
|
209
|
-
// this.logger.warn(
|
|
210
|
-
// 'Please not use "req.query" directly. Implement "query" and use "req.appInfo.query" ',
|
|
211
|
-
// );
|
|
212
|
-
// return target[prop];
|
|
213
|
-
// },
|
|
214
|
-
// });
|
|
215
|
-
|
|
216
|
-
if (!routeObject.handler) {
|
|
217
|
-
this.logger.error(`Route object have no handler defined`);
|
|
218
|
-
return res.status(500).json({
|
|
219
|
-
message:
|
|
220
|
-
'Platform error 2. Please check later or contact support',
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
if (routeObject.handler.constructor.name !== 'AsyncFunction') {
|
|
225
|
-
const error =
|
|
226
|
-
"Handler should be AsyncFunction. Perhabs you miss 'async' of function declaration?";
|
|
227
|
-
this.logger.error(error);
|
|
228
|
-
return res.status(500).json({
|
|
229
|
-
message:
|
|
230
|
-
'Platform error. Please check later or contact support',
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
return routeObject.handler.call(this, req, res, next).catch((e) => {
|
|
234
|
-
this.logger.error(e);
|
|
235
|
-
return res.status(500).json({
|
|
236
|
-
message:
|
|
237
|
-
'Platform error. Please check later or contact support',
|
|
238
|
-
});
|
|
239
|
-
});
|
|
240
|
-
},
|
|
241
|
-
);
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* Generate text info
|
|
247
|
-
*/
|
|
248
|
-
const text = ['', `Controller '${this.getConstructorName()}' registered.`];
|
|
249
|
-
|
|
250
|
-
const reports = {
|
|
251
|
-
'Middlewares:': middlewaresInfo,
|
|
252
|
-
'Route middlewares:': routeMiddlewaresReg,
|
|
253
|
-
'Callbacks:': routesInfo,
|
|
254
|
-
};
|
|
255
|
-
for (const key in reports) {
|
|
256
|
-
text.push(`${key}`);
|
|
257
|
-
for (const item of reports[key]) {
|
|
258
|
-
text.push(
|
|
259
|
-
`Path:'${item.path}'. Full path: '${
|
|
260
|
-
item.fullPath
|
|
261
|
-
}'. Method: '${item.method.toUpperCase()}'. Function: '${item.name}'`,
|
|
262
|
-
);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
text.push(`Time: ${Date.now() - time} ms`);
|
|
267
|
-
|
|
268
|
-
this.logger.verbose(text.join('\n'));
|
|
269
|
-
this.loadedTime = Date.now() - time;
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* Generate documentation
|
|
273
|
-
*/
|
|
274
|
-
if (!this.app.httpServer) {
|
|
275
|
-
this.app.documentation.push(
|
|
276
|
-
new DocumentationGenerator(this.app).convertDataToDocumentationElement(
|
|
277
|
-
this.getConstructorName(),
|
|
278
|
-
routesInfo,
|
|
279
|
-
middlewaresInfo,
|
|
280
|
-
routeMiddlewaresReg,
|
|
281
|
-
),
|
|
282
|
-
);
|
|
283
|
-
} else {
|
|
284
|
-
this.app.httpServer.express.use(httpPath, this.router);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
/**
|
|
289
|
-
* Parse middlewares to be an object.
|
|
290
|
-
*/
|
|
291
|
-
parseMiddlewares(middlewareMap, httpPath) {
|
|
292
|
-
const middlewaresInfo = [];
|
|
293
|
-
// eslint-disable-next-line prefer-const
|
|
294
|
-
for (let [path, middleware] of middlewareMap) {
|
|
295
|
-
if (!Array.isArray(middleware)) {
|
|
296
|
-
middleware = [middleware];
|
|
297
|
-
}
|
|
298
|
-
for (const M of middleware) {
|
|
299
|
-
let method = 'all';
|
|
300
|
-
let realPath = path;
|
|
301
|
-
if (typeof realPath !== 'string') {
|
|
302
|
-
this.logger.error(`Path not a string ${realPath}. Please check it`);
|
|
303
|
-
// eslint-disable-next-line no-continue
|
|
304
|
-
continue;
|
|
305
|
-
}
|
|
306
|
-
if (!realPath.startsWith('/')) {
|
|
307
|
-
method = realPath.split('/')[0]?.toLowerCase();
|
|
308
|
-
if (!method) {
|
|
309
|
-
this.logger.error(`Method not found for ${realPath}`);
|
|
310
|
-
// eslint-disable-next-line no-continue
|
|
311
|
-
continue;
|
|
312
|
-
}
|
|
313
|
-
realPath = realPath.substring(method.length);
|
|
314
|
-
}
|
|
315
|
-
if (typeof this.router[method] !== 'function') {
|
|
316
|
-
this.logger.error(
|
|
317
|
-
`Method ${method} not exist for middleware. Please check your codebase`,
|
|
318
|
-
);
|
|
319
|
-
// eslint-disable-next-line no-continue
|
|
320
|
-
continue;
|
|
321
|
-
}
|
|
322
|
-
const fullPath = `/${httpPath}/${realPath.toUpperCase()}`
|
|
323
|
-
.split('//')
|
|
324
|
-
.join('/')
|
|
325
|
-
.split('//')
|
|
326
|
-
.join('/');
|
|
327
|
-
let MiddlewareFunction = M;
|
|
328
|
-
let middlewareParams = {};
|
|
329
|
-
if (Array.isArray(M)) {
|
|
330
|
-
[MiddlewareFunction, middlewareParams] = M;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
middlewaresInfo.push({
|
|
334
|
-
name: MiddlewareFunction.name,
|
|
335
|
-
method,
|
|
336
|
-
path: realPath,
|
|
337
|
-
fullPath,
|
|
338
|
-
params: middlewareParams,
|
|
339
|
-
relatedQueryParameters: new MiddlewareFunction(
|
|
340
|
-
this.app,
|
|
341
|
-
middlewareParams,
|
|
342
|
-
)?.relatedQueryParameters,
|
|
343
|
-
authParams: new MiddlewareFunction(this.app, middlewareParams)
|
|
344
|
-
?.usedAuthParameters,
|
|
345
|
-
MiddlewareFunction,
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
return middlewaresInfo;
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
/**
|
|
353
|
-
* Object with routes. Routes relative to controller
|
|
354
|
-
* @example
|
|
355
|
-
* return {
|
|
356
|
-
* post: {
|
|
357
|
-
* "/someUrl": {
|
|
358
|
-
* handler: this.postSomeUrl,
|
|
359
|
-
* request: yup.object().shape({
|
|
360
|
-
* count: yup.number().max(100)required(),
|
|
361
|
-
* })
|
|
362
|
-
* }
|
|
363
|
-
* },
|
|
364
|
-
* };
|
|
365
|
-
*/
|
|
366
|
-
get routes() {
|
|
367
|
-
this.logger.warn('Please implement "routes" method on controller.');
|
|
368
|
-
return {};
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* Array of middlewares to append for route
|
|
373
|
-
* You should provide path relative to controller and then array of middlewares to apply.
|
|
374
|
-
* Order is matter.
|
|
375
|
-
* Be default path apply to ANY' method, but you can preattach 'METHOD' into patch to scope patch to this METHOD
|
|
376
|
-
* @returns {Map<string, Array<typeof import('../services/http/middleware/AbstractMiddleware.js').default | [Function, ...any]>>}
|
|
377
|
-
* @example
|
|
378
|
-
* return new Map([
|
|
379
|
-
* ['/{*splat}', [GetUserByToken]] // for any method for this controller
|
|
380
|
-
* ['POST/', [Auth]] // for POST method
|
|
381
|
-
* ['/superSecretMethod', [OnlySuperSecretUsers]] // route with ANY method
|
|
382
|
-
* ['PUT/superSecretMathod', [OnlySuperSecretAdmin]] // route with PUT method
|
|
383
|
-
* ]);
|
|
384
|
-
*/
|
|
385
|
-
static get middleware() {
|
|
386
|
-
return new Map([['/{*splat}', [GetUserByToken, Auth]]]);
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
/**
|
|
390
|
-
* Get constructor name that can include preix
|
|
391
|
-
*/
|
|
392
|
-
getConstructorName() {
|
|
393
|
-
if (this.prefix) {
|
|
394
|
-
return `${this.prefix.charAt(0).toUpperCase()}${this.prefix.slice(1)}/${
|
|
395
|
-
this.constructor.name
|
|
396
|
-
}`;
|
|
397
|
-
}
|
|
398
|
-
return this.constructor.name;
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
/**
|
|
402
|
-
* Get http path with inheritance of path
|
|
403
|
-
*/
|
|
404
|
-
getHttpPath() {
|
|
405
|
-
return `/${this.getConstructorName().toLowerCase()}`.replace('//', '/');
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
static get loggerGroup() {
|
|
409
|
-
return 'controller';
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
export default AbstractController;
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import type Base from './Base.js';
|
|
2
|
-
import { Model, Schema } from 'mongoose';
|
|
3
|
-
import type Server from '../server.js';
|
|
4
|
-
|
|
5
|
-
interface AbstractModel<T extends Document> extends Model, Base {
|
|
6
|
-
constructor(app: Server['app'], callback?: () => void);
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Return itself for internal methods.
|
|
10
|
-
*/
|
|
11
|
-
getSuper(): this;
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Model schema in Js object (not a mongoose schema).
|
|
15
|
-
*/
|
|
16
|
-
get modelSchema(): Object;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Mongoose schema.
|
|
20
|
-
*/
|
|
21
|
-
mongooseSchema: Schema<T>;
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Acces to mongoose model too
|
|
25
|
-
*/
|
|
26
|
-
mongooseModel: Model<T>;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Init custom hooks before model
|
|
30
|
-
*/
|
|
31
|
-
initHooks(): void;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
abstract class AbstractModel<T extends Document>
|
|
35
|
-
extends Model
|
|
36
|
-
implements AbstractModel
|
|
37
|
-
{
|
|
38
|
-
abstract get modelSchema(): Object;
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Return itself for internal methods.
|
|
42
|
-
*/
|
|
43
|
-
static abstract getSuper(): this;
|
|
44
|
-
|
|
45
|
-
mongooseSchema: Schema<T> = new Schema<T>(this.modelSchema);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export default AbstractModel;
|
package/modules/AbstractModel.js
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import mongoose from 'mongoose';
|
|
2
|
-
import Base from './Base.js';
|
|
3
|
-
|
|
4
|
-
class AbstractModel extends Base {
|
|
5
|
-
mongooseSchema = null;
|
|
6
|
-
|
|
7
|
-
mongooseModel = null;
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @param {import('../server.js').default['app']} app //TODO change to *.d.ts as this is a Server, not app
|
|
11
|
-
* @param function callback optional callback when connection ready
|
|
12
|
-
*/
|
|
13
|
-
constructor(app, callback = () => {}) {
|
|
14
|
-
super(app);
|
|
15
|
-
this.mongooseSchema = new mongoose.Schema(
|
|
16
|
-
this.modelSchema,
|
|
17
|
-
this.modelSchemaOptions,
|
|
18
|
-
);
|
|
19
|
-
mongoose.set('strictQuery', true);
|
|
20
|
-
this.mongooseSchema.set('timestamps', true);
|
|
21
|
-
this.mongooseSchema.set('minimize', false);
|
|
22
|
-
this.mongooseSchema.loadClass(this.constructor);
|
|
23
|
-
this.mongooseSchema.statics.getSuper = () => this;
|
|
24
|
-
this.mongooseSchema.methods.getSuper = () => this;
|
|
25
|
-
this.initHooks();
|
|
26
|
-
this.mongooseModel = mongoose.model(
|
|
27
|
-
this.constructor.name,
|
|
28
|
-
this.mongooseSchema,
|
|
29
|
-
);
|
|
30
|
-
if (!mongoose.connection.readyState) {
|
|
31
|
-
this.app.events.on('shutdown', async () => {
|
|
32
|
-
this.logger.verbose(
|
|
33
|
-
'Shutdown was called. Closing all mongoose connections',
|
|
34
|
-
);
|
|
35
|
-
for (const c of mongoose.connections) {
|
|
36
|
-
c.close(true);
|
|
37
|
-
}
|
|
38
|
-
// await mongoose.disconnect(); // TODO it have problems with replica-set
|
|
39
|
-
});
|
|
40
|
-
const connectionParams = {};
|
|
41
|
-
if (process.env.MONGO_APP_NAME) {
|
|
42
|
-
connectionParams.appName = process.env.MONGO_APP_NAME;
|
|
43
|
-
}
|
|
44
|
-
// do not connect on test
|
|
45
|
-
mongoose
|
|
46
|
-
.connect(this.app.getConfig('mongo').connectionString, connectionParams)
|
|
47
|
-
.then(
|
|
48
|
-
() => {
|
|
49
|
-
this.logger.info(
|
|
50
|
-
`Mongo connection success ${connectionParams.appName}`,
|
|
51
|
-
);
|
|
52
|
-
mongoose.connection.on('error', (err) => {
|
|
53
|
-
this.logger.error('Mongo connection error', err);
|
|
54
|
-
console.error(err);
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
callback();
|
|
58
|
-
},
|
|
59
|
-
(error) => {
|
|
60
|
-
this.logger.error("Can't install mongodb connection", error);
|
|
61
|
-
},
|
|
62
|
-
);
|
|
63
|
-
} else {
|
|
64
|
-
callback();
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Mongoose schema
|
|
70
|
-
*/
|
|
71
|
-
get modelSchema() {
|
|
72
|
-
this.logger.warn('You should provide modelSchema');
|
|
73
|
-
return {};
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Mongoose schema options
|
|
78
|
-
*/
|
|
79
|
-
// eslint-disable-next-line class-methods-use-this
|
|
80
|
-
get modelSchemaOptions() {
|
|
81
|
-
return {};
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
static get loggerGroup() {
|
|
85
|
-
return 'model';
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
initHooks() {
|
|
89
|
-
this.logger.verbose('Model have no hooks');
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
export default AbstractModel;
|
package/modules/Base.d.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import type winston from 'winston';
|
|
2
|
-
import type Server from '../server.js';
|
|
3
|
-
import type { Dirent } from 'node.fs';
|
|
4
|
-
|
|
5
|
-
declare class Base {
|
|
6
|
-
app: Server['app'];
|
|
7
|
-
#realLogger: null;
|
|
8
|
-
|
|
9
|
-
constructor(app: Server['app']);
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* In case of logging sometimes we might need to replace name
|
|
13
|
-
*/
|
|
14
|
-
getConstructorName(): string;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Optimzation to lazy load logger. It will be inited only on request
|
|
18
|
-
*/
|
|
19
|
-
get logger(): winston.Logger;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Get winston loger for given label
|
|
23
|
-
* @param label name of logger
|
|
24
|
-
*/
|
|
25
|
-
getLogger(label: string): winston.Logger;
|
|
26
|
-
|
|
27
|
-
getFilesPathWithInheritance(
|
|
28
|
-
internalFolder: string,
|
|
29
|
-
externalFolder: string,
|
|
30
|
-
): Promise<{ path: string; file: string }[]>;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Return logger group. Just to have all logs groupped logically
|
|
34
|
-
*/
|
|
35
|
-
static get loggerGroup(): string;
|
|
36
|
-
}
|
|
37
|
-
export default Base;
|
package/modules/Base.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { getFilesPathWithInheritance } from '../helpers/files.js';
|
|
2
|
-
|
|
3
|
-
class Base {
|
|
4
|
-
#realLogger = null;
|
|
5
|
-
|
|
6
|
-
constructor(app) {
|
|
7
|
-
this.app = app;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* In case of logging sometimes we might need to replace name
|
|
12
|
-
*/
|
|
13
|
-
getConstructorName() {
|
|
14
|
-
return this.constructor.name;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Optimzation to lazy load logger. It will be inited only on request
|
|
19
|
-
*/
|
|
20
|
-
get logger() {
|
|
21
|
-
let l;
|
|
22
|
-
try {
|
|
23
|
-
l = this.#realLogger;
|
|
24
|
-
} catch (e) {
|
|
25
|
-
console.warn(
|
|
26
|
-
`You try to accees logger not from class. that can be ok in case of models.`,
|
|
27
|
-
);
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (!l) {
|
|
32
|
-
this.#realLogger = this.getLogger(
|
|
33
|
-
this.constructor.loggerGroup + this.getConstructorName(),
|
|
34
|
-
);
|
|
35
|
-
}
|
|
36
|
-
return this.#realLogger;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Get winston loger for given label
|
|
41
|
-
* @param {string} label name of logger
|
|
42
|
-
*/
|
|
43
|
-
getLogger(label) {
|
|
44
|
-
return this.app.logger.child({ label });
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async getFilesPathWithInheritance(internalFolder, externalFolder) {
|
|
48
|
-
return getFilesPathWithInheritance({
|
|
49
|
-
internalFolder,
|
|
50
|
-
externalFolder,
|
|
51
|
-
logger: (text) => this.logger.verbose(text),
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Return logger group. Just to have all logs groupped logically
|
|
57
|
-
*/
|
|
58
|
-
static get loggerGroup() {
|
|
59
|
-
return 'Base_please_overwrite_';
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export default Base;
|