@expressots/core 1.0.1 → 1.2.0

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.
Files changed (149) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +33 -122
  3. package/lib/CHANGELOG.md +120 -0
  4. package/lib/README.md +64 -0
  5. package/lib/cjs/application/app-container.js +40 -0
  6. package/{application → lib/cjs/application}/application.js +101 -62
  7. package/{application → lib/cjs/application}/index.js +9 -9
  8. package/lib/cjs/common/index.js +2 -0
  9. package/lib/cjs/common/project-config.provider.js +2 -0
  10. package/lib/cjs/console/console.js +95 -0
  11. package/{console → lib/cjs/console}/index.js +5 -5
  12. package/lib/cjs/container-module/container-module.js +56 -0
  13. package/{container-module → lib/cjs/container-module}/index.js +5 -5
  14. package/lib/cjs/controller/base-controller.js +77 -0
  15. package/{controller → lib/cjs/controller}/index.js +5 -5
  16. package/{environment → lib/cjs/environment}/env-validator.js +87 -64
  17. package/{environment → lib/cjs/environment}/index.js +5 -5
  18. package/lib/cjs/error/application-error.js +37 -0
  19. package/{error → lib/cjs/error}/error-handler-middleware.js +17 -9
  20. package/{error → lib/cjs/error}/index.js +9 -9
  21. package/lib/cjs/error/report.js +27 -0
  22. package/{error → lib/cjs/error}/status-code.js +83 -83
  23. package/lib/cjs/index.js +24 -0
  24. package/{logger → lib/cjs/logger}/general-logger.js +132 -92
  25. package/{logger → lib/cjs/logger}/index.js +7 -7
  26. package/lib/cjs/types/application/app-container.d.ts +20 -0
  27. package/lib/cjs/types/application/app-container.d.ts.map +1 -0
  28. package/lib/cjs/types/application/application.d.ts +53 -0
  29. package/lib/cjs/types/application/application.d.ts.map +1 -0
  30. package/lib/cjs/types/application/index.d.ts +3 -0
  31. package/lib/cjs/types/application/index.d.ts.map +1 -0
  32. package/lib/cjs/types/common/index.d.ts +2 -0
  33. package/lib/cjs/types/common/index.d.ts.map +1 -0
  34. package/lib/cjs/types/common/project-config.provider.d.ts +22 -0
  35. package/lib/cjs/types/common/project-config.provider.d.ts.map +1 -0
  36. package/lib/cjs/types/console/console.d.ts +28 -0
  37. package/lib/cjs/types/console/console.d.ts.map +1 -0
  38. package/{console → lib/cjs/types/console}/index.d.ts +2 -1
  39. package/lib/cjs/types/console/index.d.ts.map +1 -0
  40. package/lib/cjs/types/container-module/container-module.d.ts +23 -0
  41. package/lib/cjs/types/container-module/container-module.d.ts.map +1 -0
  42. package/lib/cjs/types/container-module/index.d.ts +2 -0
  43. package/lib/cjs/types/container-module/index.d.ts.map +1 -0
  44. package/lib/cjs/types/controller/base-controller.d.ts +30 -0
  45. package/lib/cjs/types/controller/base-controller.d.ts.map +1 -0
  46. package/lib/cjs/types/controller/index.d.ts +2 -0
  47. package/lib/cjs/types/controller/index.d.ts.map +1 -0
  48. package/lib/cjs/types/environment/env-validator.d.ts +28 -0
  49. package/lib/cjs/types/environment/env-validator.d.ts.map +1 -0
  50. package/lib/cjs/types/environment/index.d.ts +2 -0
  51. package/lib/cjs/types/environment/index.d.ts.map +1 -0
  52. package/lib/cjs/types/error/application-error.d.ts +18 -0
  53. package/lib/cjs/types/error/application-error.d.ts.map +1 -0
  54. package/lib/cjs/types/error/error-handler-middleware.d.ts +13 -0
  55. package/lib/cjs/types/error/error-handler-middleware.d.ts.map +1 -0
  56. package/lib/cjs/types/error/index.d.ts +4 -0
  57. package/lib/cjs/types/error/index.d.ts.map +1 -0
  58. package/lib/cjs/types/error/report.d.ts +13 -0
  59. package/lib/cjs/types/error/report.d.ts.map +1 -0
  60. package/{error → lib/cjs/types/error}/status-code.d.ts +137 -136
  61. package/lib/cjs/types/error/status-code.d.ts.map +1 -0
  62. package/lib/cjs/types/index.d.ts +9 -0
  63. package/lib/cjs/types/index.d.ts.map +1 -0
  64. package/lib/cjs/types/logger/general-logger.d.ts +46 -0
  65. package/lib/cjs/types/logger/general-logger.d.ts.map +1 -0
  66. package/lib/cjs/types/logger/index.d.ts +2 -0
  67. package/lib/cjs/types/logger/index.d.ts.map +1 -0
  68. package/lib/esm/application/app-container.js +38 -0
  69. package/lib/esm/application/application.js +96 -0
  70. package/{application/index.d.ts → lib/esm/application/index.js} +2 -2
  71. package/lib/esm/common/index.js +1 -0
  72. package/lib/esm/common/project-config.provider.js +1 -0
  73. package/lib/esm/console/console.js +76 -0
  74. package/lib/esm/console/index.js +1 -0
  75. package/lib/esm/container-module/container-module.js +53 -0
  76. package/{container-module/index.d.ts → lib/esm/container-module/index.js} +1 -1
  77. package/lib/esm/controller/base-controller.js +64 -0
  78. package/{controller/index.d.ts → lib/esm/controller/index.js} +1 -1
  79. package/lib/esm/environment/env-validator.js +80 -0
  80. package/{environment/index.d.ts → lib/esm/environment/index.js} +1 -1
  81. package/lib/esm/error/application-error.js +36 -0
  82. package/lib/esm/error/error-handler-middleware.js +15 -0
  83. package/{error/index.d.ts → lib/esm/error/index.js} +3 -3
  84. package/lib/esm/error/report.js +24 -0
  85. package/lib/esm/error/status-code.js +80 -0
  86. package/{index.d.ts → lib/esm/index.mjs} +8 -7
  87. package/lib/esm/logger/general-logger.js +125 -0
  88. package/{logger/index.d.ts → lib/esm/logger/index.js} +1 -1
  89. package/lib/esm/types/application/app-container.d.ts +20 -0
  90. package/lib/esm/types/application/app-container.d.ts.map +1 -0
  91. package/lib/esm/types/application/application.d.ts +53 -0
  92. package/lib/esm/types/application/application.d.ts.map +1 -0
  93. package/lib/esm/types/application/index.d.ts +3 -0
  94. package/lib/esm/types/application/index.d.ts.map +1 -0
  95. package/lib/esm/types/common/index.d.ts +2 -0
  96. package/lib/esm/types/common/index.d.ts.map +1 -0
  97. package/lib/esm/types/common/project-config.provider.d.ts +22 -0
  98. package/lib/esm/types/common/project-config.provider.d.ts.map +1 -0
  99. package/lib/esm/types/console/console.d.ts +28 -0
  100. package/lib/esm/types/console/console.d.ts.map +1 -0
  101. package/lib/esm/types/console/index.d.ts +2 -0
  102. package/lib/esm/types/console/index.d.ts.map +1 -0
  103. package/lib/esm/types/container-module/container-module.d.ts +23 -0
  104. package/lib/esm/types/container-module/container-module.d.ts.map +1 -0
  105. package/lib/esm/types/container-module/index.d.ts +2 -0
  106. package/lib/esm/types/container-module/index.d.ts.map +1 -0
  107. package/lib/esm/types/controller/base-controller.d.ts +30 -0
  108. package/lib/esm/types/controller/base-controller.d.ts.map +1 -0
  109. package/lib/esm/types/controller/index.d.ts +2 -0
  110. package/lib/esm/types/controller/index.d.ts.map +1 -0
  111. package/lib/esm/types/environment/env-validator.d.ts +28 -0
  112. package/lib/esm/types/environment/env-validator.d.ts.map +1 -0
  113. package/lib/esm/types/environment/index.d.ts +2 -0
  114. package/lib/esm/types/environment/index.d.ts.map +1 -0
  115. package/lib/esm/types/error/application-error.d.ts +18 -0
  116. package/lib/esm/types/error/application-error.d.ts.map +1 -0
  117. package/lib/esm/types/error/error-handler-middleware.d.ts +13 -0
  118. package/lib/esm/types/error/error-handler-middleware.d.ts.map +1 -0
  119. package/lib/esm/types/error/index.d.ts +4 -0
  120. package/lib/esm/types/error/index.d.ts.map +1 -0
  121. package/lib/esm/types/error/report.d.ts +13 -0
  122. package/lib/esm/types/error/report.d.ts.map +1 -0
  123. package/lib/esm/types/error/status-code.d.ts +137 -0
  124. package/lib/esm/types/error/status-code.d.ts.map +1 -0
  125. package/lib/esm/types/index.d.ts +9 -0
  126. package/lib/esm/types/index.d.ts.map +1 -0
  127. package/lib/esm/types/logger/general-logger.d.ts +46 -0
  128. package/lib/esm/types/logger/general-logger.d.ts.map +1 -0
  129. package/lib/esm/types/logger/index.d.ts +2 -0
  130. package/lib/esm/types/logger/index.d.ts.map +1 -0
  131. package/lib/package.json +146 -0
  132. package/package.json +109 -12
  133. package/application/app-container.d.ts +0 -7
  134. package/application/app-container.js +0 -20
  135. package/application/application.d.ts +0 -21
  136. package/console/console.d.ts +0 -9
  137. package/console/console.js +0 -56
  138. package/container-module/container-module.d.ts +0 -8
  139. package/container-module/container-module.js +0 -34
  140. package/controller/base-controller.d.ts +0 -8
  141. package/controller/base-controller.js +0 -37
  142. package/environment/env-validator.d.ts +0 -12
  143. package/error/application-error.d.ts +0 -6
  144. package/error/application-error.js +0 -18
  145. package/error/error-handler-middleware.d.ts +0 -4
  146. package/error/report.d.ts +0 -5
  147. package/error/report.js +0 -15
  148. package/index.js +0 -10
  149. package/logger/general-logger.d.ts +0 -16
@@ -0,0 +1,96 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ var Application_1;
11
+ import express from "express";
12
+ import { provide } from "inversify-binding-decorators";
13
+ import { InversifyExpressServer } from "inversify-express-utils";
14
+ import process from "process";
15
+ import { Console } from "../console/console";
16
+ import errorHandler from "../error/error-handler-middleware";
17
+ /**
18
+ * Enum representing possible server environments.
19
+ */
20
+ var ServerEnvironment;
21
+ (function (ServerEnvironment) {
22
+ ServerEnvironment["Development"] = "development";
23
+ ServerEnvironment["Staging"] = "staging";
24
+ ServerEnvironment["Production"] = "production";
25
+ })(ServerEnvironment || (ServerEnvironment = {}));
26
+ /**
27
+ * The Application class provides a way to configure and manage an Express application.
28
+ * @provide Application
29
+ */
30
+ let Application = Application_1 = class Application {
31
+ app;
32
+ port;
33
+ environment;
34
+ /**
35
+ * Constructs a new instance of the Application class.
36
+ */
37
+ constructor() { }
38
+ /**
39
+ * Configure services that should be initialized before the server starts.
40
+ */
41
+ configureServices() { }
42
+ /**
43
+ * Configure services that should be executed after the server starts.
44
+ */
45
+ postServerInitialization() { }
46
+ /**
47
+ * Perform actions or cleanup after the server is shutdown.
48
+ */
49
+ serverShutdown() {
50
+ process.exit(0);
51
+ }
52
+ /**
53
+ * Create and configure the Express application.
54
+ * @param container - The InversifyJS container.
55
+ * @param middlewares - An array of Express middlewares to be applied.
56
+ * @returns The configured Application instance.
57
+ */
58
+ create(container, middlewares = []) {
59
+ this.configureServices();
60
+ const expressServer = new InversifyExpressServer(container);
61
+ expressServer.setConfig((app) => {
62
+ /* Default body parser application/json */
63
+ app.use(express.json());
64
+ /* Default body parser application/x-www-form-urlencoded */
65
+ app.use(express.urlencoded({ extended: true }));
66
+ middlewares.forEach(middleware => {
67
+ app.use(middleware);
68
+ });
69
+ });
70
+ this.app = expressServer.build();
71
+ /* Add the error handler middleware */
72
+ this.app.use(errorHandler);
73
+ return this;
74
+ }
75
+ /**
76
+ * Start listening on the given port and environment.
77
+ * @param port - The port number to listen on.
78
+ * @param environment - The server environment.
79
+ * @param consoleMessage - Optional message to display in the console.
80
+ */
81
+ listen(port, environment, consoleMessage) {
82
+ this.port = port;
83
+ this.environment = environment;
84
+ this.app.listen(this.port, () => {
85
+ new Console().messageServer(this.port, this.environment, consoleMessage);
86
+ process.on("SIGINT", this.serverShutdown.bind(this));
87
+ });
88
+ this.postServerInitialization();
89
+ }
90
+ };
91
+ Application = Application_1 = __decorate([
92
+ provide(Application_1),
93
+ __metadata("design:paramtypes", [])
94
+ ], Application);
95
+ const appServerInstance = new Application();
96
+ export { appServerInstance as AppInstance, Application, ServerEnvironment };
@@ -1,2 +1,2 @@
1
- export { AppInstance, Application, ServerEnvironment } from './application';
2
- export { AppContainer } from './app-container';
1
+ export { AppInstance, Application, ServerEnvironment } from './application';
2
+ export { AppContainer } from './app-container';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,76 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var Console_1;
8
+ import chalk from "chalk";
9
+ import { provide } from "inversify-binding-decorators";
10
+ /**
11
+ * Enum representing possible color styles for console output.
12
+ */
13
+ var ColorStyle;
14
+ (function (ColorStyle) {
15
+ ColorStyle[ColorStyle["None"] = 0] = "None";
16
+ ColorStyle[ColorStyle["Yellow"] = 1] = "Yellow";
17
+ ColorStyle[ColorStyle["Blue"] = 2] = "Blue";
18
+ ColorStyle[ColorStyle["Green"] = 3] = "Green";
19
+ ColorStyle[ColorStyle["Red"] = 4] = "Red";
20
+ })(ColorStyle || (ColorStyle = {}));
21
+ /**
22
+ * The Console class provides methods for displaying styled messages in the console.
23
+ * @provide Console
24
+ */
25
+ let Console = Console_1 = class Console {
26
+ /**
27
+ * Print a message to the console with the specified color style.
28
+ * @param message - The message to be printed.
29
+ * @param colorStyle - The color style for the message.
30
+ */
31
+ async printColor(message, colorStyle) {
32
+ switch (colorStyle) {
33
+ case ColorStyle.Yellow:
34
+ return console.log(chalk.bgYellow.black(message));
35
+ case ColorStyle.Blue:
36
+ return console.log(chalk.bgBlue.black(message));
37
+ case ColorStyle.Green:
38
+ return console.log(chalk.bgGreen.black(message));
39
+ case ColorStyle.Red:
40
+ return console.log(chalk.bgRed.black(message));
41
+ }
42
+ }
43
+ /**
44
+ * Display a message in the console with details about the running server.
45
+ * @param port - The port number the server is running on.
46
+ * @param environment - The server environment.
47
+ * @param consoleMessage - Optional application message details for console output.
48
+ */
49
+ async messageServer(port, environment, consoleMessage) {
50
+ const appConsoleMessage = {
51
+ appName: consoleMessage?.appName || "Application",
52
+ appVersion: consoleMessage?.appVersion || "not provided",
53
+ };
54
+ let terminalColor = ColorStyle.None;
55
+ switch (environment.toLowerCase()) {
56
+ case "development":
57
+ terminalColor = ColorStyle.Yellow;
58
+ break;
59
+ case "staging":
60
+ terminalColor = ColorStyle.Blue;
61
+ break;
62
+ case "production":
63
+ terminalColor = ColorStyle.Green;
64
+ break;
65
+ default:
66
+ terminalColor = ColorStyle.Red;
67
+ break;
68
+ }
69
+ this.printColor(`${appConsoleMessage.appName} version ${appConsoleMessage.appVersion} is running on ` +
70
+ `port ${port} - Environment: ${environment}`, terminalColor);
71
+ }
72
+ };
73
+ Console = Console_1 = __decorate([
74
+ provide(Console_1)
75
+ ], Console);
76
+ export { Console };
@@ -0,0 +1 @@
1
+ export { Console } from "./console";
@@ -0,0 +1,53 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ var BaseModule_1;
11
+ import { ContainerModule } from "inversify";
12
+ import { provide } from "inversify-binding-decorators";
13
+ /**
14
+ * The BaseModule class provides methods for creating InversifyJS container modules.
15
+ * @provide BaseModule
16
+ */
17
+ let BaseModule = BaseModule_1 = class BaseModule {
18
+ constructor() { }
19
+ /**
20
+ * Create a map of symbols for the provided controllers.
21
+ * @param controllers - An array of controller classes.
22
+ * @returns A map of symbols mapped to controller constructor functions.
23
+ */
24
+ static createSymbols(controllers) {
25
+ const symbols = new Map();
26
+ for (const controller of controllers) {
27
+ const target = controller;
28
+ const symbol = Symbol.for(target.name);
29
+ symbols.set(symbol, target);
30
+ }
31
+ return symbols;
32
+ }
33
+ /**
34
+ * Create an InversifyJS ContainerModule for the provided controllers.
35
+ * @param controllers - An array of controller classes.
36
+ * @returns A ContainerModule with the controller bindings.
37
+ */
38
+ static createContainerModule(controllers) {
39
+ const symbols = BaseModule_1.createSymbols(controllers);
40
+ return new ContainerModule(bind => {
41
+ for (const symbol of symbols) {
42
+ const target = symbol.valueOf();
43
+ bind(target[0]).to(target[1]);
44
+ }
45
+ });
46
+ }
47
+ };
48
+ BaseModule = BaseModule_1 = __decorate([
49
+ provide(BaseModule_1),
50
+ __metadata("design:paramtypes", [])
51
+ ], BaseModule);
52
+ const CreateModule = BaseModule.createContainerModule;
53
+ export { CreateModule };
@@ -1 +1 @@
1
- export { CreateModule } from "./container-module";
1
+ export { CreateModule } from "./container-module";
@@ -0,0 +1,64 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ var BaseController_1;
11
+ import { provide } from 'inversify-binding-decorators';
12
+ import { Report } from '../error';
13
+ /**
14
+ * The BaseController class is an abstract base class for controllers.
15
+ * It provides methods for handling use case calls and sending appropriate responses.
16
+ * @provide BaseController
17
+ */
18
+ let BaseController = BaseController_1 = class BaseController {
19
+ serviceName;
20
+ /**
21
+ * Constructs a new BaseController instance with a specified service name.
22
+ * @param serviceName - The name of the service associated with the controller.
23
+ */
24
+ constructor(serviceName) {
25
+ this.serviceName = serviceName;
26
+ }
27
+ /**
28
+ * Calls an asynchronous use case and sends an appropriate response based on the result.
29
+ * @param useCase - A promise representing the asynchronous use case to call.
30
+ * @param res - The Express response object.
31
+ * @param successStatusCode - The HTTP status code to return upon successful execution.
32
+ */
33
+ async callUseCaseAsync(useCase, res, successStatusCode) {
34
+ let dataReturn;
35
+ try {
36
+ dataReturn = await useCase;
37
+ return res.status(successStatusCode).json(dataReturn);
38
+ }
39
+ catch (error) {
40
+ Report.Error(error);
41
+ }
42
+ }
43
+ /**
44
+ * Calls a use case and sends an appropriate response based on the result.
45
+ * @param useCase - The use case to call.
46
+ * @param res - The Express response object.
47
+ * @param successStatusCode - The HTTP status code to return upon successful execution.
48
+ */
49
+ callUseCase(useCase, res, successStatusCode) {
50
+ let dataReturn;
51
+ try {
52
+ dataReturn = useCase;
53
+ return res.status(successStatusCode).json(dataReturn);
54
+ }
55
+ catch (error) {
56
+ Report.Error(error);
57
+ }
58
+ }
59
+ };
60
+ BaseController = BaseController_1 = __decorate([
61
+ provide(BaseController_1),
62
+ __metadata("design:paramtypes", [String])
63
+ ], BaseController);
64
+ export { BaseController };
@@ -1 +1 @@
1
- export { BaseController } from './base-controller';
1
+ export { BaseController } from './base-controller';
@@ -0,0 +1,80 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var EnvValidatorProvider_1;
8
+ import fs from "fs";
9
+ import path from "path";
10
+ import dotenv from "dotenv";
11
+ import { provide } from "inversify-binding-decorators";
12
+ import { LogLevel, log } from "../logger";
13
+ /**
14
+ * The EnvValidatorProvider class provides utility methods for working with environment variables.
15
+ * It validates, loads, and retrieves environment variables from the .env file.
16
+ * @provide EnvValidatorProvider
17
+ */
18
+ let EnvValidatorProvider = EnvValidatorProvider_1 = class EnvValidatorProvider {
19
+ /**
20
+ * Retrieves the value of an environment variable, or a default value if the variable is not set.
21
+ * @param key - The key of the environment variable.
22
+ * @param defaultValue - The default value to return if the environment variable is not set.
23
+ * @returns The value of the environment variable, or the default value if not set.
24
+ */
25
+ static get(key, defaultValue = undefined) {
26
+ return process.env[key] ?? defaultValue;
27
+ }
28
+ /**
29
+ * Validates and loads all environment variables from the .env file.
30
+ * If the .env file does not exist or any environment variables are not set, the process will exit with an error.
31
+ */
32
+ static checkAll() {
33
+ /* Load .env file */
34
+ dotenv.config();
35
+ /* Verify if .env file exists */
36
+ const envFilePath = path.join(process.cwd(), ".", ".env");
37
+ if (!fs.existsSync(envFilePath)) {
38
+ log(LogLevel.Info, "Environment file .env is not defined.", "env-validator-provider");
39
+ process.exit(1);
40
+ }
41
+ const regexIgnoreDefaultEnvKeys = /^npm_config_/;
42
+ let hasError = false;
43
+ for (const key in process.env) {
44
+ if (regexIgnoreDefaultEnvKeys.test(key)) {
45
+ continue;
46
+ }
47
+ if (!process.env[key] || process.env[key] === "") {
48
+ log(LogLevel.Info, `Environment variable ${key} is not defined.`, "env-validator-provider");
49
+ hasError = true;
50
+ }
51
+ }
52
+ if (hasError) {
53
+ process.exit(1);
54
+ }
55
+ }
56
+ };
57
+ EnvValidatorProvider = EnvValidatorProvider_1 = __decorate([
58
+ provide(EnvValidatorProvider_1)
59
+ ], EnvValidatorProvider);
60
+ String.prototype.AsBoolean = function () {
61
+ switch (this.toLowerCase().trim()) {
62
+ case "true":
63
+ case "1":
64
+ case "yes":
65
+ return true;
66
+ case "false":
67
+ case "0":
68
+ case "no":
69
+ return false;
70
+ default:
71
+ return undefined;
72
+ }
73
+ };
74
+ String.prototype.AsNumber = function () {
75
+ return Number(this);
76
+ };
77
+ String.prototype.AsString = function () {
78
+ return String(this);
79
+ };
80
+ export { EnvValidatorProvider as Environments };
@@ -1 +1 @@
1
- export { Environments } from './env-validator';
1
+ export { Environments } from './env-validator';
@@ -0,0 +1,36 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ var AppError_1;
11
+ import { provide } from "inversify-binding-decorators";
12
+ /**
13
+ * The AppError class extends the built-in Error class, adding a status code and service property.
14
+ * It is designed for handling application-specific errors with more detailed information.
15
+ * @provide AppError
16
+ */
17
+ let AppError = AppError_1 = class AppError extends Error {
18
+ statusCode;
19
+ service;
20
+ /**
21
+ * Constructs a new AppError instance.
22
+ * @param statusCode - The status code associated with the error.
23
+ * @param message - The error message.
24
+ * @param service - An optional service name related to the error.
25
+ */
26
+ constructor(statusCode, message, service) {
27
+ super(message);
28
+ this.statusCode = statusCode;
29
+ this.service = service;
30
+ }
31
+ };
32
+ AppError = AppError_1 = __decorate([
33
+ provide(AppError_1),
34
+ __metadata("design:paramtypes", [Number, String, String])
35
+ ], AppError);
36
+ export { AppError };
@@ -0,0 +1,15 @@
1
+ import { StatusCode } from "./status-code";
2
+ import { LogLevel, log } from "../logger";
3
+ /**
4
+ * errorHandler is a custom Express error-handling middleware function.
5
+ * It logs the error, sets the status code, and sends a JSON response containing the status code and error message.
6
+ * @param error - An instance of AppError containing error details.
7
+ * @param req - The Express request object.
8
+ * @param res - The Express response object.
9
+ * @param next - The Express next function for passing control to the next middleware function.
10
+ */
11
+ function errorHandler(error, req, res, next) {
12
+ log(LogLevel.Error, error, error.service || "service-undefined");
13
+ res.status(error.statusCode || StatusCode.InternalServerError).json({ statusCode: error.statusCode, error: error.message });
14
+ }
15
+ export default errorHandler;
@@ -1,3 +1,3 @@
1
- export { StatusCode } from './status-code';
2
- export { AppError } from './application-error';
3
- export { Report } from './report';
1
+ export { StatusCode } from './status-code';
2
+ export { AppError } from './application-error';
3
+ export { Report } from './report';
@@ -0,0 +1,24 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var Report_1;
8
+ import { provide } from "inversify-binding-decorators";
9
+ /**
10
+ * Report class is a utility class to manage and throw application-specific errors.
11
+ */
12
+ let Report = Report_1 = class Report {
13
+ /**
14
+ * Error method takes an instance of AppError and throws it.
15
+ * @param error - An instance of AppError containing error details.
16
+ */
17
+ static Error(error) {
18
+ throw error;
19
+ }
20
+ };
21
+ Report = Report_1 = __decorate([
22
+ provide(Report_1)
23
+ ], Report);
24
+ export { Report };
@@ -0,0 +1,80 @@
1
+ /* Http Error Code Response */
2
+ var InformationResponse;
3
+ (function (InformationResponse) {
4
+ InformationResponse[InformationResponse["Continue"] = 100] = "Continue";
5
+ InformationResponse[InformationResponse["SwitchingProtocols"] = 101] = "SwitchingProtocols";
6
+ InformationResponse[InformationResponse["Processing"] = 102] = "Processing";
7
+ InformationResponse[InformationResponse["eEarlyHints"] = 103] = "eEarlyHints";
8
+ })(InformationResponse || (InformationResponse = {}));
9
+ var SuccessfulResponse;
10
+ (function (SuccessfulResponse) {
11
+ SuccessfulResponse[SuccessfulResponse["OK"] = 200] = "OK";
12
+ SuccessfulResponse[SuccessfulResponse["Created"] = 201] = "Created";
13
+ SuccessfulResponse[SuccessfulResponse["Accepted"] = 202] = "Accepted";
14
+ SuccessfulResponse[SuccessfulResponse["NonAuthoritativeInformation"] = 203] = "NonAuthoritativeInformation";
15
+ SuccessfulResponse[SuccessfulResponse["NoContent"] = 204] = "NoContent";
16
+ SuccessfulResponse[SuccessfulResponse["ResetContent"] = 205] = "ResetContent";
17
+ SuccessfulResponse[SuccessfulResponse["PartialContent"] = 206] = "PartialContent";
18
+ SuccessfulResponse[SuccessfulResponse["MultiStatus"] = 207] = "MultiStatus";
19
+ SuccessfulResponse[SuccessfulResponse["AlreadyReported"] = 208] = "AlreadyReported";
20
+ SuccessfulResponse[SuccessfulResponse["IMUsed"] = 226] = "IMUsed";
21
+ })(SuccessfulResponse || (SuccessfulResponse = {}));
22
+ var RedirectionMessage;
23
+ (function (RedirectionMessage) {
24
+ RedirectionMessage[RedirectionMessage["MultipleChoices"] = 300] = "MultipleChoices";
25
+ RedirectionMessage[RedirectionMessage["MovedPermanently"] = 301] = "MovedPermanently";
26
+ RedirectionMessage[RedirectionMessage["Found"] = 302] = "Found";
27
+ RedirectionMessage[RedirectionMessage["SeeOther"] = 303] = "SeeOther";
28
+ RedirectionMessage[RedirectionMessage["NotModified"] = 304] = "NotModified";
29
+ RedirectionMessage[RedirectionMessage["TemporaryRedirect"] = 307] = "TemporaryRedirect";
30
+ RedirectionMessage[RedirectionMessage["PermanentRedirect"] = 308] = "PermanentRedirect";
31
+ })(RedirectionMessage || (RedirectionMessage = {}));
32
+ var ClientErrorResponse;
33
+ (function (ClientErrorResponse) {
34
+ ClientErrorResponse[ClientErrorResponse["BadRequest"] = 400] = "BadRequest";
35
+ ClientErrorResponse[ClientErrorResponse["Unauthorized"] = 401] = "Unauthorized";
36
+ ClientErrorResponse[ClientErrorResponse["PaymentRequired"] = 402] = "PaymentRequired";
37
+ ClientErrorResponse[ClientErrorResponse["Forbidden"] = 403] = "Forbidden";
38
+ ClientErrorResponse[ClientErrorResponse["NotFound"] = 404] = "NotFound";
39
+ ClientErrorResponse[ClientErrorResponse["MethodNotAllowed"] = 405] = "MethodNotAllowed";
40
+ ClientErrorResponse[ClientErrorResponse["NotAcceptable"] = 406] = "NotAcceptable";
41
+ ClientErrorResponse[ClientErrorResponse["ProxyAuthenticationRequired"] = 407] = "ProxyAuthenticationRequired";
42
+ ClientErrorResponse[ClientErrorResponse["RequestTimeout"] = 408] = "RequestTimeout";
43
+ ClientErrorResponse[ClientErrorResponse["Conflict"] = 409] = "Conflict";
44
+ ClientErrorResponse[ClientErrorResponse["Gone"] = 410] = "Gone";
45
+ ClientErrorResponse[ClientErrorResponse["LengthRequired"] = 411] = "LengthRequired";
46
+ ClientErrorResponse[ClientErrorResponse["PreconditionFailed"] = 412] = "PreconditionFailed";
47
+ ClientErrorResponse[ClientErrorResponse["PayloadTooLarge"] = 413] = "PayloadTooLarge";
48
+ ClientErrorResponse[ClientErrorResponse["URITooLong"] = 414] = "URITooLong";
49
+ ClientErrorResponse[ClientErrorResponse["UnsupportedMediaType"] = 415] = "UnsupportedMediaType";
50
+ ClientErrorResponse[ClientErrorResponse["RangeNotSatisfiable"] = 416] = "RangeNotSatisfiable";
51
+ ClientErrorResponse[ClientErrorResponse["ExpectationFailed"] = 417] = "ExpectationFailed";
52
+ ClientErrorResponse[ClientErrorResponse["ImATeapot"] = 418] = "ImATeapot";
53
+ ClientErrorResponse[ClientErrorResponse["MisdirectedRequest"] = 421] = "MisdirectedRequest";
54
+ ClientErrorResponse[ClientErrorResponse["UnprocessableEntity"] = 422] = "UnprocessableEntity";
55
+ ClientErrorResponse[ClientErrorResponse["Locked"] = 423] = "Locked";
56
+ ClientErrorResponse[ClientErrorResponse["FailedDependency"] = 424] = "FailedDependency";
57
+ ClientErrorResponse[ClientErrorResponse["TooEarly"] = 425] = "TooEarly";
58
+ ClientErrorResponse[ClientErrorResponse["UpgradeRequired"] = 426] = "UpgradeRequired";
59
+ ClientErrorResponse[ClientErrorResponse["PreconditionRequired"] = 428] = "PreconditionRequired";
60
+ ClientErrorResponse[ClientErrorResponse["TooManyRequests"] = 429] = "TooManyRequests";
61
+ ClientErrorResponse[ClientErrorResponse["RequestHeaderFieldsTooLarge"] = 431] = "RequestHeaderFieldsTooLarge";
62
+ ClientErrorResponse[ClientErrorResponse["UnavailableForLegalReasons"] = 451] = "UnavailableForLegalReasons";
63
+ })(ClientErrorResponse || (ClientErrorResponse = {}));
64
+ var ServerErrorResponse;
65
+ (function (ServerErrorResponse) {
66
+ ServerErrorResponse[ServerErrorResponse["InternalServerError"] = 500] = "InternalServerError";
67
+ ServerErrorResponse[ServerErrorResponse["NotImplemented"] = 501] = "NotImplemented";
68
+ ServerErrorResponse[ServerErrorResponse["BadGateway"] = 502] = "BadGateway";
69
+ ServerErrorResponse[ServerErrorResponse["ServiceUnavailable"] = 503] = "ServiceUnavailable";
70
+ ServerErrorResponse[ServerErrorResponse["GatewayTimeout"] = 504] = "GatewayTimeout";
71
+ ServerErrorResponse[ServerErrorResponse["HTTPVersionNotSupported"] = 505] = "HTTPVersionNotSupported";
72
+ ServerErrorResponse[ServerErrorResponse["VariantAlsoNegotiates"] = 506] = "VariantAlsoNegotiates";
73
+ ServerErrorResponse[ServerErrorResponse["InsufficientStorage"] = 507] = "InsufficientStorage";
74
+ ServerErrorResponse[ServerErrorResponse["LoopDetected"] = 508] = "LoopDetected";
75
+ ServerErrorResponse[ServerErrorResponse["NotExtended"] = 510] = "NotExtended";
76
+ ServerErrorResponse[ServerErrorResponse["NetworkAuthenticationRequired"] = 511] = "NetworkAuthenticationRequired";
77
+ })(ServerErrorResponse || (ServerErrorResponse = {}));
78
+ const HttpStatusErrorCode = { ...InformationResponse, ...SuccessfulResponse, ...RedirectionMessage, ...ClientErrorResponse, ...ServerErrorResponse };
79
+ const StatusCode = { ...HttpStatusErrorCode };
80
+ export { StatusCode };
@@ -1,7 +1,8 @@
1
- export * from "./application";
2
- export * from "./console";
3
- export * from "./container-module";
4
- export * from "./controller";
5
- export * from "./environment";
6
- export * from "./error";
7
- export * from "./logger";
1
+ export * from "./application";
2
+ export * from "./common";
3
+ export * from "./console";
4
+ export * from "./container-module";
5
+ export * from "./controller";
6
+ export * from "./environment";
7
+ export * from "./error";
8
+ export * from "./logger";