Skip to content

[4.10.0] cannot .use middleware created from createMiddleware that doesn't return anything #4462

@NamesMT

Description

@NamesMT

What version of Hono are you using?

4.10.0

What runtime/platform is your app running on? (with version if possible)

What steps can reproduce the bug?

import { Hono } from 'hono'
import { createMiddleware } from 'hono/factory'

const sampleMiddleware = createMiddleware(async (c) => {
  console.warn(c.req.url.length)
})

const _app = new Hono()
  .use(sampleMiddleware) // Error here

What is the expected behavior?

Works normally

What do you see instead?

Image

Additional information

This is because the response type of the middleware handler that is created got created as HandlerResponse<any> instead of the default = Response, not sure why this happens, as the declaration looks good to me honestly, and it works fine for non-async middleware handler and async handler that returns any response.

From my test it can be fixed easily by changing from
export type MiddlewareHandler<E extends Env = any, P extends string = string, I extends Input = {}, R extends HandlerResponse<any> = Response>
to
export type MiddlewareHandler<E extends Env = any, P extends string = string, I extends Input = {}, R extends HandlerResponse<any> = any>,
Similar to how Handler type is declared.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions