@expressots/core 2.16.0 → 2.16.2

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/lib/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
 
2
2
 
3
+ ## [2.16.2](https://github.com/expressots/expressots/compare/2.16.1...2.16.2) (2024-09-02)
4
+
5
+
6
+ ### Code Refactoring
7
+
8
+ * add stackTrace app provider and add @expressots/cli to devDependencies ([212c681](https://github.com/expressots/expressots/commit/212c681eb32e4319545254f62d079da4b457a89a))
9
+ * add test and coverage for base controller and error middleware ([5ddb7b2](https://github.com/expressots/expressots/commit/5ddb7b2265d8a7bcd3298133d5f7bb6c14e6d7ef))
10
+ * adjust error middleware code property ([ef4e9b8](https://github.com/expressots/expressots/commit/ef4e9b8c2b93e5e3431172f60a28c433858557ae))
11
+ * remove render from base controller, add usecase as type T ([9002129](https://github.com/expressots/expressots/commit/9002129f5f1eddd9da7c3b1f641e225d3776159a))
12
+ * update templates main.ts removing reflect and serverenv type ([e5a789c](https://github.com/expressots/expressots/commit/e5a789c2a158eef1c0de658f00bb833e1eb8f644))
13
+ * update templates nonop & add .env gitignore ([304cf88](https://github.com/expressots/expressots/commit/304cf885e9e9da7dbc7c24184868b1a557d93d04))
14
+
15
+ ## [2.16.1](https://github.com/expressots/expressots/compare/2.16.0...2.16.1) (2024-08-21)
16
+
17
+
18
+ ### Bug Fixes
19
+
20
+ * remove reflect-metadata from main, add server env types ([43a5a33](https://github.com/expressots/expressots/commit/43a5a33d64381456930800555aef3ba0c6d1fd29))
21
+
3
22
  ## [2.16.0](https://github.com/expressots/expressots/compare/2.15.0...2.16.0) (2024-08-08)
4
23
 
5
24
 
@@ -10,6 +10,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.AppContainer = void 0;
13
+ require("reflect-metadata");
13
14
  const inversify_1 = require("inversify");
14
15
  const inversify_binding_decorators_1 = require("inversify-binding-decorators");
15
16
  /**
@@ -1,2 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ServerEnvironment = void 0;
4
+ var server_env_types_1 = require("./server-env.types");
5
+ Object.defineProperty(exports, "ServerEnvironment", { enumerable: true, get: function () { return server_env_types_1.ServerEnvironment; } });
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ServerEnvironment = void 0;
4
+ /**
5
+ * Enum representing possible server environments.
6
+ * @options Development - Development environment.
7
+ * @options Production - Production environment.
8
+ */
9
+ var ServerEnvironment;
10
+ (function (ServerEnvironment) {
11
+ ServerEnvironment["Development"] = "development";
12
+ ServerEnvironment["Production"] = "production";
13
+ })(ServerEnvironment || (exports.ServerEnvironment = ServerEnvironment = {}));
@@ -20,8 +20,14 @@ let BaseController = class BaseController {
20
20
  * @param res - The Express response object.
21
21
  * @param successStatusCode - The HTTP status code to return upon successful execution.
22
22
  */
23
- async callUseCaseAsync(useCase, res, successStatusCode) {
24
- return res.status(successStatusCode).json(await useCase);
23
+ async callUseCaseAsync(useCase, res, successStatusCode = 200) {
24
+ try {
25
+ const result = await useCase;
26
+ res.status(successStatusCode).json(result);
27
+ }
28
+ catch (error) {
29
+ this.handleError(res, error);
30
+ }
25
31
  }
26
32
  /**
27
33
  * Calls a use case and sends an appropriate response based on the result.
@@ -29,42 +35,25 @@ let BaseController = class BaseController {
29
35
  * @param res - The Express response object.
30
36
  * @param successStatusCode - The HTTP status code to return upon successful execution.
31
37
  */
32
- callUseCase(useCase, res, successStatusCode) {
33
- return res.status(successStatusCode).json(useCase);
38
+ callUseCase(useCase, res, successStatusCode = 200) {
39
+ try {
40
+ res.status(successStatusCode).json(useCase);
41
+ }
42
+ catch (error) {
43
+ this.handleError(res, error);
44
+ }
34
45
  }
35
46
  /**
36
- * Synchronously renders a template with the given options using the Express `Response` object's render method.
37
- *
38
- * @protected
39
- * @method callUseRender
40
- *
41
- * @param {Response} res - The Express `Response` object.
42
- * @param {string} template - The name of the template to render.
43
- * @param {Object} [options={}] - An optional object containing data to be passed to the template.
44
- *
45
- */
46
- callUseRender(res, template, options = {}) {
47
- return res.render(template, options);
48
- }
49
- /**
50
- * Asynchronously renders a template with the given options using the Express `Response` object's render method.
51
- *
52
- * @protected
53
- * @method callUseRenderAsync
54
- *
55
- * @param {Response} res - The Express `Response` object.
56
- * @param {string} template - The name of the template to render.
57
- * @param {Object} [options={}] - An optional object containing data to be passed to the template.
47
+ * Handles errors by sending a 500 status with an error message.
48
+ * This method can be extended to provide more detailed error handling.
58
49
  *
50
+ * @param res - The Express response object.
51
+ * @param error - The error that occurred during the execution of the use case.
59
52
  */
60
- callUseRenderAsync(res, template, options = {}) {
61
- return new Promise((resolve, reject) => {
62
- res.render(template, options, (err, compiled) => {
63
- if (err) {
64
- reject(err);
65
- }
66
- resolve(compiled);
67
- });
53
+ handleError(res, error) {
54
+ res.status(500).json({
55
+ message: "An unexpected error occurred.",
56
+ error: error instanceof Error ? error.message : error,
68
57
  });
69
58
  }
70
59
  };
@@ -7,24 +7,21 @@ const utils_1 = require("./utils");
7
7
  * errorHandler is a custom Express error-handling middleware function.
8
8
  * It logs the error, sets the status code, and sends a JSON response containing the status code and error message.
9
9
  * @param error - An instance of IAppError containing error details.
10
- * @param req - The Express request object.
11
10
  * @param res - The Express response object.
12
11
  * @param next - The Express next function for passing control to the next middleware function.
13
12
  * @param showStackTrace - A boolean value indicating whether to show the stack trace in the response.
14
13
  */
15
- function defaultErrorHandler(error, req, res,
16
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
17
- next, showStackTrace = false) {
14
+ function defaultErrorHandler(error, res, next, showStackTrace = false) {
18
15
  try {
19
16
  if (error instanceof app_error_1.AppError) {
20
17
  res.status(error.statusCode).json({
21
- statusCode: error.statusCode,
18
+ code: error.statusCode,
22
19
  error: error.message,
23
20
  });
24
21
  }
25
22
  else {
26
23
  res.status(status_code_1.StatusCode.InternalServerError).json({
27
- statusCode: status_code_1.StatusCode.InternalServerError,
24
+ code: status_code_1.StatusCode.InternalServerError,
28
25
  error: "An unexpected error occurred.",
29
26
  });
30
27
  }
@@ -283,7 +283,7 @@ let Middleware = class Middleware {
283
283
  const { errorHandler: errorHandling, showStackTrace } = options;
284
284
  if (!errorHandling) {
285
285
  this.errorHandler = (error, req, res, next) => {
286
- (0, error_handler_middleware_1.default)(error, req, res, next, showStackTrace);
286
+ (0, error_handler_middleware_1.default)(error, res, next, showStackTrace);
287
287
  };
288
288
  }
289
289
  else {
@@ -1,3 +1,4 @@
1
+ import "reflect-metadata";
1
2
  import { Container, ContainerModule, interfaces } from "inversify";
2
3
  /**
3
4
  * Interface for container options that can be passed to the AppContainer class.
@@ -1 +1,2 @@
1
1
  export { Pattern, ExpressoConfig } from "./project-config";
2
+ export { ServerEnvironment } from "./server-env.types";
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Enum representing possible server environments.
3
+ * @options Development - Development environment.
4
+ * @options Production - Production environment.
5
+ */
6
+ export declare enum ServerEnvironment {
7
+ Development = "development",
8
+ Production = "production"
9
+ }
@@ -12,37 +12,21 @@ declare abstract class BaseController implements Controller {
12
12
  * @param res - The Express response object.
13
13
  * @param successStatusCode - The HTTP status code to return upon successful execution.
14
14
  */
15
- protected callUseCaseAsync(useCase: Promise<any>, res: Response, successStatusCode: number): Promise<any>;
15
+ protected callUseCaseAsync<T>(useCase: Promise<T>, res: Response, successStatusCode?: number): Promise<void>;
16
16
  /**
17
17
  * Calls a use case and sends an appropriate response based on the result.
18
18
  * @param useCase - The use case to call.
19
19
  * @param res - The Express response object.
20
20
  * @param successStatusCode - The HTTP status code to return upon successful execution.
21
21
  */
22
- protected callUseCase(useCase: any, res: Response, successStatusCode: number): any;
22
+ protected callUseCase<T>(useCase: T, res: Response, successStatusCode?: number): void;
23
23
  /**
24
- * Synchronously renders a template with the given options using the Express `Response` object's render method.
25
- *
26
- * @protected
27
- * @method callUseRender
28
- *
29
- * @param {Response} res - The Express `Response` object.
30
- * @param {string} template - The name of the template to render.
31
- * @param {Object} [options={}] - An optional object containing data to be passed to the template.
32
- *
33
- */
34
- protected callUseRender(res: Response, template: string, options?: object): void;
35
- /**
36
- * Asynchronously renders a template with the given options using the Express `Response` object's render method.
37
- *
38
- * @protected
39
- * @method callUseRenderAsync
40
- *
41
- * @param {Response} res - The Express `Response` object.
42
- * @param {string} template - The name of the template to render.
43
- * @param {Object} [options={}] - An optional object containing data to be passed to the template.
24
+ * Handles errors by sending a 500 status with an error message.
25
+ * This method can be extended to provide more detailed error handling.
44
26
  *
27
+ * @param res - The Express response object.
28
+ * @param error - The error that occurred during the execution of the use case.
45
29
  */
46
- protected callUseRenderAsync(res: Response, template: string, options?: object): Promise<string>;
30
+ private handleError;
47
31
  }
48
32
  export { BaseController };
@@ -1,12 +1,11 @@
1
- import { NextFunction, Request, Response } from "express";
1
+ import { NextFunction, Response } from "express";
2
2
  /**
3
3
  * errorHandler is a custom Express error-handling middleware function.
4
4
  * It logs the error, sets the status code, and sends a JSON response containing the status code and error message.
5
5
  * @param error - An instance of IAppError containing error details.
6
- * @param req - The Express request object.
7
6
  * @param res - The Express response object.
8
7
  * @param next - The Express next function for passing control to the next middleware function.
9
8
  * @param showStackTrace - A boolean value indicating whether to show the stack trace in the response.
10
9
  */
11
- declare function defaultErrorHandler(error: Error, req: Request, res: Response, next: NextFunction, showStackTrace?: boolean): void;
10
+ declare function defaultErrorHandler(error: Error, res: Response, next: NextFunction, showStackTrace?: boolean): void;
12
11
  export default defaultErrorHandler;
package/lib/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@expressots/core",
3
- "version": "2.16.0",
3
+ "version": "2.16.2",
4
4
  "description": "Expressots - modern, fast, lightweight nodejs web framework (@core)",
5
5
  "author": "Richard Zampieri",
6
6
  "main": "./lib/cjs/index.js",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@expressots/core",
3
- "version": "2.16.0",
3
+ "version": "2.16.2",
4
4
  "description": "Expressots - modern, fast, lightweight nodejs web framework (@core)",
5
5
  "author": "Richard Zampieri",
6
6
  "main": "./lib/cjs/index.js",