@adaptivestone/framework 5.0.0-alpha.4 → 5.0.0-alpha.6

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,12 @@
1
+ ### 5.0.0-alpha.6
2
+
3
+ [UPDATE] Update internal documentation (jsdoc, d.ts)
4
+
5
+ ### 5.0.0-alpha.5
6
+
7
+ [UPDATE] More verbose errors for rapsing body request.
8
+ [UPDATE] deps update
9
+
1
10
  ### 5.0.0-alpha.4
2
11
 
3
12
  [UPDATE] Update rate-limiter-flexible to v5
@@ -60,7 +60,9 @@ class CreateUser extends AbstractCommand {
60
60
 
61
61
  await user.generateToken();
62
62
 
63
- this.logger.info(`User was created/updated ${JSON.stringify(user, 0, 4)}`);
63
+ this.logger.info(
64
+ `User was created/updated ${JSON.stringify(user, null, 4)}`,
65
+ );
64
66
 
65
67
  return user;
66
68
  }
package/jsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "node16",
4
+ "target": "ES2022",
5
+ "moduleResolution": "node16",
6
+ "checkJs": true
7
+ },
8
+ "exclude": ["node_modules"]
9
+ }
@@ -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
- * @override
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
  );
@@ -370,6 +373,7 @@ 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<AbstractMiddleware | [Function, ...any]>>}
373
377
  * @example
374
378
  * return new Map([
375
379
  * ['/*', [GetUserByToken]] // for any method for this controller
@@ -3,12 +3,12 @@ import Base from './Base.js';
3
3
 
4
4
  class AbstractModel extends Base {
5
5
  /**
6
- * @param {import('../server')} app //TODO change to *.d.ts as this is a Server, not app
6
+ * @param {import('../server.js').default['app']} app //TODO change to *.d.ts as this is a Server, not app
7
7
  * @param function callback optional callback when connection ready
8
8
  */
9
9
  constructor(app, callback = () => {}) {
10
10
  super(app);
11
- this.mongooseSchema = mongoose.Schema(this.modelSchema);
11
+ this.mongooseSchema = new mongoose.Schema(this.modelSchema);
12
12
  mongoose.set('strictQuery', true);
13
13
  this.mongooseSchema.set('timestamps', true);
14
14
  this.mongooseSchema.set('minimize', false);
package/modules/Base.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import winston from 'winston';
2
- import Server from '../server';
2
+ import Server from '../server.js';
3
+ import type { Dirent } from '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<Dirent[]>;
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 = Base;
37
+ export default Base;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adaptivestone/framework",
3
- "version": "5.0.0-alpha.4",
3
+ "version": "5.0.0-alpha.6",
4
4
  "description": "Adaptive stone node js framework",
5
5
  "main": "index.js",
6
6
  "type": "module",
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: boolean;
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 = Server;
98
+ export default Server;
package/server.js CHANGED
@@ -19,6 +19,8 @@ class Server {
19
19
 
20
20
  #isInited = false;
21
21
 
22
+ cli = null;
23
+
22
24
  /**
23
25
  * Construct new server
24
26
  * @param {Object} config main config object
@@ -56,8 +58,6 @@ class Server {
56
58
  models: new Map(),
57
59
  modelConstructors: new Map(),
58
60
  };
59
-
60
- this.cli = false;
61
61
  }
62
62
 
63
63
  /**
@@ -350,7 +350,7 @@ class Server {
350
350
  * Return model from {modelName} (file name) on model folder.
351
351
  * Support cache
352
352
  * @param {String} modelName name on config file to load
353
- * @returns {import('mongoose').Model}
353
+ * @returns {import('mongoose').Model | false| {}}
354
354
  */
355
355
  getModel(modelName) {
356
356
  if (modelName.endsWith('s')) {
@@ -1,5 +1,5 @@
1
1
  import Base from '../../modules/Base';
2
- import Server from '../../server';
2
+ import Server from '../../server.js';
3
3
 
4
4
  declare class Cache extends Base {
5
5
  app: Server['app'];
@@ -32,4 +32,4 @@ declare class Cache extends Base {
32
32
  removeKey(key: string): Promise<number>;
33
33
  }
34
34
 
35
- export = Cache;
35
+ export default Cache;
@@ -57,7 +57,7 @@ class HttpServer extends Base {
57
57
  res.status(500).json({ message: 'Something broke!' });
58
58
  });
59
59
 
60
- this.httpServer = http.Server(this.express);
60
+ this.httpServer = http.createServer(this.express);
61
61
 
62
62
  const listener = this.httpServer.listen(
63
63
  httpConfig.port,
@@ -11,7 +11,7 @@ class GetUserByToken extends AbstractMiddleware {
11
11
  name: 'Authorization',
12
12
  type: 'apiKey',
13
13
  in: 'header',
14
- description: this?.description,
14
+ description: this.constructor.description,
15
15
  },
16
16
  ];
17
17
  }
@@ -18,7 +18,10 @@ class RequestParser extends AbstractMiddleware {
18
18
  [fields, files] = await form.parse(req);
19
19
  } catch (err) {
20
20
  this.logger.error(`Parsing failed ${err}`);
21
- return next(err);
21
+ return res.status(400).json({
22
+ message: `Error to parse your request. You provided invalid content type or content-length. Please check your request headers and content type.`,
23
+ });
24
+ // return next(err);
22
25
  }
23
26
  this.logger.verbose(
24
27
  `Parsing multipart/formdata request DONE ${Date.now() - time}ms`,
@@ -79,12 +79,21 @@ d\r
79
79
  const server = createServer(async (req, res) => {
80
80
  req.appInfo = {};
81
81
  const middleware = new RequestParser(global.server.app);
82
- middleware.middleware(req, {}, (err) => {
83
- expect(err).toBeDefined();
82
+ let status;
84
83
 
85
- res.writeHead(200);
86
- res.end('ok');
87
- });
84
+ const resp = {
85
+ status: (code) => {
86
+ status = code;
87
+ return resp;
88
+ },
89
+ json: () => resp,
90
+ };
91
+ await middleware.middleware(req, resp, () => {});
92
+ expect(status).toBe(400);
93
+ // expect(err).toBeDefined();
94
+
95
+ res.writeHead(200);
96
+ res.end('ok');
88
97
  });
89
98
  server.listen(null, async () => {
90
99
  const chosenPort = server.address().port;
@@ -66,7 +66,8 @@ class Mail extends Base {
66
66
  * @param {object} templateData
67
67
  * @returns string
68
68
  */
69
- static async #renderTemplateFile({ type, fullPath } = {}, templateData = {}) {
69
+ // eslint-disable-next-line class-methods-use-this
70
+ async #renderTemplateFile({ type, fullPath } = {}, templateData = {}) {
70
71
  if (!type) {
71
72
  return null;
72
73
  }
@@ -116,19 +117,10 @@ class Mail extends Base {
116
117
 
117
118
  const [htmlRendered, subjectRendered, textRendered, extraCss] =
118
119
  await Promise.all([
119
- this.constructor.#renderTemplateFile(
120
- templates.html,
121
- templateDataToRender,
122
- ),
123
- this.constructor.#renderTemplateFile(
124
- templates.subject,
125
- templateDataToRender,
126
- ),
127
- this.constructor.#renderTemplateFile(
128
- templates.text,
129
- templateDataToRender,
130
- ),
131
- this.constructor.#renderTemplateFile(templates.style),
120
+ this.#renderTemplateFile(templates.html, templateDataToRender),
121
+ this.#renderTemplateFile(templates.subject, templateDataToRender),
122
+ this.#renderTemplateFile(templates.text, templateDataToRender),
123
+ this.#renderTemplateFile(templates.style),
132
124
  ]);
133
125
 
134
126
  juice.tableElements = ['TABLE'];
@@ -171,7 +163,7 @@ class Mail extends Base {
171
163
 
172
164
  /**
173
165
  * Send provided text (html) to email. Low level function. All data should be prepared before sending (like inline styles)
174
- * @param {objetc} app application
166
+ * @param {import('../../../server.js').default['app']} app application
175
167
  * @param {string} to send to
176
168
  * @param {string} subject email topic
177
169
  * @param {string} html hmlt body of emain