@@ -15,12 +15,29 @@ import { IApplicationExpress } from "./application-express.interface";
15
15
import { InversifyExpressServer } from "./express-utils/inversify-express-server" ;
16
16
import { ApplicationBase } from "./application-base" ;
17
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
+ */
18
28
type ExpressHandler =
19
29
| express . ErrorRequestHandler
20
30
| express . RequestParamHandler
21
31
| express . RequestHandler
22
32
| undefined ;
23
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
+ */
24
41
type MiddlewareConfig = {
25
42
path ?: string ;
26
43
middlewares : Array < ExpressHandler > ;
@@ -80,6 +97,40 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
80
97
process . exit ( 0 ) ;
81
98
}
82
99
100
+ /**
101
+ * Configures the Express application with the provided middleware entries.
102
+ * @param app - The Express application instance.
103
+ * @param middlewareEntries - An array of Express middleware entries to be applied.
104
+ */
105
+ private async configureMiddleware (
106
+ app : express . Application ,
107
+ middlewareEntries : Array < ExpressHandler | MiddlewareConfig | ExpressoMiddleware > ,
108
+ ) : Promise < void > {
109
+ for ( const entry of middlewareEntries ) {
110
+ if ( typeof entry === "function" ) {
111
+ app . use ( entry as express . RequestHandler ) ;
112
+ // eslint-disable-next-line no-prototype-builtins
113
+ } else if ( entry ?. hasOwnProperty ( "path" ) ) {
114
+ const { path, middlewares } = entry as MiddlewareConfig ;
115
+ for ( const mid of middlewares ) {
116
+ if ( path ) {
117
+ if ( typeof mid === "function" ) {
118
+ app . use ( path , mid as express . RequestHandler ) ;
119
+ } else {
120
+ const middleware = mid as unknown as ExpressoMiddleware ;
121
+ middleware . use = middleware . use . bind ( middleware ) ;
122
+ app . use ( path , middleware . use ) ;
123
+ }
124
+ }
125
+ }
126
+ } else {
127
+ const middleware = entry as ExpressoMiddleware ;
128
+ middleware . use = middleware . use . bind ( middleware ) ;
129
+ app . use ( middleware . use ) ;
130
+ }
131
+ }
132
+ }
133
+
83
134
/**
84
135
* Create and configure the Express application.
85
136
* @param container - The InversifyJS container.
@@ -90,46 +141,20 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
90
141
container : Container ,
91
142
middlewares : Array < express . RequestHandler > = [ ] ,
92
143
) : Promise < ApplicationExpress > {
93
- await Promise . resolve ( this . configureServices ( ) ) ;
144
+ await this . configureServices ( ) ;
94
145
95
146
const middleware = container . get < IMiddleware > ( Middleware ) ;
96
147
const sortedMiddlewarePipeline = middleware . getMiddlewarePipeline ( ) ;
97
148
const pipeline = sortedMiddlewarePipeline . map ( ( entry ) => entry . middleware ) ;
98
149
99
150
this . middlewares . push ( ...middlewares , ...( pipeline as Array < ExpressHandler > ) ) ;
100
151
101
- const allMiddlewareEntries : Array < ExpressHandler | MiddlewareConfig | ExpressoMiddleware > = [
102
- ...this . middlewares ,
103
- ] ;
104
-
105
152
const expressServer = new InversifyExpressServer ( container , null , {
106
153
rootPath : this . globalPrefix as string ,
107
154
} ) ;
108
155
109
156
expressServer . setConfig ( ( app : express . Application ) => {
110
- allMiddlewareEntries . forEach ( ( entry ) => {
111
- if ( typeof entry === "function" ) {
112
- app . use ( entry as express . RequestHandler ) ;
113
- // eslint-disable-next-line no-prototype-builtins
114
- } else if ( entry ?. hasOwnProperty ( "path" ) ) {
115
- const { path, middlewares } = entry as MiddlewareConfig ;
116
- middlewares . forEach ( ( mid ) => {
117
- if ( path ) {
118
- if ( typeof mid === "function" ) {
119
- app . use ( path , mid as express . RequestHandler ) ;
120
- } else {
121
- const middleware = mid as unknown as ExpressoMiddleware ;
122
- middleware . use = middleware . use . bind ( middleware ) ;
123
- app . use ( middleware . use as express . RequestHandler ) ;
124
- }
125
- }
126
- } ) ;
127
- } else {
128
- const middleware = entry as ExpressoMiddleware ;
129
- middleware . use = middleware . use . bind ( middleware ) ;
130
- app . use ( middleware . use ) ;
131
- }
132
- } ) ;
157
+ this . configureMiddleware ( app , this . middlewares ) ;
133
158
} ) ;
134
159
135
160
expressServer . setErrorConfig ( ( app : express . Application ) => {
0 commit comments