Skip to content

Commit

Permalink
feat: fix path call on expresso middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
rsaz committed Mar 3, 2024
1 parent 3a6f7dc commit 934ae17
Showing 1 changed file with 53 additions and 28 deletions.
81 changes: 53 additions & 28 deletions src/adapter-express/application-express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,29 @@ import { IApplicationExpress } from "./application-express.interface";
import { InversifyExpressServer } from "./express-utils/inversify-express-server";
import { ApplicationBase } from "./application-base";

/**
* ExpressHandler Type
*
* The ExpressHandler type is a union type that represents various types of Express middleware functions.
* It can be one of the following types:
* - express.ErrorRequestHandler: Handles errors in the middleware pipeline.
* - express.RequestParamHandler: Handles parameters in the middleware pipeline.
* - express.RequestHandler: General request handler.
* - undefined: Represents the absence of a handler.
*/
type ExpressHandler =
| express.ErrorRequestHandler
| express.RequestParamHandler
| express.RequestHandler
| undefined;

/**
* MiddlewareConfig Interface
*
* The MiddlewareConfig interface specifies the structure for middleware configuration objects.
* - path: Optional. The route path for which the middleware is configured.
* - middlewares: An array of ExpressHandler types that make up the middleware pipeline for the route specified by 'path'.
*/
type MiddlewareConfig = {
path?: string;
middlewares: Array<ExpressHandler>;
Expand Down Expand Up @@ -80,6 +97,40 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
process.exit(0);
}

/**
* Configures the Express application with the provided middleware entries.
* @param app - The Express application instance.
* @param middlewareEntries - An array of Express middleware entries to be applied.
*/
private async configureMiddleware(
app: express.Application,
middlewareEntries: Array<ExpressHandler | MiddlewareConfig | ExpressoMiddleware>,
): Promise<void> {
for (const entry of middlewareEntries) {
if (typeof entry === "function") {
app.use(entry as express.RequestHandler);
// eslint-disable-next-line no-prototype-builtins
} else if (entry?.hasOwnProperty("path")) {
const { path, middlewares } = entry as MiddlewareConfig;
for (const mid of middlewares) {
if (path) {
if (typeof mid === "function") {
app.use(path, mid as express.RequestHandler);
} else {
const middleware = mid as unknown as ExpressoMiddleware;
middleware.use = middleware.use.bind(middleware);
app.use(path, middleware.use);
}
}
}
} else {
const middleware = entry as ExpressoMiddleware;
middleware.use = middleware.use.bind(middleware);
app.use(middleware.use);
}
}
}

/**
* Create and configure the Express application.
* @param container - The InversifyJS container.
Expand All @@ -90,46 +141,20 @@ class ApplicationExpress extends ApplicationBase implements IApplicationExpress
container: Container,
middlewares: Array<express.RequestHandler> = [],
): Promise<ApplicationExpress> {
await Promise.resolve(this.configureServices());
await this.configureServices();

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

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

const allMiddlewareEntries: Array<ExpressHandler | MiddlewareConfig | ExpressoMiddleware> = [
...this.middlewares,
];

const expressServer = new InversifyExpressServer(container, null, {
rootPath: this.globalPrefix as string,
});

expressServer.setConfig((app: express.Application) => {
allMiddlewareEntries.forEach((entry) => {
if (typeof entry === "function") {
app.use(entry as express.RequestHandler);
// eslint-disable-next-line no-prototype-builtins
} else if (entry?.hasOwnProperty("path")) {
const { path, middlewares } = entry as MiddlewareConfig;
middlewares.forEach((mid) => {
if (path) {
if (typeof mid === "function") {
app.use(path, mid as express.RequestHandler);
} else {
const middleware = mid as unknown as ExpressoMiddleware;
middleware.use = middleware.use.bind(middleware);
app.use(middleware.use as express.RequestHandler);
}
}
});
} else {
const middleware = entry as ExpressoMiddleware;
middleware.use = middleware.use.bind(middleware);
app.use(middleware.use);
}
});
this.configureMiddleware(app, this.middlewares);
});

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

0 comments on commit 934ae17

Please sign in to comment.