Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
fix(express): added fix to handle arrow functions as middlewares (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiisol authored Jan 30, 2023
1 parent 03f7c58 commit 87d8c72
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion express/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@decorators/express",
"version": "2.8.0",
"version": "2.8.1",
"description": "node decorators - decorators for express library",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
21 changes: 10 additions & 11 deletions express/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,21 @@ function invokeMiddleware(
args: Parameters<MiddlewareFunction> | Parameters<ErrorMiddlewareFunction>,
) {
const next = args[args.length - 1] as NextFunction;
let instance: InstanceType<Type<MiddlewareClass | ErrorMiddlewareClass>>;

try {
instance = Container.get(middleware as InjectionToken);
} catch {
try {
instance = new (middleware as Type<MiddlewareClass | ErrorMiddlewareClass>)(...args);
} catch (err) {
next(err);
let instance: MiddlewareClass | ErrorMiddlewareClass | MiddlewareFunction;

return;
if (typeof middleware === 'function') {
if (middleware.prototype?.use) {
instance = new (middleware as Type<MiddlewareClass | ErrorMiddlewareClass>)(...args);
} else {
instance = middleware as MiddlewareFunction;
}
} else {
instance = Container.get(middleware);
}
}

try {
const handler = instance.use ?? instance;
const handler = (instance as MiddlewareClass | ErrorMiddlewareClass)?.use ?? instance;
const result = typeof handler === 'function' ? handler.apply(instance, args) : instance;

if (result instanceof Promise) {
Expand Down

0 comments on commit 87d8c72

Please sign in to comment.