@adaptivestone/framework 5.0.0-alpha.12 → 5.0.0-alpha.14

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,13 @@
1
+ ### 5.0.0-alpha.14
2
+
3
+ [NEW] add types for Abstract model (wip)
4
+
5
+ ### 5.0.0-alpha.13
6
+
7
+ [UPDATE] update deps
8
+ [UPDATE] update i18n internal implementation
9
+ [CHANGE] disable https server view
10
+
1
11
  ### 5.0.0-alpha.12
2
12
 
3
13
  [UPDATE] update deps
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
- let [internalFiles, externalFiles] = await Promise.all([
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
- internalFiles = internalFiles
36
+ const internalFilesString = internalFiles
37
37
  .filter(filterIndexFile)
38
38
  .map((fileDirent) =>
39
- join(fileDirent.path, fileDirent.name)
39
+ join(fileDirent.parentPath, fileDirent.name)
40
40
  .replace(`${internalFolder}/`, '')
41
41
  .replace(`${internalFolder}`, ''),
42
42
  );
43
- externalFiles = externalFiles
43
+ const externalFilesString = externalFiles
44
44
  .filter(filterIndexFile)
45
45
  .map((fileDirent) =>
46
- join(fileDirent.path, fileDirent.name)
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 internalFiles) {
53
- if (externalFiles.includes(file)) {
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 externalFiles) {
67
+ for (const file of externalFilesString) {
68
68
  filesToLoad.push({
69
69
  path: join(externalFolder, file),
70
70
  file,
@@ -373,7 +373,7 @@ class AbstractController extends Base {
373
373
  * You should provide path relative to controller and then array of middlewares to apply.
374
374
  * Order is matter.
375
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<AbstractMiddleware | [Function, ...any]>>}
376
+ * @returns {Map<string, Array<typeof import('../services/http/middleware/AbstractMiddleware.js').default | [Function, ...any]>>}
377
377
  * @example
378
378
  * return new Map([
379
379
  * ['/*', [GetUserByToken]] // for any method for this controller
@@ -0,0 +1,48 @@
1
+ import Base from './Base.js';
2
+ import { Model, Schema } from 'mongoose';
3
+ import 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;
@@ -2,6 +2,10 @@ 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
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
package/modules/Base.d.ts CHANGED
@@ -27,7 +27,7 @@ declare class Base {
27
27
  getFilesPathWithInheritance(
28
28
  internalFolder: string,
29
29
  externalFolder: string,
30
- ): Promise<Dirent[]>;
30
+ ): Promise<{ path: string; file: string }[]>;
31
31
 
32
32
  /**
33
33
  * Return logger group. Just to have all logs groupped logically
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adaptivestone/framework",
3
- "version": "5.0.0-alpha.12",
3
+ "version": "5.0.0-alpha.14",
4
4
  "description": "Adaptive stone node js framework",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -35,10 +35,8 @@
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
39
  "juice": "^11.0.0",
41
- "mime": "^4.0.0",
42
40
  "minimist": "^1.2.5",
43
41
  "mongoose": "^8.0.0",
44
42
  "nodemailer": "^6.6.3",
package/server.js CHANGED
@@ -66,7 +66,7 @@ class Server {
66
66
 
67
67
  /**
68
68
  * Start server (http + init all http ralated functions)
69
- * @param <Promise>callbackBefore404 code that should be executed before adding page 404
69
+ * @param {Function} callbackBefore404 code that should be executed before adding page 404
70
70
  * @returns {Promise}
71
71
  */
72
72
  async startServer(callbackBefore404 = async () => Promise.resolve()) {
@@ -43,8 +43,10 @@ class Cache extends Base {
43
43
  }
44
44
  const key = this.getKeyWithNameSpace(keyValue);
45
45
  // 5 mins default
46
- let resolve = null;
47
- let reject = null;
46
+ // eslint-disable-next-line no-unused-vars
47
+ let resolve = (value) => {};
48
+ // eslint-disable-next-line no-unused-vars
49
+ let reject = (value) => {};
48
50
  if (this.promiseMapping.has(key)) {
49
51
  return this.promiseMapping.get(key);
50
52
  }
@@ -71,7 +73,7 @@ class Cache extends Base {
71
73
 
72
74
  this.redisClient.set(
73
75
  key,
74
- JSON.stringify(result, (jsonkey, value) =>
76
+ JSON.stringify(result, (_jsonkey, value) =>
75
77
  typeof value === 'bigint' ? `${value}n` : value,
76
78
  ),
77
79
  {
@@ -86,7 +88,7 @@ class Cache extends Base {
86
88
  )}`,
87
89
  );
88
90
  try {
89
- result = JSON.parse(result, (jsonkey, value) => {
91
+ result = JSON.parse(result, (_jsonkey, value) => {
90
92
  if (typeof value === 'string' && /^\d+n$/.test(value)) {
91
93
  return BigInt(value.slice(0, value.length - 1));
92
94
  }
@@ -1,6 +1,6 @@
1
1
  import http from 'node:http';
2
- import path from 'node:path';
3
- import * as url from 'node:url';
2
+ // import path from 'node:path';
3
+ // import * as url from 'node:url';
4
4
  import express from 'express';
5
5
  import RequestLoggerMiddleware from './middleware/RequestLogger.js';
6
6
  import I18nMiddleware from './middleware/I18n.js';
@@ -18,12 +18,12 @@ class HttpServer extends Base {
18
18
  super(app);
19
19
  this.express = express();
20
20
  this.express.disable('x-powered-by');
21
- const dirname = url.fileURLToPath(new URL('.', import.meta.url));
22
- this.express.set('views', [
23
- this.app.foldersConfig.views,
24
- path.join(dirname, '../../views'),
25
- ]);
26
- this.express.set('view engine', 'pug');
21
+ // const dirname = url.fileURLToPath(new URL('.', import.meta.url));
22
+ // this.express.set('views', [
23
+ // this.app.foldersConfig.views,
24
+ // path.join(dirname, '../../views'),
25
+ // ]);
26
+ // this.express.set('view engine', 'pug');
27
27
 
28
28
  this.express.use(new PrepareAppInfoMiddleware(this.app).getMiddleware());
29
29
  this.express.use(new IpDetector(this.app).getMiddleware());
@@ -5,13 +5,14 @@ class GetUserByToken extends AbstractMiddleware {
5
5
  return 'Grab a token and try to parse the user from it. It user exist will add req.appInfo.user variable';
6
6
  }
7
7
 
8
+ // eslint-disable-next-line class-methods-use-this
8
9
  get usedAuthParameters() {
9
10
  return [
10
11
  {
11
12
  name: 'Authorization',
12
13
  type: 'apiKey',
13
14
  in: 'header',
14
- description: this.constructor.description,
15
+ description: GetUserByToken.description,
15
16
  },
16
17
  ];
17
18
  }
@@ -1,37 +1,36 @@
1
1
  import i18next from 'i18next';
2
2
  import BackendFS from 'i18next-fs-backend';
3
- import Backend from 'i18next-chained-backend';
4
3
  import AbstractMiddleware from './AbstractMiddleware.js';
5
4
 
6
5
  class I18n extends AbstractMiddleware {
6
+ cache = {};
7
+
8
+ enabled = true;
9
+
10
+ lookupQuerystring = '';
11
+
12
+ supportedLngs = [];
13
+
14
+ fallbackLng = 'en';
15
+
16
+ /** @type {i18next} */
17
+ i18n = {
18
+ // @ts-ignore
19
+ t: (text) => text,
20
+ language: 'en',
21
+ };
22
+
7
23
  constructor(app, params) {
8
24
  super(app, params);
9
25
  const I18NConfig = this.app.getConfig('i18n');
10
- this.i18n = {
11
- t: (text) => text,
12
- language: I18NConfig.fallbackLng,
13
- };
14
- this.cache = {};
15
26
 
16
27
  if (I18NConfig.enabled) {
17
28
  this.logger.info('Enabling i18n support');
18
29
  this.i18n = i18next;
19
- i18next.use(Backend).init({
30
+ i18next.use(BackendFS).init({
20
31
  backend: {
21
- backends: [
22
- BackendFS,
23
- // BackendFS,
24
- ],
25
- backendOptions: [
26
- // {
27
- // loadPath: __dirname + '/../../locales/{{lng}}/{{ns}}.json',
28
- // addPath: __dirname + '/../../locales/{{lng}}/{{ns}}.missing.json'
29
- // },
30
- {
31
- loadPath: `${this.app.foldersConfig.locales}/{{lng}}/{{ns}}.json`,
32
- addPath: `${this.app.foldersConfig.locales}/{{lng}}/{{ns}}.missing.json`,
33
- },
34
- ],
32
+ loadPath: `${this.app.foldersConfig.locales}/{{lng}}/{{ns}}.json`,
33
+ addPath: `${this.app.foldersConfig.locales}/{{lng}}/{{ns}}.missing.json`,
35
34
  },
36
35
  fallbackLng: I18NConfig.fallbackLng,
37
36
  preload: I18NConfig.preload,
@@ -19,8 +19,8 @@ class Pagination extends AbstractMiddleware {
19
19
  async middleware(req, res, next) {
20
20
  let { limit, maxLimit } = this.params;
21
21
 
22
- limit = typeof limit === 'number' ? parseInt(limit, 10) : 10;
23
- maxLimit = typeof maxLimit === 'number' ? parseInt(maxLimit, 10) : 100;
22
+ limit = typeof limit !== 'number' ? parseInt(limit, 10) : 10;
23
+ maxLimit = typeof maxLimit !== 'number' ? parseInt(maxLimit, 10) : 100;
24
24
 
25
25
  req.appInfo.pagination = {};
26
26
  req.appInfo.pagination.page =