@expressots/core 1.0.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.
- package/README.md +153 -0
- package/application/app-container.d.ts +7 -0
- package/application/app-container.js +20 -0
- package/application/application.d.ts +21 -0
- package/application/application.js +62 -0
- package/application/index.d.ts +2 -0
- package/application/index.js +9 -0
- package/console/console.d.ts +9 -0
- package/console/console.js +56 -0
- package/console/index.d.ts +1 -0
- package/console/index.js +5 -0
- package/container-module/container-module.d.ts +8 -0
- package/container-module/container-module.js +34 -0
- package/container-module/index.d.ts +1 -0
- package/container-module/index.js +5 -0
- package/controller/base-controller.d.ts +8 -0
- package/controller/base-controller.js +37 -0
- package/controller/index.d.ts +1 -0
- package/controller/index.js +5 -0
- package/environment/env-validator.d.ts +12 -0
- package/environment/env-validator.js +64 -0
- package/environment/index.d.ts +1 -0
- package/environment/index.js +5 -0
- package/error/application-error.d.ts +6 -0
- package/error/application-error.js +18 -0
- package/error/error-handler-middleware.d.ts +4 -0
- package/error/error-handler-middleware.js +9 -0
- package/error/index.d.ts +3 -0
- package/error/index.js +9 -0
- package/error/report.d.ts +5 -0
- package/error/report.js +15 -0
- package/error/status-code.d.ts +136 -0
- package/error/status-code.js +83 -0
- package/index.d.ts +7 -0
- package/index.js +10 -0
- package/logger/general-logger.d.ts +16 -0
- package/logger/general-logger.js +92 -0
- package/logger/index.d.ts +1 -0
- package/logger/index.js +7 -0
- package/package.json +50 -0
package/README.md
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://expresso-ts.com/" target="blank"><img src="https://github.com/expressots/expressots/blob/main/media/alogo.png" width="120" alt="Expresso TS Logo" /></a>
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
# Expresso TS
|
|
6
|
+
|
|
7
|
+
A Typescript + [Node.js]("https://nodejs.org/en/") lightweight framework for quick building scalable, easy to read and maintain, server-side applications 🚀
|
|
8
|
+
|
|
9
|
+
## Philosophy
|
|
10
|
+
|
|
11
|
+
Expresso TS is a framework designed to make the lives of the developers easier by providing a structure for building server-side applications that is clear to read, maintain and scale. The philosophy is centered around the idea that developers should not have to waste time on repetitive tasks such as setting up a logging system, authentication, error handling, database connection, and organizing the project for better maintainability.
|
|
12
|
+
|
|
13
|
+
Expresso TS offers a solution that is designed to help developers jump ahead and focus on the most important part of the development process, writing code. The framework provides capability to the developers to quickly extend the framework functionalities by creating providers and adding them to the dependency injection system. This way, developers can use these new functionalities throughout the entire application without having to worry about the complexities of integrating it into the system.
|
|
14
|
+
|
|
15
|
+
## The Project Structure
|
|
16
|
+
|
|
17
|
+
### Clean code using solid principles with Nodejs and Typescript
|
|
18
|
+
|
|
19
|
+
The idea of this project is to offer a clean and concise architecture boilerplate for those trying to navigate on node development.
|
|
20
|
+
|
|
21
|
+
It respects the fundamentals of clean code and some of the SOLID concepts. This boilerplate has my own flavour, which means that I don't follow follow strictly all the concepts, I rather customize to my own needs. I tried to extract the best practices of the 5 main principles:
|
|
22
|
+
|
|
23
|
+
- **_S_**: Single responsibility
|
|
24
|
+
- **_O_**: Open-Close
|
|
25
|
+
- **_L_**: Liskov substitution
|
|
26
|
+
- **_I_**: Interface segregation
|
|
27
|
+
- **_D_**: Dependency inversion
|
|
28
|
+
|
|
29
|
+
`Reference:` [Uncle Bob article](http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod)
|
|
30
|
+
|
|
31
|
+
> 
|
|
32
|
+
|
|
33
|
+
- **_Entities_**: class definitions, or models with their attributes, properties and methods.
|
|
34
|
+
- **_Providers_**: is the layer responsible to provide externals resources such as database, email services. Everything external to the application.
|
|
35
|
+
- **_Repositories_**: is the layer responsible to communicate with the database.
|
|
36
|
+
- **_Use Cases_**: use cases represents the implementation of an operation that can be performed in the system
|
|
37
|
+
|
|
38
|
+
## Key Features
|
|
39
|
+
|
|
40
|
+
- Module mapping (tsconfig-paths) : Entity, Provider, Use case, repository, controller examples
|
|
41
|
+
- IOC (Inversion of control): Dependency Injection with InversifyJS
|
|
42
|
+
- API Decorators: HttpGet, HttpPost, HttpPut, HttpDelete, HttpPatch, HttpOptions, HttpHead
|
|
43
|
+
- Error handling
|
|
44
|
+
- API Logger : Morgan
|
|
45
|
+
- Application General Logger: Winston
|
|
46
|
+
- App container and module creation
|
|
47
|
+
|
|
48
|
+
## Feature Details
|
|
49
|
+
|
|
50
|
+
- **Module Mapping**: Using the tsconfig-paths module, we are mapping the entities, providers, repositories, use cases folders, so that we can import them using relative paths. This allows us to create a project structure that is configured using a development pattern.
|
|
51
|
+
|
|
52
|
+
```json
|
|
53
|
+
"paths": {
|
|
54
|
+
"@entities/*": ["entities/*"],
|
|
55
|
+
"@providers/*": ["providers/*"],
|
|
56
|
+
"@repositories/*": ["repositories/*"],
|
|
57
|
+
"@useCases/*": ["useCases/*"],
|
|
58
|
+
},
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
- **IOC**: Using InversifyJS we are creating the IoC container and registering all the dependencies. The project contains AppContainer class to register the modules, and a mechanism to create modules in which controllers can bind to it.
|
|
62
|
+
|
|
63
|
+
AppContainer for registering the modules:
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
const container = new Container();
|
|
67
|
+
|
|
68
|
+
container.load(buildProviderModule(), UserContainerModule);
|
|
69
|
+
|
|
70
|
+
export { container };
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Creating the modules and registering the controllers:
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
export const UserContainerModule = CreateModule([
|
|
77
|
+
CreateUserController,
|
|
78
|
+
DeleteUserController,
|
|
79
|
+
FindByIdController,
|
|
80
|
+
UpdateUserController,
|
|
81
|
+
FindAllUsersController,
|
|
82
|
+
]);
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
- **API Decorators**: Using the decorators we are creating the endpoints for the controllers.
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
@controller('/user/create')
|
|
89
|
+
@httpPost('/')
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
- **Entity, Provider, Use case, repository, controller examples**: Folders organization for the clean architecture.
|
|
93
|
+
|
|
94
|
+
- **Error handling**: Using `Report.Error()` we can report errors using predefined error codes. This is useful when we want to report known errors to the client. Inside of `Report.Error()` there is a try catch block encapsulating the error.
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
type ErrorType = GeneralErrorCode | ApplicationErrorCode | HttpStatusErrorCode;
|
|
98
|
+
|
|
99
|
+
// Reporting errors
|
|
100
|
+
if (userExist) {
|
|
101
|
+
const error = Report.Error(new ApplicationError(StatusCode.BadRequest, "User already exist!"), "user-create");
|
|
102
|
+
return error;
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
- **API Request Logger**: Morgan is a logger for nodejs. It is a middleware that logs the requests and responses in a file.
|
|
107
|
+
|
|
108
|
+
```Text
|
|
109
|
+
[localhost ::ffff:127.0.0.1]-[2023-02-02 17:2:16]-[pid: 48644] POST /user/create 151 - 0.007 ms - -
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
- **Application General Logger**: Winston is a logger for nodejs. It is a middleware that logs all the other exceptions in a file.
|
|
113
|
+
|
|
114
|
+
```Text
|
|
115
|
+
[2023-02-01 20:27:58] [core-api] [user-create] error: User already exist! - (Error) [file: CreateUserUseCase.<anonymous> (<app_path>\src\useCases\user\create\CreateUser.UseCase.ts:31:50)]
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
How To Log:
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
Log(LogLevel.Error, error, this.serviceName);
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
- LogLevel: enum with the following values: Debug, Error, Info
|
|
125
|
+
- error: Error object
|
|
126
|
+
- serviceName: is the name of the service that is generating the error
|
|
127
|
+
|
|
128
|
+
## Getting Started
|
|
129
|
+
|
|
130
|
+
To get started with Expresso TS, simply clone the repository and follow the below steps
|
|
131
|
+
|
|
132
|
+
```
|
|
133
|
+
1. run `yarn install` to install all dependencies
|
|
134
|
+
2. run `yarn start` to start the project
|
|
135
|
+
|
|
136
|
+
3. run the docker-compose file to create the mongodb container
|
|
137
|
+
4. create the user and password in mongodb and apply management rights
|
|
138
|
+
5. add the user and password to the .env file
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## Documentation
|
|
142
|
+
|
|
143
|
+
To be developed
|
|
144
|
+
|
|
145
|
+
## Contributing Guide
|
|
146
|
+
|
|
147
|
+
```
|
|
148
|
+
1. Clone it to your local
|
|
149
|
+
2. Contribute to it
|
|
150
|
+
3. Push it to your remote repo
|
|
151
|
+
4. Send a PR to the main repo with your branch
|
|
152
|
+
5. Your contribution will be evaluated then we will merge your changes with the original repository.
|
|
153
|
+
```
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var AppContainer_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.AppContainer = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const inversify_1 = require("inversify");
|
|
7
|
+
const inversify_binding_decorators_1 = require("inversify-binding-decorators");
|
|
8
|
+
let AppContainer = AppContainer_1 = class AppContainer {
|
|
9
|
+
constructor() { }
|
|
10
|
+
create(modules) {
|
|
11
|
+
this.container = new inversify_1.Container();
|
|
12
|
+
this.container.load((0, inversify_binding_decorators_1.buildProviderModule)(), ...modules);
|
|
13
|
+
return this.container;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
AppContainer = AppContainer_1 = tslib_1.__decorate([
|
|
17
|
+
(0, inversify_binding_decorators_1.provide)(AppContainer_1),
|
|
18
|
+
tslib_1.__metadata("design:paramtypes", [])
|
|
19
|
+
], AppContainer);
|
|
20
|
+
exports.AppContainer = AppContainer;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import { Container } from "inversify";
|
|
3
|
+
import { IApplicationMessageToConsole } from "../console/console";
|
|
4
|
+
declare enum ServerEnvironment {
|
|
5
|
+
Development = "development",
|
|
6
|
+
Staging = "staging",
|
|
7
|
+
Production = "production"
|
|
8
|
+
}
|
|
9
|
+
declare class Application {
|
|
10
|
+
private app;
|
|
11
|
+
private port;
|
|
12
|
+
private environment;
|
|
13
|
+
constructor();
|
|
14
|
+
protected configureServices(): void;
|
|
15
|
+
protected postServerInitialization(): void;
|
|
16
|
+
protected serverShutdown(): void;
|
|
17
|
+
create(container: Container, middlewares?: express.RequestHandler[]): Application;
|
|
18
|
+
listen(port: number, environment: ServerEnvironment, consoleMessage?: IApplicationMessageToConsole): void;
|
|
19
|
+
}
|
|
20
|
+
declare const appServerInstance: Application;
|
|
21
|
+
export { appServerInstance as AppInstance, Application, ServerEnvironment };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var Application_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.ServerEnvironment = exports.Application = exports.AppInstance = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const express_1 = tslib_1.__importDefault(require("express"));
|
|
7
|
+
const inversify_binding_decorators_1 = require("inversify-binding-decorators");
|
|
8
|
+
const inversify_express_utils_1 = require("inversify-express-utils");
|
|
9
|
+
const process_1 = tslib_1.__importDefault(require("process"));
|
|
10
|
+
const console_1 = require("../console/console");
|
|
11
|
+
const error_handler_middleware_1 = tslib_1.__importDefault(require("../error/error-handler-middleware"));
|
|
12
|
+
var ServerEnvironment;
|
|
13
|
+
(function (ServerEnvironment) {
|
|
14
|
+
ServerEnvironment["Development"] = "development";
|
|
15
|
+
ServerEnvironment["Staging"] = "staging";
|
|
16
|
+
ServerEnvironment["Production"] = "production";
|
|
17
|
+
})(ServerEnvironment || (ServerEnvironment = {}));
|
|
18
|
+
exports.ServerEnvironment = ServerEnvironment;
|
|
19
|
+
let Application = Application_1 = class Application {
|
|
20
|
+
constructor() { }
|
|
21
|
+
/* Add any service that you want to be initialized before the server starts */
|
|
22
|
+
configureServices() { }
|
|
23
|
+
/* Add any service that you want to execute after the server starts */
|
|
24
|
+
postServerInitialization() { }
|
|
25
|
+
/* Add any service that you want to execute after server is shutdown */
|
|
26
|
+
serverShutdown() {
|
|
27
|
+
process_1.default.exit(0);
|
|
28
|
+
}
|
|
29
|
+
create(container, middlewares = []) {
|
|
30
|
+
this.configureServices();
|
|
31
|
+
const expressServer = new inversify_express_utils_1.InversifyExpressServer(container);
|
|
32
|
+
expressServer.setConfig((app) => {
|
|
33
|
+
/* Default body parser application/json */
|
|
34
|
+
app.use(express_1.default.json());
|
|
35
|
+
/* Default body parser application/x-www-form-urlencoded */
|
|
36
|
+
app.use(express_1.default.urlencoded({ extended: true }));
|
|
37
|
+
middlewares.forEach(middleware => {
|
|
38
|
+
app.use(middleware);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
this.app = expressServer.build();
|
|
42
|
+
/* Add the error handler middleware */
|
|
43
|
+
this.app.use(error_handler_middleware_1.default);
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
listen(port, environment, consoleMessage) {
|
|
47
|
+
this.port = port;
|
|
48
|
+
this.environment = environment;
|
|
49
|
+
this.app.listen(this.port, () => {
|
|
50
|
+
new console_1.Console().messageServer(this.port, this.environment, consoleMessage);
|
|
51
|
+
process_1.default.on("SIGINT", this.serverShutdown.bind(this));
|
|
52
|
+
});
|
|
53
|
+
this.postServerInitialization();
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
Application = Application_1 = tslib_1.__decorate([
|
|
57
|
+
(0, inversify_binding_decorators_1.provide)(Application_1),
|
|
58
|
+
tslib_1.__metadata("design:paramtypes", [])
|
|
59
|
+
], Application);
|
|
60
|
+
exports.Application = Application;
|
|
61
|
+
const appServerInstance = new Application();
|
|
62
|
+
exports.AppInstance = appServerInstance;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AppContainer = exports.ServerEnvironment = exports.Application = exports.AppInstance = void 0;
|
|
4
|
+
var application_1 = require("./application");
|
|
5
|
+
Object.defineProperty(exports, "AppInstance", { enumerable: true, get: function () { return application_1.AppInstance; } });
|
|
6
|
+
Object.defineProperty(exports, "Application", { enumerable: true, get: function () { return application_1.Application; } });
|
|
7
|
+
Object.defineProperty(exports, "ServerEnvironment", { enumerable: true, get: function () { return application_1.ServerEnvironment; } });
|
|
8
|
+
var app_container_1 = require("./app-container");
|
|
9
|
+
Object.defineProperty(exports, "AppContainer", { enumerable: true, get: function () { return app_container_1.AppContainer; } });
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
interface IApplicationMessageToConsole {
|
|
2
|
+
appName: string;
|
|
3
|
+
appVersion: string;
|
|
4
|
+
}
|
|
5
|
+
declare class Console {
|
|
6
|
+
private printColor;
|
|
7
|
+
messageServer(port: any, environment: string, consoleMessage?: IApplicationMessageToConsole): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
export { Console, IApplicationMessageToConsole };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var Console_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Console = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const chalk_1 = tslib_1.__importDefault(require("chalk"));
|
|
7
|
+
const inversify_binding_decorators_1 = require("inversify-binding-decorators");
|
|
8
|
+
var ColorStyle;
|
|
9
|
+
(function (ColorStyle) {
|
|
10
|
+
ColorStyle[ColorStyle["None"] = 0] = "None";
|
|
11
|
+
ColorStyle[ColorStyle["Yellow"] = 1] = "Yellow";
|
|
12
|
+
ColorStyle[ColorStyle["Blue"] = 2] = "Blue";
|
|
13
|
+
ColorStyle[ColorStyle["Green"] = 3] = "Green";
|
|
14
|
+
ColorStyle[ColorStyle["Red"] = 4] = "Red";
|
|
15
|
+
})(ColorStyle || (ColorStyle = {}));
|
|
16
|
+
let Console = Console_1 = class Console {
|
|
17
|
+
async printColor(message, colorStyle) {
|
|
18
|
+
switch (colorStyle) {
|
|
19
|
+
case ColorStyle.Yellow:
|
|
20
|
+
return console.log(chalk_1.default.bgYellow.black(message));
|
|
21
|
+
case ColorStyle.Blue:
|
|
22
|
+
return console.log(chalk_1.default.bgBlue.black(message));
|
|
23
|
+
case ColorStyle.Green:
|
|
24
|
+
return console.log(chalk_1.default.bgGreen.black(message));
|
|
25
|
+
case ColorStyle.Red:
|
|
26
|
+
return console.log(chalk_1.default.bgRed.black(message));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async messageServer(port, environment, consoleMessage) {
|
|
30
|
+
const appConsoleMessage = {
|
|
31
|
+
appName: (consoleMessage === null || consoleMessage === void 0 ? void 0 : consoleMessage.appName) || "Application",
|
|
32
|
+
appVersion: (consoleMessage === null || consoleMessage === void 0 ? void 0 : consoleMessage.appVersion) || "not provided",
|
|
33
|
+
};
|
|
34
|
+
let terminalColor = ColorStyle.None;
|
|
35
|
+
switch (environment.toLowerCase()) {
|
|
36
|
+
case "development":
|
|
37
|
+
terminalColor = ColorStyle.Yellow;
|
|
38
|
+
break;
|
|
39
|
+
case "staging":
|
|
40
|
+
terminalColor = ColorStyle.Blue;
|
|
41
|
+
break;
|
|
42
|
+
case "production":
|
|
43
|
+
terminalColor = ColorStyle.Green;
|
|
44
|
+
break;
|
|
45
|
+
default:
|
|
46
|
+
terminalColor = ColorStyle.Red;
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
this.printColor(`${appConsoleMessage.appName} version ${appConsoleMessage.appVersion} is running on ` +
|
|
50
|
+
`port ${port} - Environment: ${environment}`, terminalColor);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
Console = Console_1 = tslib_1.__decorate([
|
|
54
|
+
(0, inversify_binding_decorators_1.provide)(Console_1)
|
|
55
|
+
], Console);
|
|
56
|
+
exports.Console = Console;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Console, IApplicationMessageToConsole } from "./console";
|
package/console/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ContainerModule } from "inversify";
|
|
2
|
+
declare class BaseModule {
|
|
3
|
+
constructor();
|
|
4
|
+
private static createSymbols;
|
|
5
|
+
static createContainerModule(controllers: any[]): ContainerModule;
|
|
6
|
+
}
|
|
7
|
+
declare const CreateModule: typeof BaseModule.createContainerModule;
|
|
8
|
+
export { CreateModule };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var BaseModule_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.CreateModule = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const inversify_1 = require("inversify");
|
|
7
|
+
const inversify_binding_decorators_1 = require("inversify-binding-decorators");
|
|
8
|
+
let BaseModule = BaseModule_1 = class BaseModule {
|
|
9
|
+
constructor() { }
|
|
10
|
+
static createSymbols(controllers) {
|
|
11
|
+
const symbols = new Map();
|
|
12
|
+
for (const controller of controllers) {
|
|
13
|
+
const target = controller;
|
|
14
|
+
const symbol = Symbol.for(target.name);
|
|
15
|
+
symbols.set(symbol, target);
|
|
16
|
+
}
|
|
17
|
+
return symbols;
|
|
18
|
+
}
|
|
19
|
+
static createContainerModule(controllers) {
|
|
20
|
+
const symbols = BaseModule_1.createSymbols(controllers);
|
|
21
|
+
return new inversify_1.ContainerModule(bind => {
|
|
22
|
+
for (const symbol of symbols) {
|
|
23
|
+
const target = symbol.valueOf();
|
|
24
|
+
bind(target[0]).to(target[1]);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
BaseModule = BaseModule_1 = tslib_1.__decorate([
|
|
30
|
+
(0, inversify_binding_decorators_1.provide)(BaseModule_1),
|
|
31
|
+
tslib_1.__metadata("design:paramtypes", [])
|
|
32
|
+
], BaseModule);
|
|
33
|
+
const CreateModule = BaseModule.createContainerModule;
|
|
34
|
+
exports.CreateModule = CreateModule;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CreateModule } from "./container-module";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CreateModule = void 0;
|
|
4
|
+
var container_module_1 = require("./container-module");
|
|
5
|
+
Object.defineProperty(exports, "CreateModule", { enumerable: true, get: function () { return container_module_1.CreateModule; } });
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { interfaces } from 'inversify-express-utils';
|
|
2
|
+
declare abstract class BaseController implements interfaces.Controller {
|
|
3
|
+
private serviceName;
|
|
4
|
+
constructor(serviceName: string);
|
|
5
|
+
protected callUseCaseAsync(useCase: Promise<any>, res: any, successStatusCode: number): Promise<any>;
|
|
6
|
+
protected callUseCase(useCase: any, res: any, successStatusCode: number): any;
|
|
7
|
+
}
|
|
8
|
+
export { BaseController };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var BaseController_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.BaseController = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const inversify_binding_decorators_1 = require("inversify-binding-decorators");
|
|
7
|
+
const error_1 = require("../error");
|
|
8
|
+
let BaseController = BaseController_1 = class BaseController {
|
|
9
|
+
constructor(serviceName) {
|
|
10
|
+
this.serviceName = serviceName;
|
|
11
|
+
}
|
|
12
|
+
async callUseCaseAsync(useCase, res, successStatusCode) {
|
|
13
|
+
let dataReturn;
|
|
14
|
+
try {
|
|
15
|
+
dataReturn = await useCase;
|
|
16
|
+
return res.status(successStatusCode).json(dataReturn);
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
error_1.Report.Error(error);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
callUseCase(useCase, res, successStatusCode) {
|
|
23
|
+
let dataReturn;
|
|
24
|
+
try {
|
|
25
|
+
dataReturn = useCase;
|
|
26
|
+
return res.status(successStatusCode).json(dataReturn);
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
error_1.Report.Error(error);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
BaseController = BaseController_1 = tslib_1.__decorate([
|
|
34
|
+
(0, inversify_binding_decorators_1.provide)(BaseController_1),
|
|
35
|
+
tslib_1.__metadata("design:paramtypes", [String])
|
|
36
|
+
], BaseController);
|
|
37
|
+
exports.BaseController = BaseController;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { BaseController } from './base-controller';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseController = void 0;
|
|
4
|
+
var base_controller_1 = require("./base-controller");
|
|
5
|
+
Object.defineProperty(exports, "BaseController", { enumerable: true, get: function () { return base_controller_1.BaseController; } });
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
declare class EnvValidatorProvider {
|
|
2
|
+
static get(key: string, defaultValue?: any): any;
|
|
3
|
+
static checkAll(): void;
|
|
4
|
+
}
|
|
5
|
+
declare global {
|
|
6
|
+
interface String {
|
|
7
|
+
AsBoolean(): boolean | undefined;
|
|
8
|
+
AsNumber(): number | undefined;
|
|
9
|
+
AsString(): string | undefined;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export { EnvValidatorProvider as Environments };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var EnvValidatorProvider_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Environments = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
7
|
+
const path_1 = tslib_1.__importDefault(require("path"));
|
|
8
|
+
const dotenv_1 = tslib_1.__importDefault(require("dotenv"));
|
|
9
|
+
const inversify_binding_decorators_1 = require("inversify-binding-decorators");
|
|
10
|
+
const logger_1 = require("../logger");
|
|
11
|
+
let EnvValidatorProvider = EnvValidatorProvider_1 = class EnvValidatorProvider {
|
|
12
|
+
static get(key, defaultValue = undefined) {
|
|
13
|
+
var _a;
|
|
14
|
+
return (_a = process.env[key]) !== null && _a !== void 0 ? _a : defaultValue;
|
|
15
|
+
}
|
|
16
|
+
static checkAll() {
|
|
17
|
+
/* Load .env file */
|
|
18
|
+
dotenv_1.default.config();
|
|
19
|
+
/* Verify if .env file exists */
|
|
20
|
+
const envFilePath = path_1.default.join(process.cwd(), ".", ".env");
|
|
21
|
+
if (!fs_1.default.existsSync(envFilePath)) {
|
|
22
|
+
(0, logger_1.log)(logger_1.LogLevel.Info, "Environment file .env is not defined.", "env-validator-provider");
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
const regexIgnoreDefaultEnvKeys = /^npm_config_/;
|
|
26
|
+
let hasError = false;
|
|
27
|
+
for (const key in process.env) {
|
|
28
|
+
if (regexIgnoreDefaultEnvKeys.test(key)) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
if (!process.env[key] || process.env[key] === "") {
|
|
32
|
+
(0, logger_1.log)(logger_1.LogLevel.Info, `Environment variable ${key} is not defined.`, "env-validator-provider");
|
|
33
|
+
hasError = true;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (hasError) {
|
|
37
|
+
process.exit(1);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
EnvValidatorProvider = EnvValidatorProvider_1 = tslib_1.__decorate([
|
|
42
|
+
(0, inversify_binding_decorators_1.provide)(EnvValidatorProvider_1)
|
|
43
|
+
], EnvValidatorProvider);
|
|
44
|
+
exports.Environments = EnvValidatorProvider;
|
|
45
|
+
String.prototype.AsBoolean = function () {
|
|
46
|
+
switch (this.toLowerCase().trim()) {
|
|
47
|
+
case "true":
|
|
48
|
+
case "1":
|
|
49
|
+
case "yes":
|
|
50
|
+
return true;
|
|
51
|
+
case "false":
|
|
52
|
+
case "0":
|
|
53
|
+
case "no":
|
|
54
|
+
return false;
|
|
55
|
+
default:
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
String.prototype.AsNumber = function () {
|
|
60
|
+
return Number(this);
|
|
61
|
+
};
|
|
62
|
+
String.prototype.AsString = function () {
|
|
63
|
+
return String(this);
|
|
64
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { Environments } from './env-validator';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Environments = void 0;
|
|
4
|
+
var env_validator_1 = require("./env-validator");
|
|
5
|
+
Object.defineProperty(exports, "Environments", { enumerable: true, get: function () { return env_validator_1.Environments; } });
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var AppError_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.AppError = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const inversify_binding_decorators_1 = require("inversify-binding-decorators");
|
|
7
|
+
let AppError = AppError_1 = class AppError extends Error {
|
|
8
|
+
constructor(statusCode, message, service) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.statusCode = statusCode;
|
|
11
|
+
this.service = service;
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
AppError = AppError_1 = tslib_1.__decorate([
|
|
15
|
+
(0, inversify_binding_decorators_1.provide)(AppError_1),
|
|
16
|
+
tslib_1.__metadata("design:paramtypes", [Number, String, String])
|
|
17
|
+
], AppError);
|
|
18
|
+
exports.AppError = AppError;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const status_code_1 = require("./status-code");
|
|
4
|
+
const logger_1 = require("../logger");
|
|
5
|
+
function errorHandler(error, req, res, next) {
|
|
6
|
+
(0, logger_1.log)(logger_1.LogLevel.Error, error, error.service || "service-undefined");
|
|
7
|
+
res.status(error.statusCode || status_code_1.StatusCode.InternalServerError).json({ statusCode: error.statusCode, error: error.message });
|
|
8
|
+
}
|
|
9
|
+
exports.default = errorHandler;
|
package/error/index.d.ts
ADDED
package/error/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Report = exports.AppError = exports.StatusCode = void 0;
|
|
4
|
+
var status_code_1 = require("./status-code");
|
|
5
|
+
Object.defineProperty(exports, "StatusCode", { enumerable: true, get: function () { return status_code_1.StatusCode; } });
|
|
6
|
+
var application_error_1 = require("./application-error");
|
|
7
|
+
Object.defineProperty(exports, "AppError", { enumerable: true, get: function () { return application_error_1.AppError; } });
|
|
8
|
+
var report_1 = require("./report");
|
|
9
|
+
Object.defineProperty(exports, "Report", { enumerable: true, get: function () { return report_1.Report; } });
|
package/error/report.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var Report_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Report = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const inversify_binding_decorators_1 = require("inversify-binding-decorators");
|
|
7
|
+
let Report = Report_1 = class Report {
|
|
8
|
+
static Error(error) {
|
|
9
|
+
throw error;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
Report = Report_1 = tslib_1.__decorate([
|
|
13
|
+
(0, inversify_binding_decorators_1.provide)(Report_1)
|
|
14
|
+
], Report);
|
|
15
|
+
exports.Report = Report;
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
declare enum InformationResponse {
|
|
2
|
+
Continue = 100,
|
|
3
|
+
SwitchingProtocols = 101,
|
|
4
|
+
Processing = 102,
|
|
5
|
+
eEarlyHints = 103
|
|
6
|
+
}
|
|
7
|
+
declare enum SuccessfulResponse {
|
|
8
|
+
OK = 200,
|
|
9
|
+
Created = 201,
|
|
10
|
+
Accepted = 202,
|
|
11
|
+
NonAuthoritativeInformation = 203,
|
|
12
|
+
NoContent = 204,
|
|
13
|
+
ResetContent = 205,
|
|
14
|
+
PartialContent = 206,
|
|
15
|
+
MultiStatus = 207,
|
|
16
|
+
AlreadyReported = 208,
|
|
17
|
+
IMUsed = 226
|
|
18
|
+
}
|
|
19
|
+
declare enum RedirectionMessage {
|
|
20
|
+
MultipleChoices = 300,
|
|
21
|
+
MovedPermanently = 301,
|
|
22
|
+
Found = 302,
|
|
23
|
+
SeeOther = 303,
|
|
24
|
+
NotModified = 304,
|
|
25
|
+
TemporaryRedirect = 307,
|
|
26
|
+
PermanentRedirect = 308
|
|
27
|
+
}
|
|
28
|
+
declare enum ClientErrorResponse {
|
|
29
|
+
BadRequest = 400,
|
|
30
|
+
Unauthorized = 401,
|
|
31
|
+
PaymentRequired = 402,
|
|
32
|
+
Forbidden = 403,
|
|
33
|
+
NotFound = 404,
|
|
34
|
+
MethodNotAllowed = 405,
|
|
35
|
+
NotAcceptable = 406,
|
|
36
|
+
ProxyAuthenticationRequired = 407,
|
|
37
|
+
RequestTimeout = 408,
|
|
38
|
+
Conflict = 409,
|
|
39
|
+
Gone = 410,
|
|
40
|
+
LengthRequired = 411,
|
|
41
|
+
PreconditionFailed = 412,
|
|
42
|
+
PayloadTooLarge = 413,
|
|
43
|
+
URITooLong = 414,
|
|
44
|
+
UnsupportedMediaType = 415,
|
|
45
|
+
RangeNotSatisfiable = 416,
|
|
46
|
+
ExpectationFailed = 417,
|
|
47
|
+
ImATeapot = 418,
|
|
48
|
+
MisdirectedRequest = 421,
|
|
49
|
+
UnprocessableEntity = 422,
|
|
50
|
+
Locked = 423,
|
|
51
|
+
FailedDependency = 424,
|
|
52
|
+
TooEarly = 425,
|
|
53
|
+
UpgradeRequired = 426,
|
|
54
|
+
PreconditionRequired = 428,
|
|
55
|
+
TooManyRequests = 429,
|
|
56
|
+
RequestHeaderFieldsTooLarge = 431,
|
|
57
|
+
UnavailableForLegalReasons = 451
|
|
58
|
+
}
|
|
59
|
+
declare enum ServerErrorResponse {
|
|
60
|
+
InternalServerError = 500,
|
|
61
|
+
NotImplemented = 501,
|
|
62
|
+
BadGateway = 502,
|
|
63
|
+
ServiceUnavailable = 503,
|
|
64
|
+
GatewayTimeout = 504,
|
|
65
|
+
HTTPVersionNotSupported = 505,
|
|
66
|
+
VariantAlsoNegotiates = 506,
|
|
67
|
+
InsufficientStorage = 507,
|
|
68
|
+
LoopDetected = 508,
|
|
69
|
+
NotExtended = 510,
|
|
70
|
+
NetworkAuthenticationRequired = 511
|
|
71
|
+
}
|
|
72
|
+
declare const StatusCode: {
|
|
73
|
+
[x: number]: string;
|
|
74
|
+
InternalServerError: ServerErrorResponse.InternalServerError;
|
|
75
|
+
NotImplemented: ServerErrorResponse.NotImplemented;
|
|
76
|
+
BadGateway: ServerErrorResponse.BadGateway;
|
|
77
|
+
ServiceUnavailable: ServerErrorResponse.ServiceUnavailable;
|
|
78
|
+
GatewayTimeout: ServerErrorResponse.GatewayTimeout;
|
|
79
|
+
HTTPVersionNotSupported: ServerErrorResponse.HTTPVersionNotSupported;
|
|
80
|
+
VariantAlsoNegotiates: ServerErrorResponse.VariantAlsoNegotiates;
|
|
81
|
+
InsufficientStorage: ServerErrorResponse.InsufficientStorage;
|
|
82
|
+
LoopDetected: ServerErrorResponse.LoopDetected;
|
|
83
|
+
NotExtended: ServerErrorResponse.NotExtended;
|
|
84
|
+
NetworkAuthenticationRequired: ServerErrorResponse.NetworkAuthenticationRequired;
|
|
85
|
+
BadRequest: ClientErrorResponse.BadRequest;
|
|
86
|
+
Unauthorized: ClientErrorResponse.Unauthorized;
|
|
87
|
+
PaymentRequired: ClientErrorResponse.PaymentRequired;
|
|
88
|
+
Forbidden: ClientErrorResponse.Forbidden;
|
|
89
|
+
NotFound: ClientErrorResponse.NotFound;
|
|
90
|
+
MethodNotAllowed: ClientErrorResponse.MethodNotAllowed;
|
|
91
|
+
NotAcceptable: ClientErrorResponse.NotAcceptable;
|
|
92
|
+
ProxyAuthenticationRequired: ClientErrorResponse.ProxyAuthenticationRequired;
|
|
93
|
+
RequestTimeout: ClientErrorResponse.RequestTimeout;
|
|
94
|
+
Conflict: ClientErrorResponse.Conflict;
|
|
95
|
+
Gone: ClientErrorResponse.Gone;
|
|
96
|
+
LengthRequired: ClientErrorResponse.LengthRequired;
|
|
97
|
+
PreconditionFailed: ClientErrorResponse.PreconditionFailed;
|
|
98
|
+
PayloadTooLarge: ClientErrorResponse.PayloadTooLarge;
|
|
99
|
+
URITooLong: ClientErrorResponse.URITooLong;
|
|
100
|
+
UnsupportedMediaType: ClientErrorResponse.UnsupportedMediaType;
|
|
101
|
+
RangeNotSatisfiable: ClientErrorResponse.RangeNotSatisfiable;
|
|
102
|
+
ExpectationFailed: ClientErrorResponse.ExpectationFailed;
|
|
103
|
+
ImATeapot: ClientErrorResponse.ImATeapot;
|
|
104
|
+
MisdirectedRequest: ClientErrorResponse.MisdirectedRequest;
|
|
105
|
+
UnprocessableEntity: ClientErrorResponse.UnprocessableEntity;
|
|
106
|
+
Locked: ClientErrorResponse.Locked;
|
|
107
|
+
FailedDependency: ClientErrorResponse.FailedDependency;
|
|
108
|
+
TooEarly: ClientErrorResponse.TooEarly;
|
|
109
|
+
UpgradeRequired: ClientErrorResponse.UpgradeRequired;
|
|
110
|
+
PreconditionRequired: ClientErrorResponse.PreconditionRequired;
|
|
111
|
+
TooManyRequests: ClientErrorResponse.TooManyRequests;
|
|
112
|
+
RequestHeaderFieldsTooLarge: ClientErrorResponse.RequestHeaderFieldsTooLarge;
|
|
113
|
+
UnavailableForLegalReasons: ClientErrorResponse.UnavailableForLegalReasons;
|
|
114
|
+
MultipleChoices: RedirectionMessage.MultipleChoices;
|
|
115
|
+
MovedPermanently: RedirectionMessage.MovedPermanently;
|
|
116
|
+
Found: RedirectionMessage.Found;
|
|
117
|
+
SeeOther: RedirectionMessage.SeeOther;
|
|
118
|
+
NotModified: RedirectionMessage.NotModified;
|
|
119
|
+
TemporaryRedirect: RedirectionMessage.TemporaryRedirect;
|
|
120
|
+
PermanentRedirect: RedirectionMessage.PermanentRedirect;
|
|
121
|
+
OK: SuccessfulResponse.OK;
|
|
122
|
+
Created: SuccessfulResponse.Created;
|
|
123
|
+
Accepted: SuccessfulResponse.Accepted;
|
|
124
|
+
NonAuthoritativeInformation: SuccessfulResponse.NonAuthoritativeInformation;
|
|
125
|
+
NoContent: SuccessfulResponse.NoContent;
|
|
126
|
+
ResetContent: SuccessfulResponse.ResetContent;
|
|
127
|
+
PartialContent: SuccessfulResponse.PartialContent;
|
|
128
|
+
MultiStatus: SuccessfulResponse.MultiStatus;
|
|
129
|
+
AlreadyReported: SuccessfulResponse.AlreadyReported;
|
|
130
|
+
IMUsed: SuccessfulResponse.IMUsed;
|
|
131
|
+
Continue: InformationResponse.Continue;
|
|
132
|
+
SwitchingProtocols: InformationResponse.SwitchingProtocols;
|
|
133
|
+
Processing: InformationResponse.Processing;
|
|
134
|
+
eEarlyHints: InformationResponse.eEarlyHints;
|
|
135
|
+
};
|
|
136
|
+
export { StatusCode };
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StatusCode = void 0;
|
|
4
|
+
/* Http Error Code Response */
|
|
5
|
+
var InformationResponse;
|
|
6
|
+
(function (InformationResponse) {
|
|
7
|
+
InformationResponse[InformationResponse["Continue"] = 100] = "Continue";
|
|
8
|
+
InformationResponse[InformationResponse["SwitchingProtocols"] = 101] = "SwitchingProtocols";
|
|
9
|
+
InformationResponse[InformationResponse["Processing"] = 102] = "Processing";
|
|
10
|
+
InformationResponse[InformationResponse["eEarlyHints"] = 103] = "eEarlyHints";
|
|
11
|
+
})(InformationResponse || (InformationResponse = {}));
|
|
12
|
+
var SuccessfulResponse;
|
|
13
|
+
(function (SuccessfulResponse) {
|
|
14
|
+
SuccessfulResponse[SuccessfulResponse["OK"] = 200] = "OK";
|
|
15
|
+
SuccessfulResponse[SuccessfulResponse["Created"] = 201] = "Created";
|
|
16
|
+
SuccessfulResponse[SuccessfulResponse["Accepted"] = 202] = "Accepted";
|
|
17
|
+
SuccessfulResponse[SuccessfulResponse["NonAuthoritativeInformation"] = 203] = "NonAuthoritativeInformation";
|
|
18
|
+
SuccessfulResponse[SuccessfulResponse["NoContent"] = 204] = "NoContent";
|
|
19
|
+
SuccessfulResponse[SuccessfulResponse["ResetContent"] = 205] = "ResetContent";
|
|
20
|
+
SuccessfulResponse[SuccessfulResponse["PartialContent"] = 206] = "PartialContent";
|
|
21
|
+
SuccessfulResponse[SuccessfulResponse["MultiStatus"] = 207] = "MultiStatus";
|
|
22
|
+
SuccessfulResponse[SuccessfulResponse["AlreadyReported"] = 208] = "AlreadyReported";
|
|
23
|
+
SuccessfulResponse[SuccessfulResponse["IMUsed"] = 226] = "IMUsed";
|
|
24
|
+
})(SuccessfulResponse || (SuccessfulResponse = {}));
|
|
25
|
+
var RedirectionMessage;
|
|
26
|
+
(function (RedirectionMessage) {
|
|
27
|
+
RedirectionMessage[RedirectionMessage["MultipleChoices"] = 300] = "MultipleChoices";
|
|
28
|
+
RedirectionMessage[RedirectionMessage["MovedPermanently"] = 301] = "MovedPermanently";
|
|
29
|
+
RedirectionMessage[RedirectionMessage["Found"] = 302] = "Found";
|
|
30
|
+
RedirectionMessage[RedirectionMessage["SeeOther"] = 303] = "SeeOther";
|
|
31
|
+
RedirectionMessage[RedirectionMessage["NotModified"] = 304] = "NotModified";
|
|
32
|
+
RedirectionMessage[RedirectionMessage["TemporaryRedirect"] = 307] = "TemporaryRedirect";
|
|
33
|
+
RedirectionMessage[RedirectionMessage["PermanentRedirect"] = 308] = "PermanentRedirect";
|
|
34
|
+
})(RedirectionMessage || (RedirectionMessage = {}));
|
|
35
|
+
var ClientErrorResponse;
|
|
36
|
+
(function (ClientErrorResponse) {
|
|
37
|
+
ClientErrorResponse[ClientErrorResponse["BadRequest"] = 400] = "BadRequest";
|
|
38
|
+
ClientErrorResponse[ClientErrorResponse["Unauthorized"] = 401] = "Unauthorized";
|
|
39
|
+
ClientErrorResponse[ClientErrorResponse["PaymentRequired"] = 402] = "PaymentRequired";
|
|
40
|
+
ClientErrorResponse[ClientErrorResponse["Forbidden"] = 403] = "Forbidden";
|
|
41
|
+
ClientErrorResponse[ClientErrorResponse["NotFound"] = 404] = "NotFound";
|
|
42
|
+
ClientErrorResponse[ClientErrorResponse["MethodNotAllowed"] = 405] = "MethodNotAllowed";
|
|
43
|
+
ClientErrorResponse[ClientErrorResponse["NotAcceptable"] = 406] = "NotAcceptable";
|
|
44
|
+
ClientErrorResponse[ClientErrorResponse["ProxyAuthenticationRequired"] = 407] = "ProxyAuthenticationRequired";
|
|
45
|
+
ClientErrorResponse[ClientErrorResponse["RequestTimeout"] = 408] = "RequestTimeout";
|
|
46
|
+
ClientErrorResponse[ClientErrorResponse["Conflict"] = 409] = "Conflict";
|
|
47
|
+
ClientErrorResponse[ClientErrorResponse["Gone"] = 410] = "Gone";
|
|
48
|
+
ClientErrorResponse[ClientErrorResponse["LengthRequired"] = 411] = "LengthRequired";
|
|
49
|
+
ClientErrorResponse[ClientErrorResponse["PreconditionFailed"] = 412] = "PreconditionFailed";
|
|
50
|
+
ClientErrorResponse[ClientErrorResponse["PayloadTooLarge"] = 413] = "PayloadTooLarge";
|
|
51
|
+
ClientErrorResponse[ClientErrorResponse["URITooLong"] = 414] = "URITooLong";
|
|
52
|
+
ClientErrorResponse[ClientErrorResponse["UnsupportedMediaType"] = 415] = "UnsupportedMediaType";
|
|
53
|
+
ClientErrorResponse[ClientErrorResponse["RangeNotSatisfiable"] = 416] = "RangeNotSatisfiable";
|
|
54
|
+
ClientErrorResponse[ClientErrorResponse["ExpectationFailed"] = 417] = "ExpectationFailed";
|
|
55
|
+
ClientErrorResponse[ClientErrorResponse["ImATeapot"] = 418] = "ImATeapot";
|
|
56
|
+
ClientErrorResponse[ClientErrorResponse["MisdirectedRequest"] = 421] = "MisdirectedRequest";
|
|
57
|
+
ClientErrorResponse[ClientErrorResponse["UnprocessableEntity"] = 422] = "UnprocessableEntity";
|
|
58
|
+
ClientErrorResponse[ClientErrorResponse["Locked"] = 423] = "Locked";
|
|
59
|
+
ClientErrorResponse[ClientErrorResponse["FailedDependency"] = 424] = "FailedDependency";
|
|
60
|
+
ClientErrorResponse[ClientErrorResponse["TooEarly"] = 425] = "TooEarly";
|
|
61
|
+
ClientErrorResponse[ClientErrorResponse["UpgradeRequired"] = 426] = "UpgradeRequired";
|
|
62
|
+
ClientErrorResponse[ClientErrorResponse["PreconditionRequired"] = 428] = "PreconditionRequired";
|
|
63
|
+
ClientErrorResponse[ClientErrorResponse["TooManyRequests"] = 429] = "TooManyRequests";
|
|
64
|
+
ClientErrorResponse[ClientErrorResponse["RequestHeaderFieldsTooLarge"] = 431] = "RequestHeaderFieldsTooLarge";
|
|
65
|
+
ClientErrorResponse[ClientErrorResponse["UnavailableForLegalReasons"] = 451] = "UnavailableForLegalReasons";
|
|
66
|
+
})(ClientErrorResponse || (ClientErrorResponse = {}));
|
|
67
|
+
var ServerErrorResponse;
|
|
68
|
+
(function (ServerErrorResponse) {
|
|
69
|
+
ServerErrorResponse[ServerErrorResponse["InternalServerError"] = 500] = "InternalServerError";
|
|
70
|
+
ServerErrorResponse[ServerErrorResponse["NotImplemented"] = 501] = "NotImplemented";
|
|
71
|
+
ServerErrorResponse[ServerErrorResponse["BadGateway"] = 502] = "BadGateway";
|
|
72
|
+
ServerErrorResponse[ServerErrorResponse["ServiceUnavailable"] = 503] = "ServiceUnavailable";
|
|
73
|
+
ServerErrorResponse[ServerErrorResponse["GatewayTimeout"] = 504] = "GatewayTimeout";
|
|
74
|
+
ServerErrorResponse[ServerErrorResponse["HTTPVersionNotSupported"] = 505] = "HTTPVersionNotSupported";
|
|
75
|
+
ServerErrorResponse[ServerErrorResponse["VariantAlsoNegotiates"] = 506] = "VariantAlsoNegotiates";
|
|
76
|
+
ServerErrorResponse[ServerErrorResponse["InsufficientStorage"] = 507] = "InsufficientStorage";
|
|
77
|
+
ServerErrorResponse[ServerErrorResponse["LoopDetected"] = 508] = "LoopDetected";
|
|
78
|
+
ServerErrorResponse[ServerErrorResponse["NotExtended"] = 510] = "NotExtended";
|
|
79
|
+
ServerErrorResponse[ServerErrorResponse["NetworkAuthenticationRequired"] = 511] = "NetworkAuthenticationRequired";
|
|
80
|
+
})(ServerErrorResponse || (ServerErrorResponse = {}));
|
|
81
|
+
const HttpStatusErrorCode = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, InformationResponse), SuccessfulResponse), RedirectionMessage), ClientErrorResponse), ServerErrorResponse);
|
|
82
|
+
const StatusCode = Object.assign({}, HttpStatusErrorCode);
|
|
83
|
+
exports.StatusCode = StatusCode;
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./application"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./console"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./container-module"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./controller"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./environment"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./error"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./logger"), exports);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
declare enum LogLevel {
|
|
2
|
+
Debug = 0,
|
|
3
|
+
Error = 1,
|
|
4
|
+
Info = 2
|
|
5
|
+
}
|
|
6
|
+
declare class GeneralLogger {
|
|
7
|
+
private logger;
|
|
8
|
+
constructor();
|
|
9
|
+
private createConsoleTransport;
|
|
10
|
+
private createRotationalFileTransport;
|
|
11
|
+
private createLoggerOptions;
|
|
12
|
+
private getPathAndLine;
|
|
13
|
+
log(logLevel: LogLevel, content: Error | string, service?: string): void;
|
|
14
|
+
}
|
|
15
|
+
declare const log: any;
|
|
16
|
+
export { LogLevel, GeneralLogger, log };
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var GeneralLogger_1;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.log = exports.GeneralLogger = exports.LogLevel = void 0;
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const winston_1 = require("winston");
|
|
7
|
+
const winston_daily_rotate_file_1 = tslib_1.__importDefault(require("winston-daily-rotate-file"));
|
|
8
|
+
const inversify_binding_decorators_1 = require("inversify-binding-decorators");
|
|
9
|
+
var LogLevel;
|
|
10
|
+
(function (LogLevel) {
|
|
11
|
+
LogLevel[LogLevel["Debug"] = 0] = "Debug";
|
|
12
|
+
LogLevel[LogLevel["Error"] = 1] = "Error";
|
|
13
|
+
LogLevel[LogLevel["Info"] = 2] = "Info";
|
|
14
|
+
})(LogLevel || (LogLevel = {}));
|
|
15
|
+
exports.LogLevel = LogLevel;
|
|
16
|
+
let GeneralLogger = GeneralLogger_1 = class GeneralLogger {
|
|
17
|
+
constructor() {
|
|
18
|
+
this.logger = (0, winston_1.createLogger)(this.createLoggerOptions());
|
|
19
|
+
}
|
|
20
|
+
createConsoleTransport() {
|
|
21
|
+
const consoleTransport = new winston_1.transports.Console({
|
|
22
|
+
level: "debug",
|
|
23
|
+
handleExceptions: true,
|
|
24
|
+
handleRejections: true
|
|
25
|
+
});
|
|
26
|
+
return consoleTransport;
|
|
27
|
+
}
|
|
28
|
+
createRotationalFileTransport() {
|
|
29
|
+
const rotationalFileTransport = new winston_daily_rotate_file_1.default({
|
|
30
|
+
level: "error",
|
|
31
|
+
filename: "logs/general-%DATE%.log",
|
|
32
|
+
datePattern: "YYYY-MM-DD",
|
|
33
|
+
zippedArchive: true,
|
|
34
|
+
maxSize: "20m",
|
|
35
|
+
maxFiles: "7d",
|
|
36
|
+
silent: false
|
|
37
|
+
});
|
|
38
|
+
return rotationalFileTransport;
|
|
39
|
+
}
|
|
40
|
+
createLoggerOptions() {
|
|
41
|
+
const loggerOptions = {
|
|
42
|
+
transports: [
|
|
43
|
+
this.createConsoleTransport(),
|
|
44
|
+
this.createRotationalFileTransport()
|
|
45
|
+
],
|
|
46
|
+
defaultMeta: { service: "service-unknown" },
|
|
47
|
+
format: winston_1.format.combine(winston_1.format.splat(), winston_1.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), winston_1.format.label({ label: "core-api" }), winston_1.format.printf(({ timestamp, level, message, service, label }) => {
|
|
48
|
+
return `[${timestamp}] [${label}] [${service}] ${level}: ${message}`;
|
|
49
|
+
}))
|
|
50
|
+
};
|
|
51
|
+
return loggerOptions;
|
|
52
|
+
}
|
|
53
|
+
getPathAndLine(error) {
|
|
54
|
+
let pathLine = "";
|
|
55
|
+
if (error.stack) {
|
|
56
|
+
let callerLine = error.stack.split("\n")[1];
|
|
57
|
+
let index = callerLine.indexOf("at ");
|
|
58
|
+
pathLine = callerLine.substring(index + +2, callerLine.length);
|
|
59
|
+
}
|
|
60
|
+
return pathLine;
|
|
61
|
+
}
|
|
62
|
+
log(logLevel, content, service) {
|
|
63
|
+
let pathLine = "";
|
|
64
|
+
let logMessageFormat = "";
|
|
65
|
+
if (typeof content === "object") {
|
|
66
|
+
pathLine = this.getPathAndLine(content);
|
|
67
|
+
logMessageFormat = `${content.message} - (${content.name}) [file: %s]`;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
logMessageFormat = content;
|
|
71
|
+
}
|
|
72
|
+
switch (logLevel) {
|
|
73
|
+
case LogLevel.Debug:
|
|
74
|
+
console.log(logMessageFormat, pathLine, { service });
|
|
75
|
+
break;
|
|
76
|
+
case LogLevel.Error:
|
|
77
|
+
this.logger.error(logMessageFormat, pathLine, { service });
|
|
78
|
+
break;
|
|
79
|
+
case LogLevel.Info:
|
|
80
|
+
this.logger.info(content, { service });
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
GeneralLogger = GeneralLogger_1 = tslib_1.__decorate([
|
|
86
|
+
(0, inversify_binding_decorators_1.provide)(GeneralLogger_1),
|
|
87
|
+
tslib_1.__metadata("design:paramtypes", [])
|
|
88
|
+
], GeneralLogger);
|
|
89
|
+
exports.GeneralLogger = GeneralLogger;
|
|
90
|
+
const Log = new GeneralLogger();
|
|
91
|
+
const log = Log.log.bind(Log);
|
|
92
|
+
exports.log = log;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { GeneralLogger, LogLevel, log } from './general-logger';
|
package/logger/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.log = exports.LogLevel = exports.GeneralLogger = void 0;
|
|
4
|
+
var general_logger_1 = require("./general-logger");
|
|
5
|
+
Object.defineProperty(exports, "GeneralLogger", { enumerable: true, get: function () { return general_logger_1.GeneralLogger; } });
|
|
6
|
+
Object.defineProperty(exports, "LogLevel", { enumerable: true, get: function () { return general_logger_1.LogLevel; } });
|
|
7
|
+
Object.defineProperty(exports, "log", { enumerable: true, get: function () { return general_logger_1.log; } });
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@expressots/core",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Expressots - modern, fast, lightweight nodejs web framework (@core)",
|
|
5
|
+
"author": "Richard Zampieri",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"homepage": "https://expresso-ts.com",
|
|
10
|
+
"funding": {
|
|
11
|
+
"type": "",
|
|
12
|
+
"url": ""
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/expressots/expressots"
|
|
17
|
+
},
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=18.10.0"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsc",
|
|
26
|
+
"postbuild": "xcopy \"package.json\" \"dist\" /y && xcopy \"README.md\" \"dist\" /y",
|
|
27
|
+
"release": "npm run build && npm run postbuild",
|
|
28
|
+
"test": "jest"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [],
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"chalk": "^4.1.2",
|
|
33
|
+
"dotenv": "^16.0.3",
|
|
34
|
+
"express": "^4.18.2",
|
|
35
|
+
"inversify": "^6.0.1",
|
|
36
|
+
"inversify-binding-decorators": "^4.0.0",
|
|
37
|
+
"inversify-express-utils": "^6.3.2",
|
|
38
|
+
"reflect-metadata": "^0.1.13",
|
|
39
|
+
"typescript": "^4.9.5",
|
|
40
|
+
"winston": "^3.8.1",
|
|
41
|
+
"winston-daily-rotate-file": "^4.7.1"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/express": "^4.17.17",
|
|
45
|
+
"@types/jest": "^29.4.0",
|
|
46
|
+
"jest": "^29.4.2",
|
|
47
|
+
"ts-jest": "^29.0.5",
|
|
48
|
+
"ts-node-dev": "^2.0.0"
|
|
49
|
+
}
|
|
50
|
+
}
|