Skip to content

Commit b3591d8

Browse files
authored
Merge pull request #16 from expressots/241-refactor-appfactory
241 refactor appfactory
2 parents eb5e7f3 + 2e691d5 commit b3591d8

8 files changed

+173
-171
lines changed

package.json

+21-20
Original file line numberDiff line numberDiff line change
@@ -56,35 +56,36 @@
5656
"build": "npm run clean && npm run build:cjs && npm run copy",
5757
"build:cjs": "tsc -p tsconfig.cjs.json",
5858
"release": "release-it",
59+
"prepublish": "npm run build && npm pack",
5960
"test": "jest",
6061
"format": "prettier --write \"src/**/*.ts\" --cache",
6162
"lint": "eslint \"src/**/*.ts\"",
6263
"lint:fix": "eslint \"src/**/*.ts\" --fix"
6364
},
6465
"dependencies": {
65-
"dotenv": "^16.0.3",
66-
"inversify": "^6.0.1",
67-
"inversify-binding-decorators": "^4.0.0",
68-
"jest": "^29.5.0",
69-
"reflect-metadata": "^0.1.13"
66+
"dotenv": "16.4.5",
67+
"inversify": "6.0.2",
68+
"inversify-binding-decorators": "4.0.0",
69+
"jest": "29.5.0",
70+
"reflect-metadata": "0.2.2"
7071
},
7172
"devDependencies": {
7273
"@commitlint/cli": "^18.0.0",
73-
"@commitlint/config-conventional": "^17.7.0",
74-
"@expressots/core": "^2.2.0",
75-
"@release-it/conventional-changelog": "^7.0.1",
76-
"@types/express": "^4.17.17",
77-
"@types/jest": "^29.5.0",
78-
"@types/node": "^20.4.9",
79-
"@typescript-eslint/eslint-plugin": "^6.6.0",
80-
"@typescript-eslint/parser": "^6.6.0",
81-
"eslint": "^8.48.0",
82-
"eslint-config-prettier": "^9.0.0",
83-
"husky": "^8.0.3",
84-
"prettier": "^3.0.3",
85-
"release-it": "^16.1.5",
86-
"ts-jest": "^29.0.5",
87-
"typescript": "^5.2.2"
74+
"@commitlint/config-conventional": "17.7.0",
75+
"@expressots/core": "latest",
76+
"@release-it/conventional-changelog": "7.0.1",
77+
"@types/express": "4.17.21",
78+
"@types/jest": "29.5.0",
79+
"@types/node": "20.4.9",
80+
"@typescript-eslint/eslint-plugin": "6.6.0",
81+
"@typescript-eslint/parser": "6.6.0",
82+
"eslint": "8.48.0",
83+
"eslint-config-prettier": "9.0.0",
84+
"husky": "8.0.3",
85+
"prettier": "3.0.3",
86+
"release-it": "16.1.5",
87+
"ts-jest": "29.0.5",
88+
"typescript": "5.2.2"
8889
},
8990
"release-it": {
9091
"git": {

src/adapter-express/application-base.ts src/adapter-express/application-express.base.ts

+11-19
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,32 @@ import { provide } from "inversify-binding-decorators";
2121
* @abstract
2222
*/
2323
@provide(ApplicationBase)
24-
abstract class ApplicationBase {
24+
export abstract class ApplicationBase {
2525
/**
26-
* Method to configure services that should be initialized
27-
* before the server starts. It must be implemented by the
28-
* extending class to set up necessary services or configurations.
29-
* Can return a Promise for async configuration.
26+
* Implement this method to set up required services or configurations before
27+
* the server starts. This is essential for initializing dependencies or settings
28+
* necessary for server operation. Supports asynchronous setup with a Promise.
3029
*
3130
* @abstract
3231
* @returns {void | Promise<void>}
3332
*/
3433
protected abstract configureServices(): void | Promise<void>;
3534

3635
/**
37-
* Method to configure services or actions that should be executed
38-
* after the server starts. It allows the extending class to perform
39-
* any necessary operations once the server is up and running.
40-
* Can return a Promise for async execution.
36+
* Implement this method to execute actions or configurations after the server
37+
* has started. Use this for operations that need to run once the server is
38+
* operational. Supports asynchronous execution with a Promise.
4139
*
4240
* @abstract
4341
* @returns {void | Promise<void>}
4442
*/
4543
protected abstract postServerInitialization(): void | Promise<void>;
4644

4745
/**
48-
* Method to perform any necessary actions or cleanup after the server
49-
* is shutting down. This might include closing database connections,
50-
* stopping background tasks, or other cleanup activities. It provides
51-
* a clean exit point for the server.
52-
* Can return a Promise for async cleanup.
53-
*
54-
* @abstract
55-
* @returns {void | Promise<void>}
46+
* Implement this method to handle cleanup and final actions when the server
47+
* is shutting down. Ideal for closing resources, stopping tasks, or other
48+
* cleanup procedures to ensure a graceful server shutdown. Supports asynchronous
49+
* cleanup with a Promise.
5650
*/
5751
protected abstract serverShutdown(): void | Promise<void>;
5852
}
59-
60-
export { ApplicationBase };

src/adapter-express/application-express.interface.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { IApplicationMessageToConsole, RenderTemplateOptions } from "@expressots/core";
2-
import { ServerEnvironment } from "./application-express";
2+
import { ServerEnvironment } from "./application-express.types";
33

44
/**
5-
* Interface representing the Application class for Expressjs
6-
* @interface IApplicationExpress
5+
* Public Interface for the WebServer application.
76
*/
8-
interface IApplicationExpress {
7+
export interface IWebServerPublic {
98
/**
109
* Start listening on the given port and environment.
1110
* @param port - The port number to listen on.
@@ -16,7 +15,7 @@ interface IApplicationExpress {
1615
port: number,
1716
environment: ServerEnvironment,
1817
consoleMessage?: IApplicationMessageToConsole,
19-
): Promise<void> | void;
18+
): Promise<void>;
2019

2120
/**
2221
* Configures the application's view engine based on the provided configuration options.
@@ -31,5 +30,3 @@ interface IApplicationExpress {
3130
*/
3231
setEngine<T extends RenderTemplateOptions>(options: T): void;
3332
}
34-
35-
export { IApplicationExpress };
+45-113
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,45 @@
1-
import express, { Request, Response, NextFunction } from "express";
2-
import { Container } from "inversify";
3-
import { provide } from "inversify-binding-decorators";
4-
import process from "process";
51
import {
62
Console,
73
IApplicationMessageToConsole,
8-
IMiddleware,
9-
Middleware,
10-
Logger,
114
IHandlebars,
5+
Logger,
6+
Middleware,
127
RenderTemplateOptions,
138
} from "@expressots/core";
14-
import { IApplicationExpress } from "./application-express.interface";
9+
import express from "express";
10+
import { Container } from "inversify";
11+
import { provide } from "inversify-binding-decorators";
12+
import process from "process";
13+
import { ApplicationBase } from "./application-express.base";
1514
import { InversifyExpressServer } from "./express-utils/inversify-express-server";
16-
import { ApplicationBase } from "./application-base";
17-
18-
/**
19-
* ExpressHandler Type
20-
*
21-
* The ExpressHandler type is a union type that represents various types of Express middleware functions.
22-
* It can be one of the following types:
23-
* - express.ErrorRequestHandler: Handles errors in the middleware pipeline.
24-
* - express.RequestParamHandler: Handles parameters in the middleware pipeline.
25-
* - express.RequestHandler: General request handler.
26-
* - undefined: Represents the absence of a handler.
27-
*/
28-
type ExpressHandler =
29-
| express.ErrorRequestHandler
30-
| express.RequestParamHandler
31-
| express.RequestHandler
32-
| undefined;
33-
34-
/**
35-
* MiddlewareConfig Interface
36-
*
37-
* The MiddlewareConfig interface specifies the structure for middleware configuration objects.
38-
* - path: Optional. The route path for which the middleware is configured.
39-
* - middlewares: An array of ExpressHandler types that make up the middleware pipeline for the route specified by 'path'.
40-
*/
41-
type MiddlewareConfig = {
42-
path?: string;
43-
middlewares: Array<ExpressHandler>;
44-
};
45-
46-
/**
47-
* Expresso middleware interface.
48-
*/
49-
interface IExpressoMiddleware {
50-
//readonly name: string;
51-
use(req: Request, res: Response, next: NextFunction): Promise<void> | void;
52-
}
53-
54-
/**
55-
* Abstract class for creating custom Expresso middleware.
56-
* Custom middleware classes should extend this class and implement the use method.
57-
*
58-
*/
59-
abstract class ExpressoMiddleware implements IExpressoMiddleware {
60-
get name(): string {
61-
return this.constructor.name;
62-
}
63-
64-
abstract use(req: Request, res: Response, next: NextFunction): Promise<void> | void;
65-
}
66-
67-
/**
68-
* Enum representing possible server environments.
69-
*/
70-
enum ServerEnvironment {
71-
Development = "development",
72-
Production = "production",
73-
}
15+
import {
16+
ExpressHandler,
17+
ExpressoMiddleware,
18+
IWebServer,
19+
MiddlewareConfig,
20+
ServerEnvironment,
21+
} from "./application-express.types";
7422

7523
/**
76-
* The Application class provides a way to configure and manage an Express application.
77-
* @provide Application
24+
* The AppExpress class provides methods for configuring and running an Express application.
25+
* @class AppExpress
26+
* @implements {IWebServer} - Interface for the WebServer application implementation.
27+
* @extends {ApplicationBase} - Base class for the application implementation that provides lifecycle hooks.
28+
* @method configure - Configures the InversifyJS container.
29+
* @method listen - Start listening on the given port and environment.
30+
* @method setGlobalRoutePrefix - Sets the global route prefix for the application.
31+
* @method setEngine - Configures the application's view engine based on the provided configuration options.
32+
* @method isDevelopment - Verifies if the current environment is development.
7833
*/
79-
@provide(ApplicationExpress)
80-
class ApplicationExpress extends ApplicationBase implements IApplicationExpress {
34+
@provide(AppExpress)
35+
class AppExpress extends ApplicationBase implements IWebServer {
8136
private app: express.Application;
8237
private port: number;
8338
private environment: ServerEnvironment;
8439
private container: Container;
85-
private middlewares: Array<ExpressHandler> = [];
86-
private globalPrefix: string | undefined;
40+
private globalPrefix: string = "/";
41+
private middlewares: Array<ExpressHandler | MiddlewareConfig | ExpressoMiddleware> = [];
42+
private console: Console;
8743

8844
protected configureServices(): void | Promise<void> {}
8945
protected postServerInitialization(): void | Promise<void> {}
@@ -97,6 +53,15 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
9753
process.exit(0);
9854
}
9955

56+
/**
57+
* Configures the InversifyJS container.
58+
* @param container - The InversifyJS container.
59+
*/
60+
async configure(container: Container): Promise<void> {
61+
this.container = container;
62+
this.console = this.container.get(Console);
63+
}
64+
10065
/**
10166
* Configures the Express application with the provided middleware entries.
10267
* @param app - The Express application instance.
@@ -137,19 +102,16 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
137102
* @param middlewares - An array of Express middlewares to be applied.
138103
* @returns The configured Application instance.
139104
*/
140-
private async init(
141-
container: Container,
142-
middlewares: Array<express.RequestHandler> = [],
143-
): Promise<ApplicationExpress> {
105+
private async init(): Promise<AppExpress> {
144106
await this.configureServices();
145107

146-
const middleware = container.get<IMiddleware>(Middleware);
108+
const middleware = this.container.get(Middleware);
147109
const sortedMiddlewarePipeline = middleware.getMiddlewarePipeline();
148110
const pipeline = sortedMiddlewarePipeline.map((entry) => entry.middleware);
149111

150-
this.middlewares.push(...middlewares, ...(pipeline as Array<ExpressHandler>));
112+
this.middlewares.push(...(pipeline as Array<ExpressHandler>));
151113

152-
const expressServer = new InversifyExpressServer(container, null, {
114+
const expressServer = new InversifyExpressServer(this.container, null, {
153115
rootPath: this.globalPrefix as string,
154116
});
155117

@@ -167,23 +129,6 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
167129
return this;
168130
}
169131

170-
/**
171-
* Create and configure the Express application.
172-
* @param container - The InversifyJS container.
173-
* @param middlewares - An array of Express middlewares to be applied.
174-
* @returns The configured Application instance.
175-
*/
176-
public async create(
177-
container: Container,
178-
middlewares: Array<ExpressHandler> = [],
179-
): Promise<ApplicationExpress> {
180-
this.container = container;
181-
this.middlewares = middlewares;
182-
this.globalPrefix = this.globalPrefix || "/";
183-
184-
return this;
185-
}
186-
187132
/**
188133
* Start listening on the given port and environment.
189134
* @param port - The port number to listen on.
@@ -195,17 +140,14 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
195140
environment: ServerEnvironment,
196141
consoleMessage?: IApplicationMessageToConsole,
197142
): Promise<void> {
198-
/* Initializes the application and executes the middleware pipeline */
199-
await this.init(this.container, this.middlewares as Array<express.RequestHandler>);
143+
await this.init();
200144

201-
/* Sets the port and environment */
202145
this.port = port;
203146
this.environment = environment;
204147
this.app.set("env", environment);
205148

206149
this.app.listen(this.port, () => {
207-
const console: Console = this.container.get<Console>(Console);
208-
console.messageServer(this.port, this.environment, consoleMessage);
150+
this.console.messageServer(this.port, this.environment, consoleMessage);
209151

210152
(["SIGTERM", "SIGHUP", "SIGBREAK", "SIGQUIT", "SIGINT"] as Array<NodeJS.Signals>).forEach(
211153
(signal) => {
@@ -214,7 +156,7 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
214156
);
215157
});
216158

217-
await Promise.resolve(this.postServerInitialization());
159+
await this.postServerInitialization();
218160
}
219161

220162
/**
@@ -265,16 +207,6 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
265207
.error("isDevelopment() method must be called on `PostServerInitialization`", "application");
266208
return false;
267209
}
268-
269-
/**
270-
* Verifies if the current environment is production.
271-
*
272-
* @returns A boolean value indicating whether the current environment is production or not.
273-
*
274-
*/
275-
public get ExpressApp(): express.Application {
276-
return this.app;
277-
}
278210
}
279211

280-
export { ApplicationExpress as AppExpress, ServerEnvironment };
212+
export { AppExpress };

0 commit comments

Comments
 (0)