@adaptivestone/framework 5.0.0-alpha.2 → 5.0.0-alpha.21
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 +96 -0
- package/commands/CreateUser.js +3 -1
- package/commands/GenerateRandomBytes.js +15 -0
- package/commands/migration/Migrate.js +1 -1
- package/config/auth.js +5 -1
- package/config/ipDetector.js +14 -0
- package/controllers/Auth.js +2 -2
- package/controllers/Home.js +1 -1
- package/folderConfig.js +0 -1
- package/helpers/files.js +8 -8
- package/jsconfig.json +9 -0
- package/models/User.js +7 -1
- package/modules/AbstractCommand.js +2 -1
- package/modules/AbstractController.js +22 -18
- package/modules/AbstractModel.d.ts +48 -0
- package/modules/AbstractModel.js +20 -2
- package/modules/Base.d.ts +5 -4
- package/modules/Base.js +11 -1
- package/package.json +13 -16
- package/server.d.ts +7 -5
- package/server.js +11 -7
- package/services/cache/Cache.d.ts +2 -2
- package/services/cache/Cache.js +10 -6
- package/services/http/HttpServer.js +12 -20
- package/services/http/middleware/GetUserByToken.js +3 -2
- package/services/http/middleware/I18n.js +20 -21
- package/services/http/middleware/IpDetector.js +59 -0
- package/services/http/middleware/Pagination.js +3 -2
- package/services/http/middleware/RateLimiter.js +8 -2
- package/services/http/middleware/RequestLogger.js +3 -3
- package/services/http/middleware/RequestParser.js +5 -2
- package/services/messaging/email/index.js +7 -15
- package/services/validate/ValidateService.js +1 -1
- package/tests/setup.js +3 -1
- package/tests/setupVitest.js +1 -1
- package/types/TFoldersConfig.d.ts +0 -2
- package/.eslintrc.cjs +0 -41
- package/controllers/Auth.test.js +0 -451
- package/controllers/Home.test.js +0 -12
- package/models/Migration.test.js +0 -20
- package/models/Sequence.test.js +0 -43
- package/models/User.test.js +0 -143
- package/modules/Modules.test.js +0 -18
- package/services/cache/Cache.test.js +0 -81
- package/services/http/middleware/Auth.test.js +0 -57
- package/services/http/middleware/Cors.test.js +0 -147
- package/services/http/middleware/GetUserByToken.test.js +0 -108
- package/services/http/middleware/I18n.test.js +0 -96
- package/services/http/middleware/PrepareAppInfo.test.js +0 -26
- package/services/http/middleware/RateLimiter.test.js +0 -233
- package/services/http/middleware/RequestParser.test.js +0 -112
- package/services/http/middleware/Role.test.js +0 -93
- package/services/http/middleware/StaticFiles.js +0 -59
- package/services/validate/ValidateService.test.js +0 -107
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,99 @@
|
|
|
1
|
+
### 5.0.0-alpha.21
|
|
2
|
+
|
|
3
|
+
[BREAKING] possible breaking. Framework start using express 5 instead of express 4. Please follow express migration guide too https://expressjs.com/en/guide/migrating-5.html.
|
|
4
|
+
[BREAKING] AS part of express 5 migration _ in rotes (middlewares) should have perameter. please replace _ to \*splat
|
|
5
|
+
[UPDATE] update deps
|
|
6
|
+
[UPDATE] Mailer uses await import() for startup speedup
|
|
7
|
+
|
|
8
|
+
### 5.0.0-alpha.20
|
|
9
|
+
|
|
10
|
+
[UPDATE] update deps
|
|
11
|
+
[UPDATE] #realLogger do not throw error in a scecific cases (model toJSON({virtual:true}))
|
|
12
|
+
|
|
13
|
+
### 5.0.0-alpha.19
|
|
14
|
+
|
|
15
|
+
[NEW] added modelSchemaOptions for models
|
|
16
|
+
|
|
17
|
+
### 5.0.0-alpha.18
|
|
18
|
+
|
|
19
|
+
[BREAKING] default auth responce changed to be unified. {token, user} = > {data:{token, user}}
|
|
20
|
+
[UPDATE] RateLimiter updae key generation
|
|
21
|
+
|
|
22
|
+
### 5.0.0-alpha.17
|
|
23
|
+
|
|
24
|
+
[NEW] generateRandomBytes command
|
|
25
|
+
[UPDATE] update deps
|
|
26
|
+
|
|
27
|
+
### 5.0.0-alpha.16
|
|
28
|
+
|
|
29
|
+
[UPDATE] no warning of direct usage body and query
|
|
30
|
+
[UPDATE] update deps
|
|
31
|
+
|
|
32
|
+
### 5.0.0-alpha.15
|
|
33
|
+
|
|
34
|
+
[BUG] fix bug with pagination
|
|
35
|
+
[UPDATE] update deps
|
|
36
|
+
|
|
37
|
+
### 5.0.0-alpha.14
|
|
38
|
+
|
|
39
|
+
[NEW] add types for Abstract model (wip)
|
|
40
|
+
|
|
41
|
+
### 5.0.0-alpha.13
|
|
42
|
+
|
|
43
|
+
[UPDATE] update deps
|
|
44
|
+
[UPDATE] update i18n internal implementation
|
|
45
|
+
[CHANGE] disable https server view
|
|
46
|
+
|
|
47
|
+
### 5.0.0-alpha.12
|
|
48
|
+
|
|
49
|
+
[UPDATE] update deps
|
|
50
|
+
|
|
51
|
+
### 5.0.0-alpha.11
|
|
52
|
+
|
|
53
|
+
[UPDATE] update deps
|
|
54
|
+
|
|
55
|
+
### 5.0.0-alpha.10
|
|
56
|
+
|
|
57
|
+
[UPDATE] update deps
|
|
58
|
+
[NEW] IpDetector middleware that support detecting proxy and X-Forwarded-For header
|
|
59
|
+
[BREAKING] RateLimiter now need to have IpDetector middleware before
|
|
60
|
+
|
|
61
|
+
### 5.0.0-alpha.9
|
|
62
|
+
|
|
63
|
+
[UPDATE] update deps
|
|
64
|
+
[BREAKING] removing staticFiles middleware as it not used in projects anymore. Docs with nginx config will be provided
|
|
65
|
+
[BREAKING] remove default AUTH_SALT. It should be provided on a app level now
|
|
66
|
+
[BREAKING] Vitest 2.0.0 https://vitest.dev/guide/migration.html#migrating-to-vitest-2-0
|
|
67
|
+
|
|
68
|
+
### 5.0.0-alpha.8
|
|
69
|
+
|
|
70
|
+
[UPDATE] replace dotenv with loadEnvFile
|
|
71
|
+
[UPDATE] replace nodemon with node --watch (dev only)
|
|
72
|
+
[BREAKING] Minimum node version is 20.12 as for now (process.loadEnvFile)
|
|
73
|
+
|
|
74
|
+
### 5.0.0-alpha.7
|
|
75
|
+
|
|
76
|
+
[UPDATE] deps update
|
|
77
|
+
|
|
78
|
+
### 5.0.0-alpha.6
|
|
79
|
+
|
|
80
|
+
[UPDATE] Update internal documentation (jsdoc, d.ts)
|
|
81
|
+
|
|
82
|
+
### 5.0.0-alpha.5
|
|
83
|
+
|
|
84
|
+
[UPDATE] More verbose errors for rapsing body request.
|
|
85
|
+
[UPDATE] deps update
|
|
86
|
+
|
|
87
|
+
### 5.0.0-alpha.4
|
|
88
|
+
|
|
89
|
+
[UPDATE] Update rate-limiter-flexible to v5
|
|
90
|
+
[CHANGE] Cache update redis.setEX to redis.set(..,..,{EX:xx}) as setEX deprecated
|
|
91
|
+
|
|
92
|
+
### 5.0.0-alpha.3
|
|
93
|
+
|
|
94
|
+
[UPDATE] deps update
|
|
95
|
+
[FIX] Migration commands apply
|
|
96
|
+
|
|
1
97
|
### 5.0.0-alpha.2
|
|
2
98
|
|
|
3
99
|
[UPDATE] deps update
|
package/commands/CreateUser.js
CHANGED
|
@@ -60,7 +60,9 @@ class CreateUser extends AbstractCommand {
|
|
|
60
60
|
|
|
61
61
|
await user.generateToken();
|
|
62
62
|
|
|
63
|
-
this.logger.info(
|
|
63
|
+
this.logger.info(
|
|
64
|
+
`User was created/updated ${JSON.stringify(user, null, 4)}`,
|
|
65
|
+
);
|
|
64
66
|
|
|
65
67
|
return user;
|
|
66
68
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import AbstractCommand from '../modules/AbstractCommand.js';
|
|
3
|
+
|
|
4
|
+
class GenerateRandomBytes extends AbstractCommand {
|
|
5
|
+
async run() {
|
|
6
|
+
const sizes = [16, 32, 64, 128, 256];
|
|
7
|
+
for (const size of sizes) {
|
|
8
|
+
const bytes = randomBytes(size).toString('hex');
|
|
9
|
+
this.logger.info(`${size} bytes: ${bytes}`);
|
|
10
|
+
}
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default GenerateRandomBytes;
|
|
@@ -36,7 +36,7 @@ class Migrate extends AbstractCommand {
|
|
|
36
36
|
for (const migration of migrations) {
|
|
37
37
|
this.logger.info(`=== Start migration ${migration.file} ===`);
|
|
38
38
|
// eslint-disable-next-line no-await-in-loop
|
|
39
|
-
const MigrationCommand = await import(migration.path);
|
|
39
|
+
const { default: MigrationCommand } = await import(migration.path);
|
|
40
40
|
const migrationCommand = new MigrationCommand(this.app);
|
|
41
41
|
// eslint-disable-next-line no-await-in-loop
|
|
42
42
|
await migrationCommand.up();
|
package/config/auth.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
export default {
|
|
2
2
|
hashRounds: 64,
|
|
3
|
-
saltSecret:
|
|
3
|
+
saltSecret:
|
|
4
|
+
process.env.AUTH_SALT ||
|
|
5
|
+
console.error(
|
|
6
|
+
'AUTH_SALT is not defined. You can "npm run cli generateRandomBytes" and use it',
|
|
7
|
+
),
|
|
4
8
|
isAuthWithVefificationFlow: true,
|
|
5
9
|
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
headers: ['X-Forwarded-For'],
|
|
3
|
+
trustedProxy: [
|
|
4
|
+
// list of trusted proxies.
|
|
5
|
+
'169.254.0.0/16', // linklocal
|
|
6
|
+
'fe80::/10', // linklocal
|
|
7
|
+
'127.0.0.1/8', // loopback
|
|
8
|
+
'::1/128', // loopback
|
|
9
|
+
'10.0.0.0/8', // uniquelocal
|
|
10
|
+
'172.16.0.0/12', // uniquelocal
|
|
11
|
+
'192.168.0.0/16', // uniquelocal
|
|
12
|
+
'fc00::/7', // uniquelocal
|
|
13
|
+
],
|
|
14
|
+
};
|
package/controllers/Auth.js
CHANGED
|
@@ -83,7 +83,7 @@ class Auth extends AbstractController {
|
|
|
83
83
|
}
|
|
84
84
|
const token = await user.generateToken();
|
|
85
85
|
|
|
86
|
-
return res.status(200).json({ token, user: user.getPublic() });
|
|
86
|
+
return res.status(200).json({ data: { token, user: user.getPublic() } });
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
async postRegister(req, res) {
|
|
@@ -203,7 +203,7 @@ class Auth extends AbstractController {
|
|
|
203
203
|
}
|
|
204
204
|
|
|
205
205
|
static get middleware() {
|
|
206
|
-
return new Map([['/*', [GetUserByToken, RateLimiter]]]);
|
|
206
|
+
return new Map([['/*splat', [GetUserByToken, RateLimiter]]]);
|
|
207
207
|
}
|
|
208
208
|
}
|
|
209
209
|
|
package/controllers/Home.js
CHANGED
package/folderConfig.js
CHANGED
|
@@ -6,7 +6,6 @@ export default {
|
|
|
6
6
|
models: path.resolve('./models'),
|
|
7
7
|
controllers: path.resolve('./controllers'),
|
|
8
8
|
views: path.resolve('./views'),
|
|
9
|
-
public: path.resolve('./public'),
|
|
10
9
|
locales: path.resolve('./locales'),
|
|
11
10
|
emails: path.resolve('./services/messaging/email/templates'),
|
|
12
11
|
commands: path.resolve('./commands'),
|
package/helpers/files.js
CHANGED
|
@@ -8,7 +8,7 @@ const getFilesPathWithInheritance = async ({
|
|
|
8
8
|
loggerFileType = '',
|
|
9
9
|
filter: { startWithCapital = true, notTests = true, notHidden = true } = {},
|
|
10
10
|
}) => {
|
|
11
|
-
|
|
11
|
+
const [internalFiles, externalFiles] = await Promise.all([
|
|
12
12
|
fs.readdir(internalFolder, { recursive: true, withFileTypes: true }),
|
|
13
13
|
fs.readdir(externalFolder, { recursive: true, withFileTypes: true }),
|
|
14
14
|
]);
|
|
@@ -33,24 +33,24 @@ const getFilesPathWithInheritance = async ({
|
|
|
33
33
|
return true;
|
|
34
34
|
};
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
const internalFilesString = internalFiles
|
|
37
37
|
.filter(filterIndexFile)
|
|
38
38
|
.map((fileDirent) =>
|
|
39
|
-
join(fileDirent.
|
|
39
|
+
join(fileDirent.parentPath, fileDirent.name)
|
|
40
40
|
.replace(`${internalFolder}/`, '')
|
|
41
41
|
.replace(`${internalFolder}`, ''),
|
|
42
42
|
);
|
|
43
|
-
|
|
43
|
+
const externalFilesString = externalFiles
|
|
44
44
|
.filter(filterIndexFile)
|
|
45
45
|
.map((fileDirent) =>
|
|
46
|
-
join(fileDirent.
|
|
46
|
+
join(fileDirent.parentPath, fileDirent.name)
|
|
47
47
|
.replace(`${externalFolder}/`, '')
|
|
48
48
|
.replace(`${externalFolder}`, ''),
|
|
49
49
|
);
|
|
50
50
|
|
|
51
51
|
const filesToLoad = [];
|
|
52
|
-
for (const file of
|
|
53
|
-
if (
|
|
52
|
+
for (const file of internalFilesString) {
|
|
53
|
+
if (externalFilesString.includes(file)) {
|
|
54
54
|
logger(
|
|
55
55
|
`Skipping register INTERNAL file '${file}' ${
|
|
56
56
|
loggerFileType ? `of type ${loggerFileType}` : ''
|
|
@@ -64,7 +64,7 @@ const getFilesPathWithInheritance = async ({
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
for (const file of
|
|
67
|
+
for (const file of externalFilesString) {
|
|
68
68
|
filesToLoad.push({
|
|
69
69
|
path: join(externalFolder, file),
|
|
70
70
|
file,
|
package/jsconfig.json
ADDED
package/models/User.js
CHANGED
|
@@ -4,7 +4,6 @@ import { scrypt } from 'node:crypto';
|
|
|
4
4
|
|
|
5
5
|
import { promisify } from 'node:util';
|
|
6
6
|
import AbstractModel from '../modules/AbstractModel.js';
|
|
7
|
-
import Mailer from '../services/messaging/email/index.js';
|
|
8
7
|
|
|
9
8
|
class User extends AbstractModel {
|
|
10
9
|
constructor(app) {
|
|
@@ -175,6 +174,10 @@ class User extends AbstractModel {
|
|
|
175
174
|
async sendPasswordRecoveryEmail(i18n) {
|
|
176
175
|
const passwordRecoveryToken =
|
|
177
176
|
await User.generateUserPasswordRecoveryToken(this);
|
|
177
|
+
// speed optimisation
|
|
178
|
+
const Mailer = (await import('../services/messaging/email/index.js'))
|
|
179
|
+
.default;
|
|
180
|
+
|
|
178
181
|
const mail = new Mailer(
|
|
179
182
|
this.getSuper().app,
|
|
180
183
|
'recovery',
|
|
@@ -241,6 +244,9 @@ class User extends AbstractModel {
|
|
|
241
244
|
|
|
242
245
|
async sendVerificationEmail(i18n) {
|
|
243
246
|
const verificationToken = await User.generateUserVerificationToken(this);
|
|
247
|
+
// speed optimisation
|
|
248
|
+
const Mailer = (await import('../services/messaging/email/index.js'))
|
|
249
|
+
.default;
|
|
244
250
|
const mail = new Mailer(
|
|
245
251
|
this.getSuper().app,
|
|
246
252
|
'verification',
|
|
@@ -13,10 +13,11 @@ class AbstractCommand extends Base {
|
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Entry point to every command. This method should be overridden
|
|
16
|
-
* @
|
|
16
|
+
* @return {Promise<boolean>} resut
|
|
17
17
|
*/
|
|
18
18
|
async run() {
|
|
19
19
|
this.logger.error('You should implement run method');
|
|
20
|
+
return false;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
static get loggerGroup() {
|
|
@@ -25,10 +25,12 @@ class AbstractController extends Base {
|
|
|
25
25
|
const { routes } = this;
|
|
26
26
|
let httpPath = this.getHttpPath();
|
|
27
27
|
|
|
28
|
+
// @ts-ignore
|
|
28
29
|
if (this.getExpressPath) {
|
|
29
30
|
this.logger.warn(
|
|
30
31
|
`getExpressPath deprecated. Please use getHttpPath instead. Will be removed on v5`,
|
|
31
32
|
);
|
|
33
|
+
// @ts-ignore
|
|
32
34
|
httpPath = this.getExpressPath();
|
|
33
35
|
}
|
|
34
36
|
|
|
@@ -58,6 +60,7 @@ class AbstractController extends Base {
|
|
|
58
60
|
httpPath,
|
|
59
61
|
);
|
|
60
62
|
const middlewaresInfo = this.parseMiddlewares(
|
|
63
|
+
// @ts-ignore
|
|
61
64
|
this.constructor.middleware,
|
|
62
65
|
httpPath,
|
|
63
66
|
);
|
|
@@ -193,22 +196,22 @@ class AbstractController extends Base {
|
|
|
193
196
|
errors: err.message,
|
|
194
197
|
});
|
|
195
198
|
}
|
|
196
|
-
req.body = new Proxy(req.body, {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
});
|
|
204
|
-
req.query = new Proxy(req.query, {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
});
|
|
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
|
+
// });
|
|
212
215
|
|
|
213
216
|
if (!routeObject.handler) {
|
|
214
217
|
this.logger.error(`Route object have no handler defined`);
|
|
@@ -370,16 +373,17 @@ class AbstractController extends Base {
|
|
|
370
373
|
* You should provide path relative to controller and then array of middlewares to apply.
|
|
371
374
|
* Order is matter.
|
|
372
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]>>}
|
|
373
377
|
* @example
|
|
374
378
|
* return new Map([
|
|
375
|
-
* ['/*', [GetUserByToken]] // for any method for this controller
|
|
379
|
+
* ['/*splat', [GetUserByToken]] // for any method for this controller
|
|
376
380
|
* ['POST/', [Auth]] // for POST method
|
|
377
381
|
* ['/superSecretMethod', [OnlySuperSecretUsers]] // route with ANY method
|
|
378
382
|
* ['PUT/superSecretMathod', [OnlySuperSecretAdmin]] // route with PUT method
|
|
379
383
|
* ]);
|
|
380
384
|
*/
|
|
381
385
|
static get middleware() {
|
|
382
|
-
return new Map([['/*', [GetUserByToken, Auth]]]);
|
|
386
|
+
return new Map([['/*splat', [GetUserByToken, Auth]]]);
|
|
383
387
|
}
|
|
384
388
|
|
|
385
389
|
/**
|
|
@@ -0,0 +1,48 @@
|
|
|
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
CHANGED
|
@@ -2,13 +2,20 @@ import mongoose from 'mongoose';
|
|
|
2
2
|
import Base from './Base.js';
|
|
3
3
|
|
|
4
4
|
class AbstractModel extends Base {
|
|
5
|
+
mongooseSchema = null;
|
|
6
|
+
|
|
7
|
+
mongooseModel = null;
|
|
8
|
+
|
|
5
9
|
/**
|
|
6
|
-
* @param {import('../server')} app //TODO change to *.d.ts as this is a Server, not app
|
|
10
|
+
* @param {import('../server.js').default['app']} app //TODO change to *.d.ts as this is a Server, not app
|
|
7
11
|
* @param function callback optional callback when connection ready
|
|
8
12
|
*/
|
|
9
13
|
constructor(app, callback = () => {}) {
|
|
10
14
|
super(app);
|
|
11
|
-
this.mongooseSchema = mongoose.Schema(
|
|
15
|
+
this.mongooseSchema = new mongoose.Schema(
|
|
16
|
+
this.modelSchema,
|
|
17
|
+
this.modelSchemaOptions,
|
|
18
|
+
);
|
|
12
19
|
mongoose.set('strictQuery', true);
|
|
13
20
|
this.mongooseSchema.set('timestamps', true);
|
|
14
21
|
this.mongooseSchema.set('minimize', false);
|
|
@@ -22,6 +29,9 @@ class AbstractModel extends Base {
|
|
|
22
29
|
);
|
|
23
30
|
if (!mongoose.connection.readyState) {
|
|
24
31
|
this.app.events.on('shutdown', async () => {
|
|
32
|
+
this.logger.verbose(
|
|
33
|
+
'Shutdown was called. Closing all mongoose connections',
|
|
34
|
+
);
|
|
25
35
|
for (const c of mongoose.connections) {
|
|
26
36
|
c.close(true);
|
|
27
37
|
}
|
|
@@ -51,6 +61,14 @@ class AbstractModel extends Base {
|
|
|
51
61
|
return {};
|
|
52
62
|
}
|
|
53
63
|
|
|
64
|
+
/**
|
|
65
|
+
* Mongoose schema options
|
|
66
|
+
*/
|
|
67
|
+
// eslint-disable-next-line class-methods-use-this
|
|
68
|
+
get modelSchemaOptions() {
|
|
69
|
+
return {};
|
|
70
|
+
}
|
|
71
|
+
|
|
54
72
|
static get loggerGroup() {
|
|
55
73
|
return 'model';
|
|
56
74
|
}
|
package/modules/Base.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import winston from 'winston';
|
|
2
|
-
import Server from '../server';
|
|
1
|
+
import type winston from 'winston';
|
|
2
|
+
import type Server from '../server.js';
|
|
3
|
+
import type { Dirent } from 'node.fs';
|
|
3
4
|
|
|
4
5
|
declare class Base {
|
|
5
6
|
app: Server['app'];
|
|
@@ -26,11 +27,11 @@ declare class Base {
|
|
|
26
27
|
getFilesPathWithInheritance(
|
|
27
28
|
internalFolder: string,
|
|
28
29
|
externalFolder: string,
|
|
29
|
-
): Promise<string[]>;
|
|
30
|
+
): Promise<{ path: string; file: string }[]>;
|
|
30
31
|
|
|
31
32
|
/**
|
|
32
33
|
* Return logger group. Just to have all logs groupped logically
|
|
33
34
|
*/
|
|
34
35
|
static get loggerGroup(): string;
|
|
35
36
|
}
|
|
36
|
-
export
|
|
37
|
+
export default Base;
|
package/modules/Base.js
CHANGED
|
@@ -18,7 +18,17 @@ class Base {
|
|
|
18
18
|
* Optimzation to lazy load logger. It will be inited only on request
|
|
19
19
|
*/
|
|
20
20
|
get logger() {
|
|
21
|
-
|
|
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) {
|
|
22
32
|
this.#realLogger = this.getLogger(
|
|
23
33
|
this.constructor.loggerGroup + this.getConstructorName(),
|
|
24
34
|
);
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adaptivestone/framework",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.21",
|
|
4
4
|
"description": "Adaptive stone node js framework",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"engines": {
|
|
8
|
-
"node": ">=
|
|
8
|
+
"node": ">=20.12.0"
|
|
9
9
|
},
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
@@ -13,9 +13,10 @@
|
|
|
13
13
|
},
|
|
14
14
|
"homepage": "https://framework.adaptivestone.com/",
|
|
15
15
|
"scripts": {
|
|
16
|
-
"dev": "
|
|
17
|
-
"prod": "
|
|
16
|
+
"dev": "node --watch ./index.js",
|
|
17
|
+
"prod": "node --watch ./cluster.js",
|
|
18
18
|
"test": "vitest run",
|
|
19
|
+
"t": "vitest --coverage=false --reporter=default",
|
|
19
20
|
"prettier": "prettier --check '**/*.(js|jsx|ts|tsx|json|css|scss|md)'",
|
|
20
21
|
"lint": "eslint '**/*.js'",
|
|
21
22
|
"lint:fix": "eslint '**/*.js' --fix",
|
|
@@ -30,40 +31,36 @@
|
|
|
30
31
|
"license": "MIT",
|
|
31
32
|
"dependencies": {
|
|
32
33
|
"deepmerge": "^4.2.2",
|
|
33
|
-
"
|
|
34
|
-
"express": "^4.17.1",
|
|
34
|
+
"express": "^5.0.1",
|
|
35
35
|
"formidable": "^3.5.1",
|
|
36
36
|
"html-to-text": "^9.0.3",
|
|
37
37
|
"i18next": "^23.2.8",
|
|
38
|
-
"i18next-chained-backend": "^4.0.0",
|
|
39
38
|
"i18next-fs-backend": "^2.0.0",
|
|
40
|
-
"juice": "^
|
|
41
|
-
"mime": "^4.0.0",
|
|
39
|
+
"juice": "^11.0.0",
|
|
42
40
|
"minimist": "^1.2.5",
|
|
43
41
|
"mongoose": "^8.0.0",
|
|
44
42
|
"nodemailer": "^6.6.3",
|
|
45
43
|
"nodemailer-sendmail-transport": "^1.0.2",
|
|
46
44
|
"nodemailer-stub-transport": "^1.1.0",
|
|
47
45
|
"pug": "^3.0.2",
|
|
48
|
-
"rate-limiter-flexible": "^
|
|
46
|
+
"rate-limiter-flexible": "^5.0.0",
|
|
49
47
|
"redis": "^4.3.1",
|
|
50
48
|
"winston": "^3.3.3",
|
|
51
|
-
"winston-transport-sentry-node": "^
|
|
49
|
+
"winston-transport-sentry-node": "^3.0.0",
|
|
52
50
|
"yup": "^1.0.0"
|
|
53
51
|
},
|
|
54
52
|
"devDependencies": {
|
|
55
|
-
"@vitest/coverage-v8": "^
|
|
53
|
+
"@vitest/coverage-v8": "^2.0.0",
|
|
56
54
|
"eslint": "^8.0.0",
|
|
57
55
|
"eslint-config-airbnb-base": "^15.0.0",
|
|
58
56
|
"eslint-config-prettier": "^9.0.0",
|
|
59
57
|
"eslint-plugin-prettier": "^5.0.0",
|
|
60
|
-
"eslint-plugin-vitest": "^0.
|
|
58
|
+
"eslint-plugin-vitest": "^0.4.0",
|
|
61
59
|
"husky": "^9.0.0",
|
|
62
60
|
"lint-staged": "^15.0.0",
|
|
63
|
-
"mongodb-memory-server": "^
|
|
64
|
-
"nodemon": "^3.0.1",
|
|
61
|
+
"mongodb-memory-server": "^10.0.0",
|
|
65
62
|
"prettier": "^3.0.0",
|
|
66
|
-
"vitest": "^
|
|
63
|
+
"vitest": "^2.0.0"
|
|
67
64
|
},
|
|
68
65
|
"lint-staged": {
|
|
69
66
|
"**/*.{js,jsx,ts,tsx,json,css,scss,md}": [
|
package/server.d.ts
CHANGED
|
@@ -9,6 +9,8 @@ import BaseCli from './modules/BaseCli';
|
|
|
9
9
|
import Cache from './services/cache/Cache';
|
|
10
10
|
import winston from 'winston';
|
|
11
11
|
|
|
12
|
+
import HttpServer from './services/http/HttpServer.js';
|
|
13
|
+
|
|
12
14
|
type ServerConfig = {
|
|
13
15
|
folders: ExpandDeep<TFolderConfig>;
|
|
14
16
|
};
|
|
@@ -24,7 +26,7 @@ declare class Server {
|
|
|
24
26
|
events: EventEmitter;
|
|
25
27
|
get cache(): Server['cacheService'];
|
|
26
28
|
get logger(): winston.Logger;
|
|
27
|
-
httpServer: null;
|
|
29
|
+
httpServer: HttpServer | null;
|
|
28
30
|
controllerManager: null;
|
|
29
31
|
};
|
|
30
32
|
cacheService: Cache;
|
|
@@ -33,7 +35,7 @@ declare class Server {
|
|
|
33
35
|
configs: Map<string, {}>;
|
|
34
36
|
models: Map<string, MongooseModel<any>>;
|
|
35
37
|
};
|
|
36
|
-
cli:
|
|
38
|
+
cli: null | BaseCli;
|
|
37
39
|
|
|
38
40
|
/**
|
|
39
41
|
* Construct new server
|
|
@@ -68,7 +70,7 @@ declare class Server {
|
|
|
68
70
|
* @see updateConfig
|
|
69
71
|
* @TODO generate that based on real data
|
|
70
72
|
*/
|
|
71
|
-
getConfig(configName: string): {};
|
|
73
|
+
getConfig(configName: string): { [key: string]: any };
|
|
72
74
|
|
|
73
75
|
/**
|
|
74
76
|
* Return or create new logger instance. This is a main logger instance
|
|
@@ -79,7 +81,7 @@ declare class Server {
|
|
|
79
81
|
* Primary designed for tests when we need to update some configs before start testing
|
|
80
82
|
* Should be called before any initialization was done
|
|
81
83
|
*/
|
|
82
|
-
updateConfig(configName: string, config: {}): {};
|
|
84
|
+
updateConfig(configName: string, config: {}): { [key: string]: any };
|
|
83
85
|
|
|
84
86
|
/**
|
|
85
87
|
* Return model from {modelName} (file name) on model folder.
|
|
@@ -93,4 +95,4 @@ declare class Server {
|
|
|
93
95
|
runCliCommand(commandName: string, args: {}): Promise<BaseCli['run']>;
|
|
94
96
|
}
|
|
95
97
|
|
|
96
|
-
export
|
|
98
|
+
export default Server;
|