http-server icon indicating copy to clipboard operation
http-server copied to clipboard

Named middleware not running? + Empty response

Open theoludwig opened this issue 1 year ago • 3 comments

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.ts to 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.

theoludwig avatar Aug 12 '24 15:08 theoludwig

Hey @theoludwig! 👋🏻

You have to call the method.

router.get('/', async () => 'It works!').use(middleware.foobar())

RomainLanz avatar Aug 12 '24 16:08 RomainLanz

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?

theoludwig avatar Aug 12 '24 17:08 theoludwig

Maybe possible. But I think, there should have been a runtime exception in this particular case.

thetutlage avatar Aug 13 '24 05:08 thetutlage

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 ?

nadlgit avatar Nov 27 '24 01:11 nadlgit

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.

thetutlage avatar Nov 27 '24 07:11 thetutlage