Skip to content

Commit

Permalink
Update BaseMiddleware to allow async handlers (#419)
Browse files Browse the repository at this point in the history
* feat: update BaseMiddleware to allow async handlers

* fix: update Server.resolveMiddleware

update resolveMiddleware to return handler result

* docs: update docs

* feat: update server to return handler result
  • Loading branch information
notaphplover authored Jan 30, 2025
1 parent 4ae52d1 commit b7ea184
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

### Changed
- Updated `BaseMiddleware.handler` to allow async handlers.

### Fixed

Expand Down
4 changes: 2 additions & 2 deletions src/base_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export abstract class BaseMiddleware implements BaseMiddleware {
public abstract handler(
req: Request,
res: Response,
next: NextFunction,
): void;
next: NextFunction
): void | Promise<void>;
}
9 changes: 7 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,16 @@ export class InversifyExpressServer {
this._container.get<MiddlewareInstance>(middlewareItem);

if (middlewareInstance instanceof BaseMiddleware) {
return (req: Request, res: Response, next: NextFunction): void => {
return (
req: Request,
res: Response,
next: NextFunction,
): void | Promise<void> => {
const mReq: BaseMiddleware =
this._container.get<BaseMiddleware>(middlewareItem);
mReq.httpContext = this._getHttpContext(req);
mReq.handler(req, res, next);

return mReq.handler(req, res, next);
};
}

Expand Down

0 comments on commit b7ea184

Please sign in to comment.