diff --git a/express/package.json b/express/package.json index 9deade4..76788ae 100644 --- a/express/package.json +++ b/express/package.json @@ -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", diff --git a/express/src/middleware.ts b/express/src/middleware.ts index c9d151d..4a72a5a 100644 --- a/express/src/middleware.ts +++ b/express/src/middleware.ts @@ -46,22 +46,21 @@ function invokeMiddleware( args: Parameters | Parameters, ) { const next = args[args.length - 1] as NextFunction; - let instance: InstanceType>; try { - instance = Container.get(middleware as InjectionToken); - } catch { - try { - instance = new (middleware as Type)(...args); - } catch (err) { - next(err); + let instance: MiddlewareClass | ErrorMiddlewareClass | MiddlewareFunction; - return; + if (typeof middleware === 'function') { + if (middleware.prototype?.use) { + instance = new (middleware as Type)(...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) {