Skip to content

Commit 934ae17

Browse files
committed
feat: fix path call on expresso middleware
1 parent 3a6f7dc commit 934ae17

File tree

1 file changed

+53
-28
lines changed

1 file changed

+53
-28
lines changed

src/adapter-express/application-express.ts

+53-28
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,29 @@ import { IApplicationExpress } from "./application-express.interface";
1515
import { InversifyExpressServer } from "./express-utils/inversify-express-server";
1616
import { ApplicationBase } from "./application-base";
1717

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+
*/
1828
type ExpressHandler =
1929
| express.ErrorRequestHandler
2030
| express.RequestParamHandler
2131
| express.RequestHandler
2232
| undefined;
2333

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+
*/
2441
type MiddlewareConfig = {
2542
path?: string;
2643
middlewares: Array<ExpressHandler>;
@@ -80,6 +97,40 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
8097
process.exit(0);
8198
}
8299

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+
83134
/**
84135
* Create and configure the Express application.
85136
* @param container - The InversifyJS container.
@@ -90,46 +141,20 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
90141
container: Container,
91142
middlewares: Array<express.RequestHandler> = [],
92143
): Promise<ApplicationExpress> {
93-
await Promise.resolve(this.configureServices());
144+
await this.configureServices();
94145

95146
const middleware = container.get<IMiddleware>(Middleware);
96147
const sortedMiddlewarePipeline = middleware.getMiddlewarePipeline();
97148
const pipeline = sortedMiddlewarePipeline.map((entry) => entry.middleware);
98149

99150
this.middlewares.push(...middlewares, ...(pipeline as Array<ExpressHandler>));
100151

101-
const allMiddlewareEntries: Array<ExpressHandler | MiddlewareConfig | ExpressoMiddleware> = [
102-
...this.middlewares,
103-
];
104-
105152
const expressServer = new InversifyExpressServer(container, null, {
106153
rootPath: this.globalPrefix as string,
107154
});
108155

109156
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);
133158
});
134159

135160
expressServer.setErrorConfig((app: express.Application) => {

0 commit comments

Comments
 (0)