Named middleware not running? + Empty response
Package version
6.12.1
Describe the bug
Apparently named middleware are not working, when .use one it sends a 200 Ok, empty response.
Steps to reproduce are pretty minimal/small:
-
npm init adonisjs@latest adonis-slim-kit -- --kit=slim -
node ace make:middleware foobar --stack=named - Edit
start/routes.tsto use the middleware:
import router from '@adonisjs/core/services/router'
import { middleware } from '#start/kernel'
router.get('/', async () => 'It works!').use(middleware.foobar)
"It works" is never sent (empty response, only 200 Ok), and removing .use(middleware.foobar) make it work again.
Hey @theoludwig! 👋🏻
You have to call the method.
router.get('/', async () => 'It works!').use(middleware.foobar())
Thank you, oops, that was a small mistake.
Why TypeScript didn't complain? Is there a way to make the TypeScript type stricter here, so small mistake like that are detected by the IDE?
Maybe possible. But I think, there should have been a runtime exception in this particular case.
Hi, after looking at it it, I think the issue is that in file src/router/route.ts, use method expects either MiddlewareFn or ParsedNamedMiddleware. When you use a named middleware without parenthesis, it is considered as MiddlewareFn, unless the middleware has extra arguments. That's why, when you try to use auth middleware without parenthesis, TypeScript complains, as there is also a third options argument.
That's said, it doesn't seem that simple to fix, what do you think @RomainLanz and @thetutlage ?
Yeah, that's super hard to fix, because the use method accepts a function that is considered as inline implementation of the middleware.
I think we can close this issue, since the additional checks to make it work better doesn't have a great ROI.