@expressots/core 2.16.1 → 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 +12 -0
- package/lib/cjs/controller/base-controller.js +23 -34
- package/lib/cjs/error/error-handler-middleware.js +3 -6
- package/lib/cjs/middleware/middleware-service.js +1 -1
- package/lib/cjs/types/controller/base-controller.d.ts +7 -23
- package/lib/cjs/types/error/error-handler-middleware.d.ts +2 -3
- package/lib/package.json +1 -1
- package/package.json +1 -1
package/lib/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
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
|
+
|
|
3
15
|
## [2.16.1](https://github.com/expressots/expressots/compare/2.16.0...2.16.1) (2024-08-21)
|
|
4
16
|
|
|
5
17
|
|
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
*
|
|
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
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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,
|
|
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
|
-
|
|
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
|
-
|
|
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,
|
|
286
|
+
(0, error_handler_middleware_1.default)(error, res, next, showStackTrace);
|
|
287
287
|
};
|
|
288
288
|
}
|
|
289
289
|
else {
|
|
@@ -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<
|
|
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:
|
|
22
|
+
protected callUseCase<T>(useCase: T, res: Response, successStatusCode?: number): void;
|
|
23
23
|
/**
|
|
24
|
-
*
|
|
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
|
-
|
|
30
|
+
private handleError;
|
|
47
31
|
}
|
|
48
32
|
export { BaseController };
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import { NextFunction,
|
|
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,
|
|
10
|
+
declare function defaultErrorHandler(error: Error, res: Response, next: NextFunction, showStackTrace?: boolean): void;
|
|
12
11
|
export default defaultErrorHandler;
|
package/lib/package.json
CHANGED